跳過到頁腳內容
PYTHON 幫助

scikit-image Python(開發人員指南的工作原理)

Scikit-image 是一套專為 Python 圖像處理設計的算法集合。 它是自由可用且無限制的,擁有來自活躍的志願者社群的高品質、經過同行評審的代碼。 Scikit-image 項目始於 2009 年的 Google,為 Google Summer Code 計劃的一部分,由 Stefan van der Walt 和其他 Scikit-image 貢獻者指導。 其目的是創建一個 Python 圖像處理庫,易於使用,高效且可擴展,適用於學術和工業應用。 In this article, we will learn about the Scikit-image Python imaging library and a PDF generation library from IronSoftware called IronPDF.

開始使用

想要了解 Scikit-image,請查看官方網站。此外,Data Carpentry 提供了一個關於使用 Scikit 進行 Python 圖像處理的精彩課程。

通過 pip 安裝

  • 確保您已安裝 Python(至少 3.10 版本)。
  • 打開您的終端或命令提示符。

    • 更新 pip:
    python -m pip install -U pip
    python -m pip install -U pip
    SHELL
    • 通過 pip 安裝 scikit-image:
    python -m pip install -U scikit-image
    python -m pip install -U scikit-image
    SHELL
    • 要訪問演示數據集,使用:
    python -m pip install -U scikit-image[data]
    python -m pip install -U scikit-image[data]
    SHELL
    • 對於額外的科學包,包括並行處理功能:
    python -m pip install -U scikit-image[optional]
    python -m pip install -U scikit-image[optional]
    SHELL

基本範例

import skimage.io
import matplotlib.pyplot as plt

# Load an image from file
image = skimage.io.imread(fname='land.jpg')

# Display the image
plt.imshow(image)
plt.show()
import skimage.io
import matplotlib.pyplot as plt

# Load an image from file
image = skimage.io.imread(fname='land.jpg')

# Display the image
plt.imshow(image)
plt.show()
PYTHON

濾鏡

import skimage as ski

# Load a sample image from the scikit-image default collection
image = ski.data.coins()

# Apply a Sobel filter to detect edges
edges = ski.filters.sobel(image)

# Display the edges
ski.io.imshow(edges)
ski.io.show()
import skimage as ski

# Load a sample image from the scikit-image default collection
image = ski.data.coins()

# Apply a Sobel filter to detect edges
edges = ski.filters.sobel(image)

# Display the edges
ski.io.imshow(edges)
ski.io.show()
PYTHON

Scikit-image,通常縮寫為 skimage,是一個功能強大的 Python 圖像處理庫。 它基於 NumPy 陣列、SciPy 和 matplotlib,並提供多種功能和算法來處理和分析圖像。 skimage.data.coins() 用於從庫中獲取樣本圖像。 skimage.filters 提供訪問內建濾鏡和工具函數。

Scikit-image 的主要特徵

1. 圖像過濾與邊緣檢測

from skimage import io, filters

# Load an image
image = io.imread('image.jpg')

# Apply Gaussian blur
blurred_image = filters.gaussian(image, sigma=1.0)

# Apply Sobel edge detection
edges = filters.sobel(image)

# Display the original image, blurred image, and edges
io.imshow_collection([image, blurred_image, edges])
io.show()
from skimage import io, filters

# Load an image
image = io.imread('image.jpg')

# Apply Gaussian blur
blurred_image = filters.gaussian(image, sigma=1.0)

# Apply Sobel edge detection
edges = filters.sobel(image)

# Display the original image, blurred image, and edges
io.imshow_collection([image, blurred_image, edges])
io.show()
PYTHON

輸出

scikit-image Python (運作方式:開發者指南):圖 1 - 圖像過濾與邊緣檢測輸出

2. 使用 HOG(方向梯度直方圖)的特徵提取

from skimage import io, color, feature

# Load an example image and convert to grayscale
image = io.imread('image.jpg')
gray_image = color.rgb2gray(image)

# Compute HOG features and visualize them
hog_features, hog_image = feature.hog(gray_image, visualize=True)

# Display the original image and the HOG image
io.imshow_collection([image, gray_image, hog_image])
io.show()
from skimage import io, color, feature

# Load an example image and convert to grayscale
image = io.imread('image.jpg')
gray_image = color.rgb2gray(image)

# Compute HOG features and visualize them
hog_features, hog_image = feature.hog(gray_image, visualize=True)

# Display the original image and the HOG image
io.imshow_collection([image, gray_image, hog_image])
io.show()
PYTHON

輸出

