
- 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 - 提取影像元資料
影像元資料指的是與數字影像相關聯的資訊。這些元資料可以包含各種詳細資訊,例如相機型號、拍攝日期和時間、位置資訊(GPS 座標)和關鍵詞。
在這種情況下,提取影像元資料是從影像中檢索這些底層資訊。一種廣泛使用的影像元資料型別是 EXIF 資料,它是“可交換影像檔案格式”的縮寫。這種標準化格式由佳能、美能達/索尼和尼康等組織建立,封裝了一套關於影像的綜合規範,包括相機設定、曝光引數等。
需要注意的是,所有 EXIF 資料都是元資料,但並非所有元資料都是 EXIF 資料。EXIF 是一種特定型別的元資料,您正在查詢的資料也可能屬於其他元資料型別,例如 IPTC 或 XMP 資料。
提取基本影像元資料
Python Pillow Image 物件提供了一種簡單的方法來訪問基本影像元資料,包括檔名、尺寸、格式、模式等。
示例
此示例演示瞭如何使用 Pillow Image 物件的屬性從影像中提取各種基本元資料。
from PIL import Image # Path to the image image_path = "Images/dance-cartoon.gif" # Read the image data using Pillow image = Image.open(image_path) # Access and print basic image metadata print("Filename:", image.filename) print("Image Size:", image.size) print("Image Height:", image.height) print("Image Width:", image.width) print("Image Format:", image.format) print("Image Mode:", image.mode) # Check if the image is animated (for GIF images) if hasattr(image, "is_animated"): print("Image is Animated:", image.is_animated) print("Frames in Image:", image.n_frames)
輸出
Filename: Images/dance-cartoon.gif Image Size: (370, 300) Image Height: 300 Image Width: 370 Image Format: GIF Image Mode: P Image is Animated: True Frames in Image: 12
提取高階影像元資料
Python Pillow 庫提供了訪問和管理影像元資料的工具,特別是透過Image.getexif()函式和ExifTags模組。
Image.getexif()函式從影像中檢索 EXIF 資料,其中包含大量關於影像的有價值的資訊。使用此函式的語法如下:
Image.getexif()
該函式返回一個 Exif 物件,此物件提供對 EXIF 影像資料的讀寫訪問。
PIL.ExifTags.TAGS字典用於解釋 EXIF 資料。此字典將 16 位整數 EXIF 標記列舉對映到人類可讀的描述性字串名稱,使元資料更容易理解。此字典的語法如下:
PIL.ExifTags.TAGS: dict
示例
以下示例演示瞭如何使用 Pillow 的getexif()方法訪問和列印影像檔案的 EXIF 元資料。它還展示瞭如何使用 TAGS 字典將標記 ID 對映到人類可讀的標記名稱。
from PIL import Image from PIL.ExifTags import TAGS # The path to the image image_path = "Images/flowers_canon.JPG" # Open the image using the PIL library image = Image.open(image_path) # Extract EXIF data exif_data = image.getexif() # Iterate over all EXIF data fields for tag_id, data in exif_data.items(): # Get the tag name, instead of the tag ID tag_name = TAGS.get(tag_id, tag_id) print(f"{tag_name:25}: {data}")
輸出
GPSInfo : 10628 ResolutionUnit : 2 ExifOffset : 360 Make : Canon Model : Canon EOS 80D YResolution : 72.0 Orientation : 8 DateTime : 2020:10:25 15:39:08 YCbCrPositioning : 2 Copyright : CAMERAMAN_SAI XResolution : 72.0 Artist : CAMERAMAN_SAI
廣告