I am trying to remove EXIF data from images in a dataset (which I will use in transfer learning). However, it does not seem to be working. Below is my code:
import os
from PIL import Image
import piexif
import imghdr
from tqdm import tqdm
import warnings
Folder = 'drive/My Drive/PetImages'
labels =['Dog', 'Cat']
for label in labels:
imageFolder = os.path.join(Folder, label)
listImages = os.listdir(imageFolder)
for img in tqdm(listImages):
imgPath = os.path.join(imageFolder,img)
try:
img = Image.open(imgPath)
data = list(img.getdata())
image_without_exif = Image.new(img.mode, img.size)
image_without_exif.putdata(data)
image_without_exif.save(img)
print("done")
except:
print("except")
I tried saving the image using PIL (as per a previously asked question: Python: Remove Exif info from images) but the output is purely composed of "except"s.
I tried again using the piexif module, as below:
# Same imports as above
Folder = 'drive/My Drive/PetImages'
labels =['Dog', 'Cat']
for label in labels:
imageFolder = os.path.join(Folder, label)
listImages = os.listdir(imageFolder)
for img in tqdm(listImages):
imgPath = os.path.join(imageFolder,img)
try:
ImageType = img.format
# warnings.filterwarnings("error")
if ImageType in ["JPEG", "TIF", "WAV"]:
exif_data = img._getexif()
print(exif_data)
piexif.remove(img)
print("done")
except:
print("except")
In the code above, I check for the image type first to make sure the method _getexif() actually exists, then I just remove the data after saving it in exif_data variable. The output consisted of "except"s and the occasional exif data (in the form of a dictionary) or "None" if it doesn't exist but never the word "done". Why doesn't it reach that part?
For anyone stumbling upon this through Google, there is a simple solution using PIL:
from PIL import Image
im = Image.open('some-image.jpg')
# this clears all exif data
im.getexif().clear()
im.save('some-image-without-exif.jpg')
I thought that getexif()
only allows read access as the name might imply, but it turns out that this is not the case.
Edit: In my case, it even worked to just load and save the file, without im.getexif().clear()
. I don't know how reliable that is, though.
That command definitely removes exif-data from the image-object, though. This can be simply tested in a Python shell:
>>> from PIL import Image
>>> im = Image.open('some-image.jpg')
>>> print(im.getexif())
{296: 2, 282: 72.0, 283: 72.0 ..... }
>>> im.getexif().clear()
>>> print(im.getexif())
{}