from PIL import Image
from os import listdir
from os.path import splitext
import cv2
target_directory = r"E:\pre\png"
target = '.jpg'
jpg_folder_path = r"E:\pre\jpeg"
for file in listdir(target_directory):
filename, extension = splitext(file)
try:
if extension not in ['.py', target]:
im = Image.open(filename + extension)
#im.save((os.path.join(jpg_folder_path, im))filename + target)
cv2.imwrite(os.path.join(jpg_folder_path , filename + target), im)
except OSError:
print('Cannot convert %s' % file)
OUTPUT
Cannot convert 000c1434d8d7.png
Cannot convert 00a8624548a9.png
..
Use pathlib for the filesystem access. That is more pythonic way to do.
from pathlib import Path
from PIL import Image
inputPath = Path("E:/pre/png")
inputFiles = inputPath.glob("**/*.png")
outputPath = Path("E:/pre/jpeg")
for f in inputFiles:
outputFile = outputPath / Path(f.stem + ".jpg")
im = Image.open(f)
im.save(outputFile)