Image.open(BytesIO(response.content))
day 3 homework 2Image.open(BytesIO(response.content))這行 放到 function
def img2arr_fromURLs(url_list, resize = False):
img_list = list()
img = np.empty([2,2])
for url in url_list:
print(url)
response = requests.get(url)
img = Image.open(BytesIO(response.content))
type(img)
img_list.append(img)
print(len(img_list))
回答列表
-
2019/05/02 下午 08:01Yueh-Ying Song贊同數:0不贊同數:0留言數:0
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
<ipython-input-47-ab26664158f4> in <module>
----> 1 result = img2arr_fromURLs(df[0:5][1].values)
2 print("Total images that we got: %i " % len(result)) # 如果不等於 5, 代表有些連結失效囉
3
4 for im_get in result:
5 plt.imshow(im_get)
<ipython-input-46-030945f556ee> in img2arr_fromURLs(url_list, resize)
5 print(url)
6 response = requests.get(url)
----> 7 img = Image.open(BytesIO(response.content))
8 type(img)
9 img_list.append(img)
~\AppData\Local\Continuum\anaconda3\lib\site-packages\PIL\Image.py in open(fp, mode)
2703 warnings.warn(message)
2704 raise IOError("cannot identify image file %r"
-> 2705 % (filename if filename else fp))
2706
2707 #
OSError: cannot identify image file <_io.BytesIO object at 0x000000000B870620>跳出這個錯誤請問怎麼解決? 這邊介面為什麼跟stackoverflow差這麼多....
-
2019/05/02 下午 08:29Seanyu.TW贊同數:1不贊同數:0留言數:1
Hi 你得到的 Error 可能是因為有些連結是失效的,於是 request 回來的東西是無效的。這個題目的目的是希望大家可以加上 try-except 去避開這種失效連結。
-
2019/05/02 下午 08:29Seanyu.TW贊同數:2不贊同數:0留言數:0
另外介面的問題,我們也覺得有點不是很流暢,會幫你在反映給主辦方 :)