用python将图片的 bmp 格式转换为 jpg 格式

from PIL import Image
import glob
import os

source_folder = '/path/to/source/folder/'  # 源文件夹路径
destination_folder = '/path/to/destination/folder/'  # 目标文件夹路径
ext = "bmp"
new = "jpg"

# 检查文件扩展名是否包含点号
if '.' not in ext.strip():
   ext = '.' + ext.strip()
if '.' not in new.strip():
   new = '.' + new.strip()

# 获取源文件夹中指定扩展名的文件列表
files = glob.glob(os.path.join(source_folder, '*' + ext))

# 计数器,用于记录成功转换的图像数量
success_count = 0

# 遍历文件列表,将图像保存到目标文件夹中
for f in files:
   im = Image.open(f)
   filename = os.path.basename(f)  # 提取文件名
   destination_path = os.path.join(destination_folder, filename.replace(ext, new))
   im.save(destination_path)
   success_count += 1
   print(f"图片{filename}转换成功!")

print(f"成功转换{success_count}张图像!")

注:

(1)运行时将 source_folder 和 destination_folder 替换为自己的文件夹路径;

(2)ext  new 变量表示源文件的扩展名和目标文件的扩展名。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值