gpt4 book ai didi

python-3.x - PIL.UnidentifiedImageError : cannot identify image file

转载 作者:行者123 更新时间:2023-12-04 13:38:48 26 4
gpt4 key购买 nike

我正在研究 GCP 云函数,并打算编写一个结合两个图像的函数。但是,当我调用该函数时,出现以下错误:

Traceback (most recent call last): File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 346, in run_http_function result = _function_handler.invoke_user_function(flask.request) File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 217, in invoke_user_function return call_user_function(request_or_event) File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 210, in call_user_function return self._user_function(request_or_event) File "/user_code/main.py", line 74, in execute newIntro= generateIntroImage(nameMappings['stdName'], nameMappings['stdPicture'], nameMappings['logo'], nameMappings['stdYear'], nameMappings['font']) File "/user_code/main.py", line 12, in generateIntroImage images.append(Image.open(logo)) File "/env/local/lib/python3.7/site-packages/PIL/Image.py", line 2862, in open "cannot identify image file %r" % (filename if filename else fp) PIL.UnidentifiedImageError: cannot identify image file '/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 文件。知道有什么问题吗?

最佳答案

在 Google Colab 上也发生在我身上,显然更新 PIL 版本为我解决了这个问题。

关于python-3.x - PIL.UnidentifiedImageError : cannot identify image file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60168905/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com