Create image.py
Browse files
image.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from io import BytesIO
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
|
| 6 |
+
def image(url):
|
| 7 |
+
response = requests.get(url) # 下載圖片
|
| 8 |
+
|
| 9 |
+
if response.status_code == 200: # 檢查是否成功下載
|
| 10 |
+
img = Image.open(BytesIO(response.content)) # 讀取圖片內容
|
| 11 |
+
|
| 12 |
+
plt.imshow(img) # 使用 matplotlib 顯示圖片
|
| 13 |
+
plt.axis('off') # 不顯示坐標軸
|
| 14 |
+
plt.show()
|
| 15 |
+
|
| 16 |
+
img.save('downloaded_image.jpg') # 保存圖片
|
| 17 |
+
else:
|
| 18 |
+
print('Failed to download image')
|