千家信息网

如何实现python图片格式转换脚本

发表于:2024-12-12 作者:千家信息网编辑
千家信息网最后更新 2024年12月12日,这篇文章给大家分享的是有关如何实现python图片格式转换脚本的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。1.cv2pip install opencv-python之后
千家信息网最后更新 2024年12月12日如何实现python图片格式转换脚本

这篇文章给大家分享的是有关如何实现python图片格式转换脚本的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

1.cv2

pip install opencv-python之后就可以import cv2

1.1 导包

import osimport cv2import sysimport numpy as np

1.2 路径设置

path = r"C:\\Users\\86775\\PycharmProjects\\pythonProject2\\ResNet\\test_change\\"path3 = r"C:\\Users\\86775\\PycharmProjects\\pythonProject2\\ResNet\\test_change2\\"

1.3 改格式重新传

1.3.1 用os.path.splitext()来判断

images = os.listdir(path)for i in os.listdir(path):    print(os.path.splitext(i))  # ('34474006827920603', '.png')    if os.path.splitext(i)[1] == ".jpeg":        img = cv2.imread(path + i)        # print(img)        new_imagename = i.replace(".jpeg", ".jpg")        cv2.imwrite(path3 + new_imagename, img)    elif os.path.splitext(i)[1] == ".png":        img = cv2.imread(path + i)        # print(img)        new_imagename = i.replace(".png", ".jpg")        cv2.imwrite(path3 + new_imagename, img)    elif os.path.splitext(i)[1] == ".JPG":        img = cv2.imread(path + i)        # print(img)        new_imagename = i.replace(".JPG", ".jpg")        cv2.imwrite(path3 + new_imagename, img)    elif os.path.splitext(i)[1] == ".PNG":        img = cv2.imread(path + i)        # print(img)        new_imagename = i.replace(".PNG", ".jpg")        cv2.imwrite(path3 + new_imagename, img)    elif os.path.splitext(i)[1] == ".jpg":        img = cv2.imread(path + i)        # print(img)        cv2.imwrite(path3 + i, img)

1.3.2 用split()来判断

images = os.listdir(path)for i in os.listdir(path):    print(i.split("."))    if i.split(".")[1] == "jpeg":        img = cv2.imread(path + i)        # print(img)        new_imagename = i.replace(".jpeg", ".jpg")        cv2.imwrite(path3 + new_imagename, img)    elif i.split(".")[1] == "png":        img = cv2.imread(path + i)        # print(img)        new_imagename = i.replace(".png", ".jpg")        cv2.imwrite(path3 + new_imagename, img)    elif i.split(".")[1] == "JPG":        img = cv2.imread(path + i)        # print(img)        new_imagename = i.replace(".JPG", ".jpg")        cv2.imwrite(path3 + new_imagename, img)    elif i.split(".")[1] == "PNG":        img = cv2.imread(path + i)        # print(img)        new_imagename = i.replace(".PNG", ".jpg")        cv2.imwrite(path3 + new_imagename, img)    elif i.split(".")[1] == "jpg":        img = cv2.imread(path + i)        # print(img)        cv2.imwrite(path3 + i, img)

1.4 查看结果

2.pillow

pip install pillow之后就可以from PIL import Image

2.1 导包

import osfrom PIL import Image

2.2 路径设置

path2 = r"C:\\Users\\86775\\PycharmProjects\\pythonProject2\\ResNet\\test_change\\"path3 = r"C:\\Users\\86775\\PycharmProjects\\pythonProject2\\ResNet\\test_change2\\"

2.3 写格式修改函数

RGBA意思是红色,绿色,蓝色,Alpha的色彩空间,Alpha指透明度,而JPG不支持透明度。
所以其他格式不转jpg可以直接保存为.png格式,转jpg就需要丢弃Alpha
img = img.convert("RGB"),转换成RGB,保证和jpg格式的通道数相同,也算一种图片类型变换的防报错机制

def jpeg2jpg(path_in, path_out):    img = Image.open(path_in)    img = img.convert("RGB")    img.save(path_out, "JPEG", quality=80, optimize=True, progressive=True)def png2jpg(path_in, path_out):    img = Image.open(path_in)    img = img.convert("RGB")    img.save(path_out, "JPEG", quality=80, optimize=True, progressive=True)def JPG2jpg(path_in, path_out):    img = Image.open(path_in)    img = img.convert("RGB")    img.save(path_out, "JPEG", quality=80, optimize=True, progressive=True)def PNG2jpg(path_in, path_out):    img = Image.open(path_in)    img = img.convert("RGB")    img.save(path_out, "JPEG", quality=80, optimize=True, progressive=True)def jpg2jpg(path_in, path_out):    img = Image.open(path_in)    img = img.convert("RGB")    img.save(path_out, "JPEG", quality=80, optimize=True, progressive=True)

2.4 开始转换

images = os.listdir(path2)for i in images:    print(i)    if os.path.splitext(i)[1] == ".jpeg":        source = path2 + "\\" + str(i.split(".")[0]) + ".jpeg"        target = path3 + "\\" + str(i.split(".")[0]) + ".jpg"        jpeg2jpg(source, target)    elif os.path.splitext(i)[1] == ".png":        source = path2 + "\\" + str(i.split(".")[0]) + ".png"        target = path3 + "\\" + str(i.split(".")[0]) + ".jpg"        png2jpg(source, target)    elif os.path.splitext(i)[1] == ".JPG":        source = path2 + "\\" + str(i.split(".")[0]) + ".JPG"        target = path3 + "\\" + str(i.split(".")[0]) + ".jpg"        JPG2jpg(source, target)    elif os.path.splitext(i)[1] == ".PNG":        source = path2 + "\\" + str(i.split(".")[0]) + ".PNG"        target = path3 + "\\" + str(i.split(".")[0]) + ".jpg"        PNG2jpg(source, target)    elif os.path.splitext(i)[1] == ".jpg":        source = path2 + "\\" + str(i.split(".")[0]) + ".jpg"        target = path3 + "\\" + str(i.split(".")[0]) + ".jpg"        jpg2jpg(source, target)

2.5 查看结果

感谢各位的阅读!关于"如何实现python图片格式转换脚本"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

0