請問在HW5-2中如何使用cv2讀取url圖檔問題
請問讀取url圖檔,除了使用範例所提供的pillow package外,如何使用opencv 方式讀取 HW5-2 example ```python from PIL import Image from io import BytesIO import numpy as np import matplotlib.pyplot as plt # 請用 df.loc[...] 得到第一筆資料的連結 first_link = response = requests.get(first_link) img = Image.open(BytesIO(response.content)) ``` 如果要使用opencv ```python import cv2 from io import BytesIO import numpy as np import matplotlib.pyplot as plt # 請用 df.loc[...] 得到第一筆資料的連結 first_link = response = requests.get(first_link) img = cv2.imread(BytesIO(response.content)) #display error!!! ```
回答列表
-
2021/06/06 上午 11:57Jaio贊同數:0不贊同數:0留言數:7
可以如下(這張壯麗的圖片的連結是在 google 中搜尋圖片中輸入 " picture" 找到的) : ```python import cv2 import urllib.request as urlo import numpy as np first_link = urlo.urlopen('https://www.planetware.com/wpimages/2020/02/france-in-pictures-beautiful-places-to-photograph-eiffel-tower.jpg') arr = np.asarray(bytearray(first_link.read())) img = cv2.imdecode(arr, -1) cv2.imshow('beautiful', img) if cv2.waitKey() & 0xff == 27: quit() # 按下 esc 即可關閉 ``` 有任何問題歡迎再提出!