2
votes

I'm trying to crop images in python, and need keep the exif data when I crop them, but I lose the exif data of the original image when I save the resulting cropped image, how can I avoid this from happening? The code I've been running:

from imutils import face_utils
import imutils
import numpy as np
import collections
import dlib
import cv2
import glob
import os

detector = dlib.get_frontal_face_detector()
PREDICTOR_PATH = "shape_predictor_68_face_landmarks.dat"
predictor = dlib.shape_predictor(PREDICTOR_PATH)

def face_remap(shape):
    remapped_image = cv2.convexHull(shape)
    return remapped_image


def faceCrop(imagePattern):

    imgList=glob.glob(imagePattern)
    if len(imgList)<=0:
        print ('No Images Found')
        return
    for img in imgList:
        image = cv2.imread(img)
        image = imutils.resize(image, width=500)
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        out_face = np.zeros_like(image)
        rects = detector(gray, 1)
        for (i, rect) in enumerate(rects):
            shape = predictor(gray, rect)
            shape = face_utils.shape_to_np(shape)
            #initialize mask array
            remapped_shape = np.zeros_like(shape) 
            feature_mask = np.zeros((image.shape[0], image.shape[1]))   
            # we extract the face
            remapped_shape = face_remap(shape)
            cv2.fillConvexPoly(feature_mask, remapped_shape[0:27], 1)
            feature_mask = feature_mask.astype(np.bool)
            out_face[feature_mask] = image[feature_mask]
            fname,ext=os.path.splitext(img)
            cv2.imwrite(fname+'_crop'+ext, out_face)
    print ('Listo \n')                
faceCrop('/Users/alejandroramirez/Documents/imagenes nuevas/2/*.JPG')
Can you show us what you have tried so far?Renato Afonso
Off course, I uploaded the code in the questionAlejandro Ramirez Baez
OpenCV won't help you with that. Find some other python library that lets you read and write EXIF data. this may be a good starting point.Dan Mašek
Thanks, maybe pillow would be usefull?Alejandro Ramirez Baez