简体   繁体   English

如何在 python 中调整 base64 编码图像的大小

[英]How to resize base64 encoded image in python

I want resize base64 encoded image in python.I searched I could not find.我想在 python 中调整base64 编码图像的大小。我搜索了我找不到。 I used Pillow package to do it.我用枕头package 来做到这一点。 However, Pillow has no such kind of feature.但是,Pillow 没有这种功能。

This code does the job (Python 3):这段代码完成了这项工作(Python 3):

import io
import base64
from PIL import Image

base64_str = 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='

buffer = io.BytesIO()
imgdata = base64.b64decode(base64_str)
img = Image.open(io.BytesIO(imgdata))
new_img = img.resize((2, 2))  # x, y
new_img.save(buffer, format="PNG")
img_b64 = base64.b64encode(buffer.getvalue())
print(str(img_b64)[2:-1])

EDIT: Reducing the size of a base64 image does not imply reducing the file size.编辑:减小 base64 图像的大小并不意味着减小文件大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM