- Python Pillow 教程
- Python Pillow - 首頁
- Python Pillow - 概述
- Python Pillow - 環境搭建
- 基本影像操作
- Python Pillow - 影像處理
- Python Pillow - 調整影像大小
- Python Pillow - 翻轉和旋轉影像
- Python Pillow - 裁剪影像
- Python Pillow - 為影像新增邊框
- Python Pillow - 識別影像檔案
- Python Pillow - 合併影像
- Python Pillow - 剪下和貼上影像
- Python Pillow - 影像滾動
- Python Pillow - 在影像上新增文字
- Python Pillow - ImageDraw 模組
- Python Pillow - 合併兩張影像
- Python Pillow - 建立縮圖
- Python Pillow - 建立水印
- Python Pillow - 影像序列
- Python Pillow 顏色轉換
- Python Pillow - 影像上的顏色
- Python Pillow - 建立彩色影像
- Python Pillow - 將顏色字串轉換為 RGB 顏色值
- Python Pillow - 將顏色字串轉換為灰度值
- Python Pillow - 透過更改畫素值來更改顏色
- 影像處理
- Python Pillow - 降噪
- Python Pillow - 更改影像模式
- Python Pillow - 影像合成
- Python Pillow - 使用 Alpha 通道
- Python Pillow - 應用透視變換
- 影像濾鏡
- Python Pillow - 為影像新增濾鏡
- Python Pillow - 卷積濾鏡
- Python Pillow - 模糊影像
- Python Pillow - 邊緣檢測
- Python Pillow - 浮雕影像
- Python Pillow - 增強邊緣
- Python Pillow - 銳化蒙版濾鏡
- 影像增強和校正
- Python Pillow - 增強對比度
- Python Pillow - 增強銳度
- Python Pillow - 增強顏色
- Python Pillow - 校正色彩平衡
- Python Pillow - 去噪
- 影像分析
- Python Pillow - 提取影像元資料
- Python Pillow - 識別顏色
- 高階主題
- Python Pillow - 建立動畫 GIF
- Python Pillow - 批次處理影像
- Python Pillow - 轉換影像檔案格式
- Python Pillow - 為影像新增填充
- Python Pillow - 顏色反轉
- Python Pillow 與 Numpy 的機器學習
- Python Pillow 與 Tkinter BitmapImage 和 PhotoImage 物件
- Image 模組
- Python Pillow - 影像混合
- Python Pillow 有用資源
- Python Pillow - 快速指南
- Python Pillow - 函式參考
- Python Pillow - 有用資源
- Python Pillow - 討論
Python Pillow - 建立縮圖
縮圖通常用於顯示影像預覽或原始影像的較小表示。它們對於最佳化網頁和提高影像密集型應用程式的載入速度非常有用。Pillow 提供了一種方便的方法來從影像生成縮圖。以下是一些關於 Pillow 中縮圖的關鍵點。
保持縱橫比 - 建立縮圖時,Pillow 會保持原始影像的縱橫比。這意味著縮圖的寬度和高度會根據原始影像成比例地調整,因此影像不會顯得變形。
減小檔案大小 - 與原始影像相比,縮圖的大小更小。這種尺寸減小對於最佳化網頁或在受限空間(例如畫廊或列表中)顯示影像非常有用。
便捷性 - 它簡化了建立縮圖的過程。它在調整影像大小的同時保持縱橫比,並提供了一種簡單的方法將調整大小的影像儲存到檔案中。
質量控制 - 我們可以使用各種引數來控制縮圖的質量,例如大小、調整大小的濾鏡型別以及如果我們以 JPEG 等壓縮格式儲存縮圖的壓縮設定。
在 Pillow 中,我們有一個名為 thumbnail() 的方法,它允許我們為縮圖影像指定尺寸和形狀。我們可以建立兩種不同形狀的縮圖,一種是正方形,另一種是圓形。
建立正方形縮圖
在本節中,我們將瞭解如何使用 Pillow 庫的 thumbnail() 方法建立正方形縮圖。
thumbnail() 方法的語法和引數如下。
image.thumbnail(size, resample=Image.BOX)
其中,
size (必需) - 此引數將縮圖的尺寸(即寬度和高度)指定為一個元組 (width, height)。我們也可以只指定一個維度,另一個維度將自動計算以保持縱橫比。
resample (可選) - 此引數定義在調整影像大小時要使用的重取樣濾鏡。它可以是以下常量之一:
Image.NEAREST (預設) - 最近鄰取樣。
Image.BOX - 箱式取樣,類似於最近鄰,但通常會產生略微平滑的結果。
Image.BILINEAR - 雙線性插值。
Image.HAMMING - 漢明窗 sinc 插值。
Image.BICUBIC - 雙三次插值。
Image.LANCZOS - Lanczos 窗 sinc 插值。
示例
在這個例子中,我們透過將縮圖的寬度和高度引數指定給 resize 引數,使用 thumbnail() 方法建立正方形縮圖。
from PIL import Image
#Open the image
image = Image.open("Images/tutorialspoint.png")
#Define the thumbnail size as a tuple (width, height)
thumbnail_size = (200, 200)
#Create a thumbnail
image.thumbnail(thumbnail_size, resample = Image.BOX )
image.save("output Image/square_thumbnail_image.png")
image.show()
輸出
示例
這是另一個使用 thumbnail() 模組建立寬度為 100,高度為 100 的正方形縮圖的示例。
from PIL import Image
#Open the image
image = Image.open("Images/butterfly.jpg")
#Define the thumbnail size as a tuple (width, height)
thumbnail_size = (100, 100)
#Create a thumbnail
image.thumbnail(thumbnail_size, resample = Image.Resampling.BILINEAR)
image.save("output Image/square_thumbnail_image.png")
image.show()
輸出
建立圓形縮圖
在上一節中,我們瞭解了什麼是縮圖以及如何使用 Pillow 的 thumbnail() 方法建立正方形縮圖。現在,我們將瞭解圓形縮圖的建立。圓形縮圖意味著縮圖將呈圓形。
建立圓形縮圖的 thumbnail() 方法的語法和引數與正方形縮圖相同。
以下是建立圓形縮圖的步驟。
從 Pillow 庫匯入必要的模組 Image 和 ImageDraw。
使用 Image.open() 方法載入原始影像。
使用 size 屬性確定原始影像的尺寸。
使用 ImageDraw.Draw() 方法從蒙版影像建立一個新物件。
使用 draw.ellipse() 方法在蒙版影像上繪製一個橢圓。將影像居中到橢圓的中心。
使用 Image.new() 方法建立一個與原始影像尺寸相同,背景透明的新影像。
使用 save() 方法儲存圓形縮圖影像。
使用 show() 方法顯示建立的圓形縮圖影像。
示例
在這個例子中,我們使用 Pillow 庫的 thumbnail() 方法建立圓形縮圖。
#importing the required libraries
from PIL import Image, ImageDraw
#open image file
img = Image.open('Images/butterfly.jpg')
#resize image
img.thumbnail((2000, 2000))
#create circular mask
mask = Image.new('L', img.size, 0)
draw = ImageDraw.Draw(mask)
draw.ellipse((center[0] - radius, center[1] - radius, center[0] + radius, center[1] + radius), fill = 255)
#apply mask to image
result = Image.new('RGBA', img.size, (255, 255, 255, 0))
result.paste(img, (0, 0), mask)
#save circular thumbnail image
result.save('OutputImages/circular_thumbnail1.png')
#showing the image using show() function
result.show()
使用的影像