您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關如何實現python圖片格式轉換腳本的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
pip install opencv-python之后就可以import cv2
import os import cv2 import sys import numpy as np
path = r"C:\\Users\\86775\\PycharmProjects\\pythonProject2\\ResNet\\test_change\\" path3 = r"C:\\Users\\86775\\PycharmProjects\\pythonProject2\\ResNet\\test_change2\\"
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)
pip install pillow之后就可以from PIL import Image
import os from PIL import Image
path2 = r"C:\\Users\\86775\\PycharmProjects\\pythonProject2\\ResNet\\test_change\\" path3 = r"C:\\Users\\86775\\PycharmProjects\\pythonProject2\\ResNet\\test_change2\\"
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)
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)
感謝各位的閱讀!關于“如何實現python圖片格式轉換腳本”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。