[英]Writing command line argument for Wand
我想知道如何使用Wand庫將ImageMagick的此工作命令行序列轉換為Python腳本:
convert me.jpg -fill none -fuzz 1% -draw 'matte 0,0 floodfill' -flop -draw 'matte 0,0 floodfill' -flop me.png
從根本上講,它可以使我從前景圖像中減去已知背景圖像並具有一定的容忍度(例如,模糊)。 我想用平坦的顏色填充結果區域。
我想在內存中使用我的Python腳本來完成所有這些操作,以避免將解碼后的RAW圖像數據重新壓縮為jpeg兩次。 通過命令行執行此操作會強制執行額外的壓縮步驟,而在腳本中執行此操作只需要保存一個jpeg。
一些幫助,將不勝感激。
編輯:更正,正與我嘗試過的其他東西混淆。 上面的命令不會從已知圖像中減去。 實際上,這會占用左上方的像素,並從那里填充1%的絨毛。 顯而易見,當您看到只有一張輸入圖像時。 仍然知道如何將以上內容轉換為Python仍然很有幫助。
您可以嘗試這樣。
from wand.color import Color
from wand.drawing import Drawing
from wand.image import Image
with Image(filename='me.jpg') as img:
with Drawing() as ctx:
# -fill none
ctx.fill_color = Color('transparent')
# -draw 'matte 0,0 floodfill'
ctx.matte(x=0, y=0, paint_method='floodfill')
ctx(img)
# -flop
img.flop()
ctx(img)
img.flop()
img.save(filename='me.png')
對於-fuzz 1%
,您需要做一些額外的工作來計算量子值。
from wand.color import Color
from wand.drawing import Drawing
from wand.image import Image
import ctypes
library.MagickSetImageFuzz.argtypes = (ctypes.c_void_p,
ctypes.c_double)
with Image(filename='me.jpg') as img:
# -fuzz 1%
library.MagickSetImageFuzz(img.wand, img.quantum_range * 0.1)
with Drawing() as ctx:
# -fill none
ctx.fill_color = Color('transparent')
# -draw 'matte 0,0 floodfill'
ctx.matte(x=0, y=0, paint_method='floodfill')
# -flop -draw 'matte 0,0 floodfill' -flop
ctx.matte(x=img.width - 1, y=0, paint_method='floodfill')
ctx(img)
img.save(filename='me.png')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.