
- 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 - 討論
Pillow - 調整影像大小
在 Pillow 庫中調整影像大小包括更改影像的尺寸,即寬度和高度。此操作可用於使影像更大或更小,並且可以用於各種目的,例如準備用於網站上顯示的影像、減小檔案大小或生成縮圖。
使用 resize() 方法調整影像大小
在 Pillow 中,resize() 方法用於更改影像的尺寸。此函式允許我們以以下方式調整影像大小。
絕對尺寸 - 我們可以指定影像應調整到的新寬度和高度(以畫素為單位)。
保持縱橫比 - 如果我們只指定一個維度(寬度或高度),則 Pillow 可以自動計算另一個維度以保持影像的縱橫比。
縮放 - 我們可以透過縮放因子調整影像大小,該縮放因子在保持縱橫比的同時統一調整寬度和高度。
以下是 resize() 方法的基本語法 -
PIL.Image.resize(size, resample=3)
其中,
size - 這可以是指定新寬度和高度(以畫素為單位)的元組,即指定新大小(寬度或高度)的單個整數,或指定縮放因子的浮點數。
resample(可選) - 預設值為 3,對應於抗鋸齒高質量濾鏡。我們可以從各種重取樣濾鏡中選擇,例如 Image.NEAREST、Image.BOX、Image.BILINEAR、Image.HAMMING、Image.BICUBIC、Image.LANCZOS 等。
以下是本章所有示例中使用的輸入影像。

示例
在此示例中,我們使用 resize() 函式透過傳遞元組作為輸入引數來調整影像的寬度和高度。
from PIL import Image #Open an image image = Image.open("Images/rose.jpg") #Resize to specific dimensions (e.g., 300x200 pixels) new_size = (300, 200) resized_image = image.resize(new_size) #Display resized image resized_image.show()
輸出

示例
在此示例中,我們透過保持原始輸入影像的相同縱橫比來調整影像大小。
from PIL import Image #Open an image image = Image.open("Images/rose.jpg") #Resize by maintaining aspect ratio (e.g., specify the width) new_width = 200 aspect_ratio_preserved = image.resize((new_width, int(image.height * (new_width / image.width)))) aspect_ratio_preserved.show()
輸出

示例
在此示例中,我們透過縮放因子調整影像大小。
from PIL import Image #Open an image image = Image.open("Images/rose.jpg") #Scale the image by a factor (e.g., 10% of the original size) scaling_factor = 0.1 scaled_image = image.resize((int(image.width * scaling_factor), int(image.height * scaling_factor))) scaled_image.show()
輸出

廣告