跳過到頁腳內容
PYTHON 幫助

Wand python(開發人員工作原理)

Wand是一個功能強大且易於使用的 Python 庫,它充當了流行的圖像處理工具 ImageMagick 的綁定。 它能讓您輕鬆執行各種影像處理任務,是開發人員和資料科學家處理影像的絕佳選擇。 本文將使用 Wand Python 並示範其用法。 此外,我們還將研究Iron SoftwareIronPDF ,並學習如何使用這兩個庫進行影像處理以及將影像歸檔到 PDF 中。

為什麼要使用魔杖?

Wand 為 ImageMagick 提供了一個 Pythonic 接口,使 Python 開發人員更容易使用。 以下是考慮使用 Wand 的一些主要原因:

1.全面的影像處理: Wand 支援多種影像格式,並提供多種影像處理功能,包括調整大小、裁切、旋轉和添加效果。 2.與 NumPy 整合: Wand 可以與 NumPy 無縫協作,使其適用於涉及影像資料的機器學習和資料分析任務。 3.跨平台相容性: Wand 可在各種作業系統上運行,包括 Windows、macOS 和 Linux。

Wand入門指南

安裝

要安裝 Wand,您的系統需要安裝 ImageMagick。 您可以使用 pip 安裝 Wand:

pip install Wand
pip install Wand
SHELL

基本用法

以下是一個簡單的範例,示範如何使用 Wand 進行基本影像處理:

from wand.image import Image

# Open an image file and initialize the image class
with Image(filename='example.jpg') as img:
    print(f'Original size: {img.size}')
    # Resize the image
    img.resize(200, 200)
    img.save(filename='resized_example.jpg')
    print(f'Resized size: {img.size}')  # Resized size: 200x200
from wand.image import Image

# Open an image file and initialize the image class
with Image(filename='example.jpg') as img:
    print(f'Original size: {img.size}')
    # Resize the image
    img.resize(200, 200)
    img.save(filename='resized_example.jpg')
    print(f'Resized size: {img.size}')  # Resized size: 200x200
PYTHON

在這個例子中,Wand 打開一個圖像文件,列印其原始大小,將其調整大小為 200x200 像素,並保存調整大小後的圖像。

進階功能

Python Wand 提供了許多進階功能,可用於更複雜的影像處理任務:

1.影像效果:套用各種效果,如模糊、銳利化和浮雕。 2.顏色處理:調整顏色、轉換為灰階等。 3.影像變換:執行旋轉、縮放和裁切等變換。 4.特效:加入雜訊、創造寶麗來效果等等。

範例:應用效果

以下是使用魔棒工具對影像應用一些效果的範例:

from wand.image import Image  # Import image class
from wand.display import display

# Open an image file
with Image(filename='image.jpg') as img:
    # Apply a blur effect
    img.blur(radius=5, sigma=3)  # Can also use flip function, circle function, etc.
    img.save(filename='blurred_image.jpg')
    # Display the blurred image
    display(img)
from wand.image import Image  # Import image class
from wand.display import display

# Open an image file
with Image(filename='image.jpg') as img:
    # Apply a blur effect
    img.blur(radius=5, sigma=3)  # Can also use flip function, circle function, etc.
    img.save(filename='blurred_image.jpg')
    # Display the blurred image
    display(img)
PYTHON

上述程式碼使用 blur 函數對輸入影像進行模糊處理。

輸出

Wand Python(開發者使用方法):圖 1

IronPDF簡介

Wand Python(開發者使用方法):圖 2 - 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和 Wand 產生 PDF 文檔

先決條件

  1. 確保已安裝 Visual Studio Code 作為程式碼編輯器。
  2. 已安裝 Python 版本 3。
  3. 安裝 ImageMagick。
  4. 確保環境變數 MAGICK_HOME 設定為安裝路徑。

首先,讓我們建立一個 Python 檔案來加入我們的腳本。

開啟 Visual Studio Code 並建立一個文件,wandDemo.py

安裝必要的庫:

pip install wand
pip install ironpdf
pip install wand
pip install ironpdf
SHELL

然後加入以下 Python 程式碼來示範IronPDF和 Wand Python 套件的用法:

from wand.image import Image  # Import image class
from wand.display import display
from ironpdf import * 

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

# Open an image file
with Image(filename='image.jpg') as img:
    # Apply a blur effect
    img.blur(radius=5, sigma=3)
    img.save(filename='blurred_image.jpg')
    # Display the image
    display(img)
    # Save the transformed image as PNG
    img.savefig('ironPdf-wand.png')
    # Convert the PNG image to PDF
    ImageToPdfConverter.ImageToPdf("ironPdf-wand.png").SaveAs("ironPdf-wand.pdf")
from wand.image import Image  # Import image class
from wand.display import display
from ironpdf import * 

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

# Open an image file
with Image(filename='image.jpg') as img:
    # Apply a blur effect
    img.blur(radius=5, sigma=3)
    img.save(filename='blurred_image.jpg')
    # Display the image
    display(img)
    # Save the transformed image as PNG
    img.savefig('ironPdf-wand.png')
    # Convert the PNG image to PDF
    ImageToPdfConverter.ImageToPdf("ironPdf-wand.png").SaveAs("ironPdf-wand.pdf")
PYTHON

程式碼解釋

此 Python 腳本示範如何使用 WandIronPDF 庫進行影像處理和 PDF 生成:

  1. 此腳本首先使用 Wand 對影像套用模糊效果 (image.jpg)。
  2. 儲存產生的模糊影像並顯示它。
  3. 將圖像再次儲存為 PNG 檔案(ironPdf-wand.png)。
  4. 最後,它使用IronPDF將此 PNG 圖像轉換為 PDF (ironPdf-wand.pdf)。

它展示了 Python 中影像處理庫和 PDF 生成庫的整合。

輸出 PDF

Wand Python(開發者使用方法):圖 3

IronPDF許可

IronPDF許可證密鑰可讓使用者在購買前體驗其豐富的功能。

在使用IronPDF軟體包之前,請將許可證密鑰放在腳本的開頭:

from ironpdf import * 

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

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

結論

Wand是一個功能強大且用途廣泛的 Python 影像處理函式庫。 它與 ImageMagick 和 NumPy 的整合使其成為開發人員和資料科學家的寶貴工具。 無論您需要執行簡單的影像處理還是複雜的變換,Wand 都能在一個易於使用的 Python 軟體包中提供您所需的功能。

IronPDF是一個 Python 庫,旨在以程式設計方式建立、操作和轉換 PDF 文件。 它提供從各種來源(如圖像和 HTML)生成 PDF、合併 PDF、添加安全功能等功能。 IronPDF功能全面,支援跨平台開發,並能與 Python 應用程式良好集成,使其成為高效處理 PDF 相關任務的強大工具。

這些庫可以結合起來用於影像處理,並將結果以標準 PDF 格式存儲,以便存檔。

Curtis Chau
技術作家

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

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

Iron Support Team

We're online 24 hours, 5 days a week.
Chat
Email
Call Me