python bmp转jpg

睡在床板下_ 2022-06-23 15:09:39 阅读数:388

PythonCSDNjpgbmptechnology-development

说明

工作中需要把一些bmp图转换为jpg,部分资料是使用opencv,但安装起来不方便,估使用其他方式。

代码

import os.path
from PIL import Image
from io import BytesIO
def scanDir(dirPath: str)->list:
"""扫描目录"""
fileList = []
for top, dirs, nondirs in os.walk(dirPath):
for item in nondirs:
if item.split('.)[-1] == 'bmp':
fileList.append(os.path.join(top, item))
return fileList
def bmp2jpg(filePath):
fileLst = scanDir(filePath)
for oneFile in fileLst:
bmp = Image.open(oneFile)
output_buffer = BytesIO()
bmp.save(output_buffer,format ='JPEG')
byte_data = output_buffer.getvalue()
a = os.path.splitext(oneFile)[0]
tmpUrl = f'{a}.jpg'
try:
with open(tmpUrl, 'wb') as f:
f.write(byte_data)
except Exception as e:
pass
版权声明:本文为[睡在床板下_]所创,转载请带上原文链接,感谢。 https://blog.csdn.net/corrupt/article/details/125045653