# Python3 program to convert PNG images in a folder to JPG of specified quality # This script should be stored in the same folder where PNG files are stored. # Since Linux OS is case sensitive, the extension of PNG file is checked for # both lower and upper case. The extension JPG in output is added in lowercase. #------------------------------------------------------------------------------ # Import necessary libraries from PIL import Image import os, sys, glob qty = 75 extn = 'png' folder = os.getcwd() i = 0 for path, subdirs, files in os.walk(folder): for name in files: fn = name ext1 = os.path.splitext(fn)[-1].lower() ext2 = ext1.partition('.')[2] if (ext2 == extn): img = Image.open(fn) jpg_file = fn.strip(ext1) + ".jpg" # Save image in JPG format img.save(jpg_file, "JPEG", quality=qty, optimize=True) # Close image file img.close() i = i + 1 # Print User Message print("File number " + str(i) + " converted to JPG format") print("\n")