Python中实现图片格式转换统一的脚本是什么
Admin 2022-06-01 群英技术资讯 362 次浏览
常见的图像任务通常需要把照片统一成相同的格式,所以此文章正是为了统一格式而生,常见的主要有cv2和PIL.Image的相关操作,照片格式是一串数字加上后缀名
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\\" path2 = 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(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)
pip install pillow之后就可以from PIL import Image
import os from PIL import Image
path1 = r"C:\\Users\\86775\\PycharmProjects\\pythonProject2\\ResNet\\test_change\\" path2 = 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(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)
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
Python内置函数-len()函数。python len() 方法返回对象(字符、列表、元组等)长度或项目个数。
元类一般用于创建类。在执行类定义时,解释器必须要知道这个类的正确的元类。解释器会先寻找类属性__metaclass__,如果此属性存在,就将这个属
torch.cat()函数可以将多个张量拼接成一个张量。torch.cat()有两个参数,第一个是要拼接的张量的列表或是元组;第二个参数是拼接的维度。
django聚合函数的用法是什么?很多新手对于django聚合函数不是很了解,因此这篇文章就主要介绍django聚合函数的用法,有这方面学习需求的朋友可以参考。
flask是python web开发的常用框架之一。本文将讲述flask如何实现修改密码和免密登录功能
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008