简体   繁体   English

将base64图像转换为base64 JPG

[英]Converting base64 images to base64 JPG

I have a feature with my application where a user can upload a photo. 我的应用程序具有一项功能,用户可以上传照片。 I would like to convert the photo into a JPG file and then upload it to my servers. 我想将照片转换为JPG文件,然后将其上传到我的服务器。

The photo is received in base64. 照片在base64中接收。 I've seen other answers which suggest using PIL however, it saves the image to a local directory. 我看到了其他建议使用PIL的答案 ,但是它将图像保存到本地目录中。 Instead I would like to convert the received image into a base64 JPG image. 相反,我想将接收到的图像转换为base64 JPG图像。

How would I do this? 我该怎么做? Thanks. 谢谢。

try this: 尝试这个:

import base64
from PIL import Image
from io import BytesIO

im = Image.open(BytesIO(base64.b64decode(data)))
output = BytesIO()
im.save(output, 'JPEG')
jpg_img = output.read()

it will save the data to a stream ( the same way tou enter it) and then you can read from that stream 它将数据保存到流中(以相同的方式输入),然后您可以从该流中读取

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

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