我目前遇到的问题是试图下载显示为动画gif
的图像,但显示编码为jpg
。我说它似乎编码为jpg
,因为文件扩展名和mime类型都是.jpg添加图像/ jpeg。Python3下载网址错误编码的图像
在下载文件到我的本地机(Mac OSX版),然后试图打开我得到了错误的文件:
The file could not be opened. It may be damaged or use a file format that Preview doesn’t recognize.
虽然我知道有些人会也许只是忽略图像,如果可以修复,我正在寻找一个解决方案来做到这一点,而不是忽略它。
相关网址是在这里:
http://www.supergrove.com/wp-content/uploads/2017/03/gif-images-22-1000-about-gif-on-pinterest.jpg
这里是我的代码,我愿意接受建议:
from PIL import Image
import requests
response = requests.get(media, stream = True)
response.raise_for_status()
with open(uploadedFile, 'wb') as img:
for chunk in response.iter_content(chunk_size=1024):
if chunk:
img.write(chunk)
img.close()
如果你去找到并更改文件扩展名怎么办? –
您是否尝试通过右键单击下载 - >将图像另存为,并查看它是否打开?在我的情况下(Debian 8),firefox正确打开它。 – raratiru
@ whackamadoodle3000没有区别。那是我尝试的第一件事情之一。还尝试更改文件扩展名以将文件保存到磁盘之前。 – stwhite