我正在尝试将base64字符串解码为谷歌应用引擎中python中的图像对象。我
imported Image from PIL
from PIL import Image
并尝试像这样解码
image = Image.fromString('RGBA', (iWidth, iHeight), decodeString(imageEncodedString))
我收到错误type object 'Image' has no attribute 'fromString'
那么我该如何解决这个问题: 根据请求将base64图像发送到谷歌应用引擎。在服务器上解码它。做一些转换并将其编码回base64并作为响应返回。
修改
如评论中所述,第一个问题是拼写错误,第二个问题是iWidth和iHeight被解释为字符串。所以我把我的代码改为
image = Image.fromstring('RGB', (int(iWidth), int(iHeight)), decodestring(imageEncodedString))
但现在我收到了错误
ValueError:没有足够的图像数据
这是我的base64编码图像
iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAWJJREFUeNrs3T0OgjAYgGEwnIow4MnUk8lAOnsEbqKSuOrwFVTK86ymJOJry08CVQUAAAAAAABAgerIoLbtru8 + S2k82q3b0QTH9XZdGQ52gQAQAAJAAAgAASAABIAAEAACQAAIAAEgAASAABAApWjsgri27e4Zwy8pjWczAAJAAAgAASAABIAAEABfsZkrgSVcdTMDIAAEgAAQAAJAALgOwOz16L0 + OHyIPp7PDGAJQAAIAAHgLGDxI1t38MwACAABIAAEwK7OAv7R88ykrzLeeFLamckebwbNP / 4pY3xRAVgCHAMgAASAABAAAkAACAABIAAEgAAQAAJAAAgAASAABIAAEAACQAAIAAEgAASAABAAAkAACAABIAAEgAD4Q2s / JWzIGDuttK0pc1trfb9bxrbCY + vIoE9PAU9prP2vLAEIgC1YfAngZ0Iv2DADWAIQAAJAAAgAAAAAAACAkjwEGAC6hS93fcHebgAAAABJRU5ErkJggg ==
答案 0 :(得分:0)
我最终这样做了:
from PIL import Image
from cStringIO import StringIO
from base64 import decodestring
decoded_img_str = base64.decodestring(imageEncodedString)
imgfile = StringIO(decoded_img_str)
image = Image.open(imgfile)