Python 批量图片格式转换

今天自己尝试着使用 Pthon 把图片转换想要的图片格式。下面分享代码给大家。

import os
from PIL import Image
import shutil
import sys

#原图片存放的地址
source_path="D:/robotInfo/test/picture/"
#转换后的图片格式
types='jpg' 
#最终图片存放的地址
target_path="D:/robotInfo/test/picture/tranTo/"

if not os.path.exists(target_path):
    os.makedirs(target_path)
    
#转换函数
def changeImage(source_path,types):
    files = []
    image_list=os.listdir(source_path)
    files = [os.path.join(source_path,_) for _ in image_list]
    for file in files:
        try:
            sys.stdout.flush()
            im = Image.open(file)
            image = os.path.splitext(file)[0] + "." + types
            im.save(image)
            shutil.move(image,target_path)
        except IOError as e:
            print('could not read:',file)
            print('error:',e)
            print('skip it\n')
    sys.stdout.write('Convert Over!\n')
    sys.stdout.flush()

#执行函数
changeImage(source_path,types)

如果这边文章对你有用的话,劳烦你给个赞!!!

  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值