Image.seek 没有 return 图片对象
Image.seek doesn't return Image object
我正在尝试制作一个从 URL 获取图像的程序。如果图像是动画的,它 return 是 gif 的第一帧,如果不是动画的,它只是一张图像。
代码:
def is_animated(im):
try:
im.seek(1)
return True
except EOFError:
return False
def getImg(url):
img = Image.open(requests.get(url, stream=True).raw, mode='r')
if is_animated(img):
return img
else:
print('no animation detected')
return img
问题:我用动画图片试过了:return在is_animated()
中为真,但return return img
中的图像对象。错误日志是空白的。这可能是什么问题?如果有任何帮助,我将不胜感激,在此先感谢您。
seek
return Pillow
中没有任何内容。它只是寻找下一帧,并将 img
对象指向该帧,因此如果您输出 img
,它将显示下一帧。
此外,这意味着在 is_animated
中 运行 宁 im.seek(1)
,你会自动寻找下一帧,这意味着你永远不会 return来自 getImg
函数的序列中的第一张图像。您必须 运行 im.seek(0)
才能 return 第一帧。
来源:the source code(请注意 return 在 seek
或 _seek
中没有任何内容)
我正在尝试制作一个从 URL 获取图像的程序。如果图像是动画的,它 return 是 gif 的第一帧,如果不是动画的,它只是一张图像。
代码:
def is_animated(im):
try:
im.seek(1)
return True
except EOFError:
return False
def getImg(url):
img = Image.open(requests.get(url, stream=True).raw, mode='r')
if is_animated(img):
return img
else:
print('no animation detected')
return img
问题:我用动画图片试过了:return在is_animated()
中为真,但return return img
中的图像对象。错误日志是空白的。这可能是什么问题?如果有任何帮助,我将不胜感激,在此先感谢您。
seek
return Pillow
中没有任何内容。它只是寻找下一帧,并将 img
对象指向该帧,因此如果您输出 img
,它将显示下一帧。
此外,这意味着在 is_animated
中 运行 宁 im.seek(1)
,你会自动寻找下一帧,这意味着你永远不会 return来自 getImg
函数的序列中的第一张图像。您必须 运行 im.seek(0)
才能 return 第一帧。
来源:the source code(请注意 return 在 seek
或 _seek
中没有任何内容)