from PIL import Image def IsValidImage(img_path): bValid = True try: Image.open(img_path).verify() except: bValid = False return bValid def transimg(img_path): if IsValidImage(img_path): try: str = img_path.rsplit(".", 1) output_img_path = str[0] + ".jpg" print(output_img_path) im = Image.open(img_path) im.save(output_img_path) return True except: return False if __name__ == "__main__": img_path = "images/lena.png" print(transimg(img_path))