如何用枕頭在Python中執行基本圖像處理操作?
Pillow 是一個強大且易於使用的Python 圖像處理庫,1. 首先通過pip install pillow 安裝並導入庫;2. 使用Image.open() 打開圖像,save() 保存圖像,格式由文件擴展名自動識別;3. resize() 可調整圖像大小但不保持寬高比,thumbnail() 在保持寬高比的同時創建縮略圖;4. crop() 接受(left, upper, right, lower) 元組進行裁剪;5. rotate() 按角度逆時針旋轉圖像,expand=True 可保留完整圖像,transpose() 可實現水平或垂直翻轉;6. convert('L') 轉為灰度圖,convert('1') 轉為1位黑白圖;7. 使用ImageFilter.BLUR 等應用模糊、銳化、邊緣檢測等濾鏡;8. ImageEnhance.Brightness、Contrast、Color 分別用於調整亮度、對比度和飽和度;9. 可通過size、mode、format 等屬性獲取圖像信息;10. paste() 可將一個圖像疊加到另一個圖像上,支持透明通道。 Pillow 使得圖像處理操作簡潔高效,適合自動化縮略圖生成、格式轉換和機器學習預處理等任務。
Pillow (PIL Fork) is a powerful and easy-to-use library for image processing in Python. It allows you to perform a wide range of basic operations like opening, saving, resizing, cropping, rotating, filtering, and color manipulation. Here's how to handle common image processing tasks using Pillow.

1. Install and Import Pillow
First, install Pillow if you haven't already:
pip install pillow
Then import it in your script:

from PIL import Image, ImageFilter, ImageEnhance
2. Open and Save Images
To load an image from a file:
img = Image.open('input.jpg')
To save it in a different format or location:

img.save('output.png')
Pillow automatically detects the format based on the file extension.
3. Basic Image Operations
Resize an Image
Use resize()
to change dimensions. Pass a tuple (width, height):
resized_img = img.resize((800, 600)) resized_img.save('resized.jpg')
⚠️ Note:
resize()
doesn't maintain aspect ratio by default. To preserve it, calculate dimensions manually or usethumbnail()
.
Create a Thumbnail (Preserves Aspect Ratio)
img_copy = img.copy() # Always work on a copy img_copy.thumbnail((800, 600)) # Modifies in place, respects aspect ratio img_copy.save('thumbnail.jpg')
Crop an Image
Specify a bounding box as (left, upper, right, lower):
cropped_img = img.crop((100, 100, 400, 400)) # Crops a 300x300 region cropped_img.save('cropped.jpg')
Rotate an Image
Rotate by a given angle (counterclockwise):
rotated_img = img.rotate(45, expand=True) # expand=True keeps the whole image rotated_img.save('rotated.jpg')
You can also flip or mirror:
flipped_img = img.transpose(Image.FLIP_LEFT_RIGHT) # Horizontal flip # flipped_img = img.transpose(Image.FLIP_TOP_BOTTOM) # Vertical flip flipped_img.save('flipped.jpg')
4. Color and Mode Manipulation
Convert between color modes (eg, RGB, grayscale, black & white):
gray_img = img.convert('L') # Grayscale gray_img.save('grayscale.jpg') bw_img = img.convert('1') # 1-bit black and white (dithered) bw_img.save('black_white.jpg')
5. Apply Filters and Enhancements
Apply Built-in Filters
Use ImageFilter
module:
# Blur blurred_img = img.filter(ImageFilter.BLUR) # Sharpen sharpened_img = img.filter(ImageFilter.SHARPEN) # Edge enhancement edges_img = img.filter(ImageFilter.FIND_EDGES) blurred_img.save('blurred.jpg')
Adjust Brightness, Contrast, Saturation
Use ImageEnhance
classes:
enhancer = ImageEnhance.Brightness(img) bright_img = enhancer.enhance(1.5) # Increase brightness by 50% bright_img.save('bright.jpg') # Similarly for contrast enhancer = ImageEnhance.Contrast(img) contrast_img = enhancer.enhance(2.0) # Double contrast contrast_img.save('high_contrast.jpg') # For color saturation enhancer = ImageEnhance.Color(img) color_img = enhancer.enhance(1.5) # Boost color color_img.save('color_enhanced.jpg')
6. Get Image Information
You can inspect basic image properties:
print("Size:", img.size) # (width, height) print("Mode:", img.mode) # eg, RGB, L print("Format:", img.format) # eg, JPEG, PNG print("Width:", img.width) print("Height:", img.height)
7. Combine Images (Optional)
Paste one image onto another:
base_img = Image.open('background.jpg') overlay = Image.open('logo.png').resize((100, 100)) # Paste overlay at position (50, 50) base_img.paste(overlay, (50, 50), overlay) # Third arg for alpha mask base_img.save('combined.png')
Summary
Pillow makes basic image processing simple and intuitive. Key points:
- Use
Image.open()
and.save()
for loading and saving. -
.resize()
,.crop()
,.rotate()
,.transpose()
for geometry changes. -
.convert()
for color mode changes. -
ImageFilter
andImageEnhance
for visual effects. - Always work on copies to avoid modifying the original.
With these tools, you can automate common image tasks like thumbnails, format conversion, and preprocessing for machine learning.
Basically just a few lines for most operations — but powerful when chained together.
以上是如何用枕頭在Python中執行基本圖像處理操作?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undress AI Tool
免費脫衣圖片

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Wasserstein距離,又稱EarthMover'sDistance(EMD),是一種用於測量兩個機率分佈之間差異的測量方法。相較於傳統的KL散度或JS散度,Wasserstein距離考慮了分佈之間的結構訊息,因此在許多影像處理任務中展現出更好的性能。透過計算兩個分佈之間的最小運輸成本,Wasserstein距離能夠測量將一個分佈轉換為另一個分佈所需的最小工作量。這種度量方法能夠捕捉到分佈之間的幾何差異,從而在影像生成、風格遷移等任務中發揮重要作用。因此,Wasserstein距離成為了概

