Python实现图片转jpg格式

时间:2025-02-20 08:30:04

Python实现图片转jpg格式

    经常碰到图片为直接改后缀为.jpg文件的情况,这时候就需要将图片格式转为真正的.jpg格式文件,下面为大家提供一种转化脚本供参考:

# encoding: utf-8
import os
from PIL import Image
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True

#文件夹路径
path = r'xxx'


def pilConvertJPG(path):
    for a, _, c in (path):
        for n in c:
            if '.jpg' in n or '.png' in n or '.jpeg' in n:
                img = ((a, n))
                rgb_im = ('RGB')
                error_img_path = (a,n)
                (error_img_path)
                n = ''.join(filter(lambda n: ord(n) < 256, n))
                jpg_img_path = ((a, n).replace('\\', '/'))[0]
                jpg_img_path += '.jpg'
                print(jpg_img_path)
                rgb_im.save(jpg_img_path)



pilConvertJPG(path)