我正在研究GCP云功能,并打算编写一个将两个映像结合在一起的函数。但是,当我调用函数时,会得到以下错误:
"/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py",跟踪(最近一次调用):文件
第346行,在run_http_function recent = _function_handler.invoke_user_function(flask.request) File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py",第217行中在"/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py",invoke_user_function返回call_user_function(request_or_event)文件第210行、call_user_function返回self._user_function(request_or_event)文件"/user_code/main.py“、第74行、执行newIntro= generateIntroImage(名称映射‘’stdName‘、名称映射’‘stdPicture’、名称映射‘徽标’、名称映射‘’stdName‘)中,"/env/local/lib/python3.7/site-packages/PIL/Image.py",generateIntroImage images.append(徽标)文件第2862行,打开的“无法识别图像文件%r”%(如果文件名为Image.open fp) PIL.UnidentifiedImageError:无法标识图像文件'/tmp/logo.jpg‘
我已经在我的本地机器上运行了这个函数,它正常工作,但是当我在GCP上部署它时,它会给出这个错误和崩溃。这是我的功能:
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
def generateIntroImage(stdName, stdPicture, logo, year, typeFace):
images = [Image.open(x) for x in [stdPicture, logo]]
widths, heights = zip(*(i.size for i in images))
total_width = sum(widths)
max_height = max(heights)
new_im = Image.new('RGB', (total_width, max_height))
x_offset = 0
for im in images:
new_im.paste(im, (x_offset,0))
x_offset += im.size[0]
font= ImageFont.truetype(typeFace, 70)
draw= ImageDraw.Draw(new_im)
draw.text((0, 0), stdName+"'s " +year+" Year Book", (0,0,0),font= font)
fileName= "/tmp/test.jpg"
new_im.save(fileName)
return fileName
这些图像是.jpg和.png文件。知道有什么不对吗?
发布于 2020-06-04 11:20:40
也发生在我的谷歌Colab上,显然更新PIL版本为我解决了问题。
发布于 2021-09-21 11:42:46
PIL抛出错误,因为它无法识别图像格式。最可能的原因是图像损坏了,因此不能被枕头的Image.open()
读取(或“识别”)。例如,如果您尝试在IPython提示符中打开图像,它也会失败。
In [2]: from PIL import Image
In [3]: Image.open("176678612.jpg")
---------------------------------------------------------------------------
UnidentifiedImageError Traceback (most recent call last)
<ipython-input-3-3f91b2f4e49a> in <module>
----> 1 Image.open("176678612.jpg")
/opt/conda/envs/swin/lib/python3.7/site-packages/PIL/Image.py in open(fp, mode, formats)
3022 warnings.warn(message)
3023 raise UnidentifiedImageError(
-> 3024 "cannot identify image file %r" % (filename if filename else fp)
3025 )
3026
UnidentifiedImageError: cannot identify image file '176678612.jpg'
处理此检查的相关代码来自PIL.Image.open()
"""
exception PIL.UnidentifiedImageError: If the image cannot be opened and
identified.
"""
raise UnidentifiedImageError(
"cannot identify image file %r" % (filename if filename else fp)
因此,修复方法是删除图像,或者用未损坏的版本替换它。
发布于 2021-01-25 20:27:32
您需要提供到图像的可下载链接。对我起作用的是点击下载图像和那个URL的副本。
https://stackoverflow.com/questions/60168905
复制相似问题