I know it’s a little late, but for anyone who might be dealing with image format issues, I had come up with a general approach to resolving this. I’ve created a function which you provide an image path, all images are scanned and converted to the desired format (if not already) and the string path is returned. I made it Windows friendly as well just in case. I do want to mention, if you have any files other than images in that directory, it will attempt to convert it (obviously throwing an exception). You could always add image extension verification if desired.
import os
import platform
from PIL import Image
def image_path(imgFilePath, desiredFormat='png'):
'''
:param imgFilePath: Path containing images
:param desiredFormat: Format which all images will be converted to (If they are not already)
:return: imgFilePath will be returned back after conversion is complete
'''
dirChar = ('\\' if platform.system()=='Windows' else '/') #Handle win vs linux pathing
images = os.listdir(imgFilePath)
desiredFormat = desiredFormat.replace(".", "")
for imgFileName in images:
extIndex = -(len(imgFileName) - imgFileName.index("."))
if imgFileName[extIndex:] != f".{desiredFormat}":
fullImagePath = rf'{imgFilePath}{dirChar}{imgFileName}'
print(f'Image not {desiredFormat}, will be converted: {imgFileName}')
imgOutPath = rf'{imgFilePath}{dirChar}{imgFileName[:extIndex]}.{desiredFormat}'
imgOut = Image.open(fullImagePath)
imgOut.save(imgOutPath)
print(rf'cleaning old image...')
os.remove(fullImagePath)
print(f'new image output: {imgOutPath}')
return imgFilePath