程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python BMP to jpg

編輯:Python

explain

We need to put some bmp Turn the picture into jpg, Part of the data is used opencv, But it is inconvenient to install , It is estimated to use other methods .

Code

import os.path
from PIL import Image
from io import BytesIO
def scanDir(dirPath: str)->list:
""" Scan directory """
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

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved