如何将PIL Image.image对象转换为base64字符串?

我试图以90度的角度旋转base64编码的图像。完成此操作后,我想将其转换回base64字符串。但不幸的是无法实现这一目标。

到目前为止,这是我所做的:

image_string = StringIO(base64.b64decode(base64_string_here))

image = Image.open(image_string)

angle = 90

rotated_image = image.rotate( angle, expand=1 )

Kindy帮我如何将Rotated_image转换为base64字符串。

dir()是rotated_image的:

[‘Image__transformer’,’__ doc __’,’__ getattr __’,’__ init __’,’__ module

repr __’,’ copy’,’ dump’,’ expand’,’ makeself’,’

new’,’category’,’

convert’,’copy’,’crop’,’draft’,’filter’,’format’,’format_description’,’fromstring’,’getbands’,’getbbox’,’getcolors’,’getdata’,’getextrema’

,“ getim”,“ getpalette”,“ getpixel”,“ getprojection”,“ histogram”,“ im”,“

info”,“ load”,“ mode”,“ offset”,“ palette”,“ paste”,“点”,“ putalpha”,“

putdata”,“ putpalette”,“

putpixel”,“量化”,’readonly’,’resize’,’rotate’,’save’,’seek’,’show’,’size’,’split’,’tell’,’thumbnail’,’tobitmap’,’tostring’,’transform’

‘,’转置’,’验证’]

回答:

import base64

from io import BytesIO

buffered = BytesIO()

image.save(buffered, format="JPEG")

img_str = base64.b64encode(buffered.getvalue())

import base64

import cStringIO

buffer = cStringIO.StringIO()

image.save(buffer, format="JPEG")

img_str = base64.b64encode(buffer.getvalue())

以上是 如何将PIL Image.image对象转换为base64字符串? 的全部内容, 来源链接: utcz.com/qa/422885.html

回到顶部