python jpg2png

python jpg2png,第1张

import os
from PIL import Image
import cv2
import tensorflow as tf
import os

#r'去除转义字符
path = r'/待转后缀名图片路径/'
output_path = r'/转换后缀名成功后输出路径/'
os.makedirs(output_path, exist_ok=True)

#os.listdir() 方法用于返回指定的文件夹包含的文件或文件夹的名字的列表
files = os.listdir(path)
#print(files)

#strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。
for sample_file in files:
    #print("sample_file:",sample_file)
    file_name = sample_file.strip('.jpg')
    #print("file_name:",file_name)
    image = cv2.imread(path + sample_file)
    #base_name = os.path.splitext(file_name)[0]
    #print("base_name:",file_name)
    cv2.imwrite(output_path + file_name + '.png', image)
    #print(file_name)
print("done!")

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/797459.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-06
下一篇 2022-05-06

发表评论

登录后才能评论

评论列表(0条)

保存