这篇文章主要介绍“Python怎么将bmp格式的图片批量转成jpg”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Python怎么将bmp格式的图片批量转成jpg”文章能帮助大家解决问题。
# *_* coding : UTF-8 *_*
# 开发人员: csu·pan-_-||
# 开发时间: 2020/11/21 12:40
# 文件名称: bmp_to_jpg.py
# 开发工具: PyCharm
# 功能描述: 将bmp格式的图片批量转成jpg
import os
import cv2
# 图片的路径
bmp_dir = r'E:\Projects\bmp'
jpg_dir = r'E:\Projects\jpg'
filelists = os.listdir(bmp_dir)
for i,file in enumerate(filelists):
# 读图,-1为不改变图片格式,0为灰度图
img = cv2.imread(os.path.join(bmp_dir,file),-1)
newName = file.replace('.bmp','.jpg')
cv2.imwrite(os.path.join(jpg_dir,newName),img)
print('第%d张图:%s'%(i+1,newName))
import os
from PIL import Image
json_dir = r"D:\BMP2PNG"
label_names = os.listdir(json_dir)
label_dir = []
for filename in label_names:
label_dir.append(os.path.join(json_dir,filename))
for i,filename in enumerate(label_dir):
im = Image.open(filename) # open ppm file
newname = label_names[i].split('.')[0] + '.png' # new name for png file
im.save(os.path.join(json_dir,newname))
import os
from PIL import Image
json_dir = r"D:\BMP2JPG"
label_names = os.listdir(json_dir)
label_dir = []
for filename in label_names:
label_dir.append(os.path.join(json_dir,filename))
for i,filename in enumerate(label_dir):
im = Image.open(filename) # open ppm file
newname = label_names[i].split('.')[0] + '.jpg' # new name for png file
im.save(os.path.join(json_dir,newname))
import os
from PIL import Image
import tqdm
def bmp2png(file_dir):
for root, dirs, files in os.walk(file_dir): # 获取所有文件
# for file in files: # 遍历所有文件名
for idx,file in enumerate(tqdm.tqdm(files)):
if os.path.splitext(file)[1] == '.bmp': # 指定尾缀 ***重要***
im = Image.open(os.path.join(root, file)) # open img file
newname = file.split('.')[0] + '.png' # new name for png file
im.save(os.path.join(root, newname)) # 转为png
bmp2png(r"D:\数据集\20221105-18")
import os
from PIL import Image
import tqdm
def bmp2png(file_dir):
for root, dirs, files in os.walk(file_dir): # 获取所有文件
# for file in files: # 遍历所有文件名
for idx,file in enumerate(tqdm.tqdm(files)):
if os.path.splitext(file)[1] == '.bmp': # 指定尾缀 ***重要***
newname = file.split('.')[0] + '.png' # new name for png file
if(os.path.exists(os.path.join(root, newname))):
os.remove(os.path.join(root, newname))
os.rename(os.path.join(root, file),os.path.join(root, newname))
print(os.path.join(root, file))
bmp2png(r"D:\PUCP数据集\20221105-18")
关于“Python怎么将bmp格式的图片批量转成jpg”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://blog.csdn.net/qq_36563273/article/details/110000276