Python Wand 中的 motion_blur() 函式
在給定的問題陳述中,我們必須使用 Python Wand 庫對影像應用 motion_blur 函式。有時我們需要對某些影像執行模糊操作。因此,使用 Python Wand,我們可以輕鬆完成此任務並將影像模糊到任何程度。
什麼是 Python Wand?
Wand 是一種工具,用於轉換影像格式。它可以將影像從一種格式轉換為另一種格式。此庫提供廣泛的功能來執行各種影像操作以進行處理。要在我們的系統中安裝 Wand 庫,我們可以使用此命令 pip install wand。
Wand 庫的用途包括:
它可以將影像從一種格式轉換為另一種格式。
藉助 Wand,我們可以讀取或寫入任何格式的影像。
此庫也可用於縮放和裁剪影像。
還可以使用 Wand 進行影像顏色增強。
此庫可用於轉換影像。
我們還可以向影像新增簡單和特殊效果。
語法
因此,motion_blur 函式是 Wand 庫中的內建函式,有助於重現或處理給定影像。motion_blur 的語法如下所示:
motion_blur(radius, sigma, angle)
上述問題的邏輯
為了解決給定的問題,我們將使用 Wand 庫並使用 motion_blur 函式根據給定的半徑、sigma 和角度模糊影像。
演算法
步驟 1 - 首先從 Wand 庫匯入 Image 函式。
步驟 2 - 定義一個函式來執行給定的任務,並將其命名為 apply_motion_blur,在此函式中,我們將傳遞兩個引數:img_loc,即輸入影像的位置;blur_img_loc,即模糊影像或輸出影像的位置。
步驟 3 - 接下來,我們將呼叫輸入影像以執行模糊操作。
步驟 4 - 然後,我們將使用 clone 函式克隆輸入影像,並將克隆的影像儲存為 image_motion_blur。
步驟 5 - 現在,我們將對 Image_motion_blur 影像應用 motion_blur 函式,並提供半徑、sigma 和角度的值。
步驟 6 - 應用 motion_blur 函式後,我們將模糊影像儲存到指定位置。
示例
# import the Image function from wand library
from wand.image import Image
def apply_motion_blur(img_loc, blur_img_loc):
#Call the image to perform blur functionality
with Image(filename=img_loc) as img:
# make a copy of the image
with img.clone() as image_motion_blur:
# call the motion_blur function
image_motion_blur.motion_blur(
27, 4, 50) # values of radius, sigma and angle
image_motion_blur.save(
filename=blur_img_loc
) #store the image with image_motion_blur name
#testing the function
img_location = 'H:/Python/tutorialspointimage.jpg'
blur_img_location = 'image_motion_blur.jpg'
apply_motion_blur(img_location, blur_img_location)
輸出
$$\mathrm{模糊前}$$
$$\mathrm{模糊後}$$
複雜度
上面建立的演算法的時間複雜度為 O(1),因為我們克隆了影像並執行了模糊操作,這需要恆定的時間來完成執行。
結論
我們已成功實現了用於執行和顯示 Python Wand 中 motion_blur 函式的工作機制的程式碼。藉助此函式,我們已根據給定的半徑、sigma 和角度有效地將輸入影像轉換為模糊影像。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP