我目前遇到的問題是試圖下載顯示爲動畫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