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?