scikit-image Python (運作方式:開發者指南):圖 2 - 特徵提取輸出

3. 幾何轉換 - 調整大小與旋轉

from skimage import io, transform

# Load an image
image = io.imread('image.jpg')

# Resize image by dividing its dimensions by 2
resized_image = transform.resize(image, (image.shape[0] // 2, image.shape[1] // 2))

# Rotate image by 45 degrees
rotated_image = transform.rotate(image, angle=45)

# Display the original image, resized image, and rotated image
io.imshow_collection([image, resized_image, rotated_image])
io.show()
from skimage import io, transform

# Load an image
image = io.imread('image.jpg')

# Resize image by dividing its dimensions by 2
resized_image = transform.resize(image, (image.shape[0] // 2, image.shape[1] // 2))

# Rotate image by 45 degrees
rotated_image = transform.rotate(image, angle=45)

# Display the original image, resized image, and rotated image
io.imshow_collection([image, resized_image, rotated_image])
io.show()
PYTHON

輸出

scikit-image Python (運作方式:開發者指南):圖 3 - 幾何轉換輸出

4. 使用總變異濾鏡進行圖像去噪

from skimage import io, restoration

# Load a noisy image
image = io.imread('image.jpg')

# Apply total variation denoising
denoised_image = restoration.denoise_tv_chambolle(image, weight=0.1)

# Display the noisy image and the denoised image
io.imshow_collection([image, denoised_image])
io.show()
from skimage import io, restoration

# Load a noisy image
image = io.imread('image.jpg')

# Apply total variation denoising
denoised_image = restoration.denoise_tv_chambolle(image, weight=0.1)

# Display the noisy image and the denoised image
io.imshow_collection([image, denoised_image])
io.show()
PYTHON

輸出

scikit-image Python (How It Works: A Guide for Developers): Figure 4 - Image Denoising 輸出

你可以在 官方頁面 找到更多關於圖像處理和 NumPy 陣列的內容。

介紹 IronPDF

scikit-image Python (運作方式:開發者指南):圖 5 - IronPDF:Python PDF 庫

IronPDF 是一個強大的 Python 庫,專為使用 HTML、CSS、圖像和 JavaScript 來處理 PDF 文檔的創建、編輯和簽名而設計。 它優先考慮執行效率並以最低的內存使用量運行。 關鍵特性包括:

  • HTML 到 PDF 轉換: 將 HTML 文件、HTML 字串和 URL 轉換為 PDF 文檔,利用像 Chrome PDF 渲染器之類的功能。

  • 跨平台支持: 支持在 Windows、Mac、Linux 和各種雲平台上的 Python 3+。 IronPDF 同樣適用於 .NET、Java、Python 和 Node.js 環境。

  • 編輯和簽名: 自定義 PDF 屬性,執行如密碼和許可權等安全措施,並無縫應用數位簽名。

  • 頁面模板和設置: 創建帶有頁眉、頁腳、頁碼、可調邊距、自定義紙張大小和響應式設計的 PDF 佈局。

  • 標準合規: 嚴格遵循如 PDF/A 和 PDF/UA 的 PDF 標準,確保 UTF-8 字符編碼兼容性,並熟練地管理如圖像、CSS 樣式表和字體等資產。

安裝

pip install ironpdf 
pip install scikit-image
pip install ironpdf 
pip install scikit-image
SHELL

使用 IronPDF 和 Scikit Image 生成 PDF 文檔

先決條件

  1. 確保 Visual Studio Code 已作為代碼編輯器安裝
  2. 安裝了 Python 版本 3

首先,讓我們創建一個 Python 文件來添加我們的腳本。

打開 Visual Studio Code 並創建一個文件,scikitDemo.py

安裝必要的庫:

pip install scikit-image
pip install ironpdf
pip install scikit-image
pip install ironpdf
SHELL

然後添加以下 Python 代碼來演示 IronPDF 和 scikit-image Python 包的使用。

from skimage import io, filters
from ironpdf import * 

# Apply your license key
License.LicenseKey = "YOUR_LICENSE_KEY"

# Load an image
image = io.imread('image.jpg')

# Apply Gaussian blur
blurred_image = filters.gaussian(image, sigma=1.0)

# Apply Sobel edge detection
edges = filters.sobel(image)

# Save the results to a file
io.imshow_collection([image, blurred_image, edges]).savefig('ironPdf-skimage.png')

# Convert the saved image to a PDF document
ImageToPdfConverter.ImageToPdf("ironPdf-skimage.png").SaveAs("ironPdf-skimage.pdf")

# Display the images
io.show()
from skimage import io, filters
from ironpdf import * 

# Apply your license key
License.LicenseKey = "YOUR_LICENSE_KEY"

# Load an image
image = io.imread('image.jpg')

# Apply Gaussian blur
blurred_image = filters.gaussian(image, sigma=1.0)

# Apply Sobel edge detection
edges = filters.sobel(image)

# Save the results to a file
io.imshow_collection([image, blurred_image, edges]).savefig('ironPdf-skimage.png')

# Convert the saved image to a PDF document
ImageToPdfConverter.ImageToPdf("ironPdf-skimage.png").SaveAs("ironPdf-skimage.pdf")

# Display the images
io.show()
PYTHON

代碼說明

此代碼片段演示如何將 scikit-image(skimage)與 IronPDF 一起使用,以處理圖片並將結果轉換為 PDF 文檔。 以下是每一部分的解釋:

  1. 導入聲明: 從 scikit-image 中導入必要功能以進行圖片加載和圖片處理,並導入 IronPDF 的功能。

  2. 應用許可證密鑰: 設定 IronPDF 的許可證密鑰。 此步驟是為了使用 IronPDF 功能所必需的。

  3. 加載和處理圖像: 使用 scikit-image 的 io.imread 函數加載一個名為 'image.jpg' 的圖像。 然後使用 filters.gaussian 以 sigma 值 1.0 的方式對加載的圖像應用高斯模糊,並使用 filters.sobel 對加載的圖像應用 Sobel 邊緣檢測。

  4. 顯示和保存結果: io.imshow_collection([image, blurred_image, edges]).savefig('ironPdf-skimage.png'):將一組圖片(原圖、模糊和邊緣)保存為 'ironPdf-skimage.png'

  5. 轉換圖像為 PDF: ImageToPdfConverter.ImageToPdf("ironPdf-skimage.png").SaveAs("ironPdf-skimage.pdf"):使用 IronPDF 的功能將保存的 PNG 圖像轉換為 PDF 文檔。

  6. 顯示圖片: io.show():在圖形窗口中顯示圖片。

此代碼片段結合了 scikit-image 的圖像處理功能和 IronPDF 用於將處理過的圖像轉換為 PDF 文檔。 它演示了加載圖像、應用高斯模糊和 Sobel 邊緣檢測、將其保存為 PNG 文件、使用 IronPDF 將 PNG 轉換為 PDF 並顯示處理過的圖片。 這種整合適用於需要處理、分析並以 PDF 格式記錄圖像的任務,例如在科學研究、圖像分析報告或自動化文檔生成工作流程中。

輸出

scikit-image Python (運作方式:開發者指南):圖 6 - 圖像輸入

PDF

scikit-image Python (How It Works: A Guide for Developers): Figure 7 - PDF 輸出

IronPDF 授權

IronPDF 依靠許可證密鑰在 Python 上運行。 IronPDF for Python 提供 免費試用許可證,以便用戶在購買前檢查其廣泛功能。

將許可金鑰放在腳本的開始部分,然後再使用 IronPDF 套件

from ironpdf import * 
# Apply your license key
License.LicenseKey = "YOUR_LICENSE_KEY"
from ironpdf import * 
# Apply your license key
License.LicenseKey = "YOUR_LICENSE_KEY"
PYTHON

結論

scikit-image 為 Python 開發人員提供了有效處理圖像相關任務的能力。 無論您是進行計算機視覺、醫學成像還是藝術項目,這個包都能滿足您的需求。 scikit-image 是一個多功能且強大的 Python 圖像處理庫,提供廣泛的功能和算法來完成如過濾、分割、特徵提取和幾何轉換等任務。 其與其他科學庫的無縫整合使其成為研究人員、開發者和工程師進行圖像分析和計算機視覺應用的首選。

IronPDF 是一個 Python 庫,便於在 Python 應用中進行 PDF 文檔的創建、編輯和操作。 它提供了從 HTML、圖像或現有 PDF 生成 PDF 文件等功能。 此外,IronPDF 支持如合併或拆分 PDF 文檔、添加註釋、水印或數位簽名、提取 PDF 中的文本或圖片及管理如元數據和安全設置等文檔屬性。 此庫提供了一種有效的方式來編程處理 PDF 相關任務,使其適合需要文檔生成、報告創建或文檔管理功能的應用。

同時使用這兩個庫,用戶可以處理圖像、有效地處理它們並將結果存儲在 PDF 文檔中以作存檔目的。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。