如何在 PIL 中開啟 URL 中的影像?


PIL (Python Imaging Library) 是一個廣泛使用的 Python 庫,使開發人員能夠處理影像檔案。它提供了廣泛的功能來操作和處理影像檔案,例如開啟和調整影像大小、在不同格式之間轉換等等。

處理影像時,一個經常遇到的任務是從 URL 開啟影像檔案。當處理儲存在遠端伺服器上的影像(例如從線上資料庫或網站獲取的影像)時,這尤其有用。

在本文中,我們將學習如何在 PIL 中從 URL 開啟影像。我們將看到在 PIL 中從 URL 開啟影像的不同方法,包括以下方法:

  • 使用 urllib 模組

  • 使用 requests 模組

  • 使用 io 模組

  • 使用 Pillow 模組

不同的方法

以下是 PIL 中從 URL 開啟影像的一些常用方法:

方法 1:使用 Urllib 模組

在 PIL 中從 URL 開啟影像的第一種方法是使用 urllib 模組下載影像。

示例

下面的示例演示瞭如何在 PIL 中使用 urllib 模組從 URL 開啟影像。

在給定的示例中,我們首先匯入 urllib.request 模組和 PIL 中的 Image 類。然後,我們指定要開啟的影像的 URL。

我們使用 urllib.request 模組的 urlretrieve() 函式從 URL 下載影像並將其儲存到本地檔案系統,檔名命名為“image.jpg”。此函式接受兩個引數:影像的 URL 和儲存影像的檔名。

然後,我們使用 Image.open() 函式在 PIL 中開啟儲存的影像,並使用 show() 方法顯示影像。

#imports 
import urllib.request
from PIL import Image
my_url = "https://tutorialspoint.tw/images/logo.png"
# Download the image using urllib
urllib.request.urlretrieve(my_url, "logo.png")
# Open the downloaded image in PIL
my_img = Image.open("logo.png")
# Show the image
my_img.show()

輸出

方法 2:使用 Requests 模組

在 PIL 中從 URL 開啟影像的第二種方法是使用 requests 模組下載影像。

示例

下面的示例演示瞭如何在 PIL 中使用 requests 模組從 URL 開啟影像。

在這個示例中,我們首先匯入 requests 模組和 PIL 中的 Image 類。然後,我們指定要開啟的影像的 URL。

我們使用 requests.get() 函式向 URL 傳送 HTTP GET 請求並檢索響應。此函式返回一個包含響應內容的 Response 物件。

然後,我們使用 io 模組中的 BytesIO() 函式建立一個新的記憶體二進位制流,並將響應的內容傳遞給它。然後,我們使用 Image.open() 函式在 PIL 中將二進位制流開啟為影像,並使用 show() 方法顯示影像。

#imports 
import requests
from PIL import Image

my_url = "https://tutorialspoint.tw/images/logo.png"

# Download the image using requests
my_res = requests.get(my_url)

# Open the downloaded image in PIL
my_img = Image.open(BytesIO(my_res.content))

# Show the image
my_img.show()

輸出

方法 3:使用 io 模組

在 PIL 中從 URL 開啟影像的第三種方法是使用 io 模組直接從 URL 讀取影像。以下是使用 io 模組的語法:

示例

下面的示例演示瞭如何在 PIL 中使用 io 模組從 URL 開啟影像。

在這個例子中,為了從 URL 獲取響應,我們使用 urllib.request.urlopen() 函式傳送 HTTP GET 請求。成功執行此函式後,我們得到一個檔案型物件,其中包含響應的內容,我們可以隨時讀取。

使用 with 語句,我們確保在讀取檔案型物件的內容後關閉它。下一步是使用 read() 方法提取響應的內容並將其儲存在一個名為 img_data 的變數中。

使用 Image.open() 函式,在使用 BytesIO() 函式建立一個新的記憶體二進位制流並將 img_data 變數傳遞給它之後,我們顯示影像。此過程在 PIL 中將二進位制流開啟為影像。

#imports 
import urllib.request
from PIL import Image
from io import BytesIO

my_url = "https://tutorialspoint.tw/images/logo.png"

# Read the image from the URL using the io module
with urllib.request.urlopen(my_url) as my_url_res:
   my_img_data = my_url_res.read()

# Open the image in PIL
my_img = Image.open(BytesIO(my_img_data))

# Show the image
my_img.show()

輸出

方法 4:使用 Pillow 模組

在 PIL 中從 URL 開啟影像的第四種也是最後一種方法是使用 Pillow 模組直接從 URL 開啟影像。

示例

下面的示例演示瞭如何在 PIL 中使用 Pillow 模組從 URL 開啟影像。

在這個例子中,為了獲取響應,我們使用 requests.get() 函式向 URL 傳送 HTTP GET 請求。為了防止將響應立即儲存在記憶體中,我們將 stream 引數指定為 True。

使用 requests.get() 返回的 Response 物件,我們提取所需的原始內容。獲得此內容後,我們可以使用 Image.open() 以 PIL 格式訪問圖片。然後,我們可以使用 show() 方法檢視影像。

#imports 
from PIL import Image
import requests

my_url = "https://tutorialspoint.tw/images/logo.png"

# Open the image directly from the URL using Pillow
my_img = Image.open(requests.get(my_url, stream=True).raw)

# Show the image
my_img.show()

輸出

更新於:2023年7月31日

4K+ 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告