欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python图片格式转换脚本

程序员文章站 2023-12-31 15:35:10
目录前言1.cv21.1 导包1.2 路径设置1.3 改格式重新传1.4 查看结果2.pillow2.1 导包2.2 路径设置2.3 写格式修改函数2.4 开始转换2.5 查看结果前言常见的图像任务通...

前言

常见的图像任务通常需要把照片统一成相同的格式,所以此文章正是为了统一格式而生,常见的主要有cv2和pil.image的相关操作,照片格式是一串数字加上后缀名

1.cv2

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

1.1 导包

import os
import cv2
import sys
import numpy as np

1.2 路径设置

path = r"c:\\users\\86775\\pycharmprojects\\pythonproject2\\resnet\\test_change\\"
path2 = 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(path2 + 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(path2 + 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(path2 + 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(path2 + new_imagename, img)

    elif os.path.splitext(i)[1] == ".jpg":
        img = cv2.imread(path + i)
        # print(img)
        cv2.imwrite(path2 + 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(path2 + new_imagename, img)

    elif i.split(".")[1] == "png":
        img = cv2.imread(path + i)
        # print(img)
        new_imagename = i.replace(".png", ".jpg")
        cv2.imwrite(path2 + new_imagename, img)

    elif i.split(".")[1] == "jpg":
        img = cv2.imread(path + i)
        # print(img)
        new_imagename = i.replace(".jpg", ".jpg")
        cv2.imwrite(path2 + new_imagename, img)

    elif i.split(".")[1] == "png":
        img = cv2.imread(path + i)
        # print(img)
        new_imagename = i.replace(".png", ".jpg")
        cv2.imwrite(path2 + new_imagename, img)

    elif i.split(".")[1] == "jpg":
        img = cv2.imread(path + i)
        # print(img)
        cv2.imwrite(path2 + i, img)

1.4 查看结果

python图片格式转换脚本

2.pillow

pip install pillow之后就可以from pil import image

2.1 导包

import os
from pil import image

2.2 路径设置

path1 = r"c:\\users\\86775\\pycharmprojects\\pythonproject2\\resnet\\test_change\\"
path2 = 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(path1)
for i in images:
    print(i)
    if os.path.splitext(i)[1] == ".jpeg":
        source = path1 + "\\" + str(i.split(".")[0]) + ".jpeg"
        target = path2 + "\\" + str(i.split(".")[0]) + ".jpg"
        jpeg2jpg(source, target)
    elif os.path.splitext(i)[1] == ".png":
        source = path1 + "\\" + str(i.split(".")[0]) + ".png"
        target = path2 + "\\" + str(i.split(".")[0]) + ".jpg"
        png2jpg(source, target)
    elif os.path.splitext(i)[1] == ".jpg":
        source = path1 + "\\" + str(i.split(".")[0]) + ".jpg"
        target = path2 + "\\" + str(i.split(".")[0]) + ".jpg"
        jpg2jpg(source, target)
    elif os.path.splitext(i)[1] == ".png":
        source = path1 + "\\" + str(i.split(".")[0]) + ".png"
        target = path2 + "\\" + str(i.split(".")[0]) + ".jpg"
        png2jpg(source, target)
    elif os.path.splitext(i)[1] == ".jpg":
        source = path1 + "\\" + str(i.split(".")[0]) + ".jpg"
        target = path2 + "\\" + str(i.split(".")[0]) + ".jpg"
        jpg2jpg(source, target)

2.5 查看结果

python图片格式转换脚本

到此这篇关于python图片格式转换脚本的文章就介绍到这了,更多相关python图片格式转换内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

上一篇:

下一篇: