Python转换图片bmp格式到jpg格式

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

import glob
import os
import tensorflow as tf
import shutil

tf.compat.v1.disable_eager_execution() # 兼容v.1的会话模式
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
bmp_files = glob.glob("D:\\Data\\" + "*.bmp")
for data_path in bmp_files:
    # 读取图像的原始图像  这里可能会出现decode‘utf-8’的error 读用rb就搞定
    image_raw_data = tf.io.gfile.GFile(data_path, 'rb', ).read()
    # 对图片进行解码 二进制文件解码为uint8,channels=3表示输出RGB,默认channels=0,
    # 表示采用bmp的通道数(4),会导致后面保存为jpg格式报错
    img_data = tf.image.decode_image(image_raw_data, channels=3)
    with tf.compat.v1.Session() as sess:
    	# 图片按jpeg格式编码
        encode_image = tf.image.encode_jpeg(img_data)
        # 创建文件并写入
        with tf.io.gfile.GFile(data_path.replace("bmp","jpg")) as f:
        	f.write(encode_image.eval())
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值