C#開發中如何處理影像處理和圖形介面設計問題,需要具體程式碼範例引言:在現代軟體開發中,影像處理和圖形介面設計是常見的需求。而C#作為一種通用的高階程式語言,具有強大的影像處理和圖形介面設計能力。本文將以C#為基礎,討論如何處理影像處理和圖形介面設計問題,並給出詳細的程式碼範例。一、影像處理問題:影像讀取和顯示:在C#中,影像的讀取和顯示是基本操作。可以使用.N

VisionTransformer(VIT)是Google提出的一種基於Transformer的圖片分類模型。不同於傳統CNN模型,VIT將圖像表示為序列,並透過預測圖像的類別標籤來學習圖像結構。為了實現這一點,VIT將輸入影像劃分為多個補丁,並將每個補丁中的像素透過通道連接,然後進行線性投影以達到所需的輸入維度。最後,每個補丁被展平為單一向量,從而形成輸入序列。透過Transformer的自註意力機制,VIT能夠捕捉到不同補丁之間的關係,並進行有效的特徵提取和分類預測。這種序列化的影像表示方法為

超解析度影像重建是利用深度學習技術,如卷積神經網路(CNN)和生成對抗網路(GAN),從低解析度影像中生成高解析度影像的過程。該方法的目標是透過將低解析度影像轉換為高解析度影像,從而提高影像的品質和細節。這種技術在許多領域都有廣泛的應用,如醫學影像、監視攝影、衛星影像等。透過超解析度影像重建,我們可以獲得更清晰、更具細節的影像,有助於更準確地分析和識別影像中的目標和特徵。重建方法超解析度影像重建的方法通常可以分為兩類:基於插值的方法和基於深度學習的方法。 1)基於插值的方法基於插值的超解析度影像重

尺度不變特徵變換(SIFT)演算法是一種用於影像處理和電腦視覺領域的特徵提取演算法。該演算法於1999年提出,旨在提高電腦視覺系統中的物體辨識和匹配性能。 SIFT演算法具有穩健性和準確性,被廣泛應用於影像辨識、三維重建、目標偵測、視訊追蹤等領域。它透過在多個尺度空間中檢測關鍵點,並提取關鍵點周圍的局部特徵描述符來實現尺度不變性。 SIFT演算法的主要步驟包括尺度空間的建構、關鍵點偵測、關鍵點定位、方向分配和特徵描述子產生。透過這些步驟,SIFT演算法能夠提取出具有穩健性和獨特性的特徵,從而實現對影像的高效

舊照片修復是利用人工智慧技術對舊照片進行修復、增強和改善的方法。透過電腦視覺和機器學習演算法,該技術能夠自動識別並修復舊照片中的損壞和缺陷,使其看起來更加清晰、自然和真實。舊照片修復的技術原理主要包括以下幾個面向:1.影像去雜訊和增強修復舊照片時,需要先進行去雜訊和增強處理。可以使用影像處理演算法和濾波器,如均值濾波、高斯濾波、雙邊濾波等,來解決雜訊和色斑問題,進而提升照片的品質。 2.影像復原和修復在舊照片中,可能存在一些缺陷和損壞,例如刮痕、裂縫、褪色等。這些問題可以透過影像復原和修復演算法來解決

卷積神經網路在影像去噪任務中表現出色。它利用學習到的濾波器對雜訊進行過濾,從而恢復原始影像。本文詳細介紹了基於卷積神經網路的影像去噪方法。一、卷積神經網路概述卷積神經網路是一種深度學習演算法,透過多個卷積層、池化層和全連接層的組合來進行影像特徵學習和分類。在卷積層中,透過卷積操作提取影像的局部特徵,從而捕捉影像中的空間相關性。池化層則透過降低特徵維度來減少計算量,並保留主要特徵。全連接層負責將學習到的特徵與標籤進行映射,以實現影像的分類或其他任務。這種網路結構的設計使得卷積神經網路在影像處理與識

Pillow庫是Python中一個非常強大的影像處理庫,它是基於PythonImagingLibrary(PIL)發展而來,並在其基礎上進行了最佳化和擴展。 Pillow庫提供了豐富的影像處理功能,可以處理各種類型的影像文件,並進行影像的編輯、合併、濾鏡處理等操作。本文將為大家提供一個Pillow庫的安裝指南,幫助你輕鬆掌握這個強大的影像處理工具。一、安裝P
