Python将tiff转换成png
文章目录
- 问题描述
- 解决方案
- 压缩并转换
- 参考文献
问题描述
base64 的 image/tiff 无法在页面直接展示,将其转换为 image/png
解决方案
from io import BytesIOfrom PIL import Imagewith Image.open('a.tiff') as image:bytesIO = BytesIO()image.save(bytesIO, format='PNG', quality=75)img = bytesIO.getvalue()with open('a.png', 'wb') as f: # 写入图片f.write(img)
压缩并转换
from io import BytesIOfrom PIL import Imagewith Image.open('a.tiff') as image:width, height = image.sizetarget_width = 500 # 目标宽度if width > target_width:height = round(target_width * height / width)width = target_widthsize = (width, height)image = image.resize(size)bytesIO = BytesIO()image.save(bytesIO, format='PNG', quality=75)img = bytesIO.getvalue()with open('a.png', 'wb') as f: # 写入图片f.write(img)
参考文献
- Image Module - Pillow Documentation
- Python图片按比例缩放后的宽和高(PIL等比缩放)