python圖片格式轉換腳本

前言

常見的圖像任務通常需要把照片統一成相同的格式,所以此文章正是為瞭統一格式而生,常見的主要有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 查看結果

在這裡插入圖片描述

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圖片格式轉換內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: