我该如何优化这个魔杖代码?执行时间很长

How can I optimize this wand code? It is very long to execute

我有一个代码在我的本地机器上需要 45 秒(这是一个很好的 i7 配置)。我目前正在使用魔杖,但如果您有其他想法,我愿意向其他图书馆开放。这段代码的目标是模糊一张图片的中间,然后逐渐去模糊直到边界:

from wand.image import Image, CHANNELS
from wand.api import library
from wand.display import display
from wand.drawing import Drawing
from wand.color import Color

with Image(filename='paysage.jpg') as img:
    points = {'black': (0, img.height), 'white': (0, 0)}
    img.sparse_color('bilinear', points)
    img.function('polynomial', [3, -3, 1])
    img.negate()
    img.save(filename='filtre.jpg')

with Image(filename='paysage.jpg') as src:
    with Image(filename='filtre.jpg') as dst:
        src.composite(dst, operator='blur', arguments='20')
    src.save(filename='paysagefiltered.jpg')
    display(src)

感谢您的帮助:)

另一个有问题的镜头,它直接应用半径逐渐变化的模糊,从图像中裁剪部分,将所有部分组合成一个图像并应用最后一个模糊来平滑所有过渡。您可以根据需要调整值。

from PIL import Image
from PIL import ImageFilter

RADIUS = 0
INPUT_IMAGE_FILENAME = "./w2UR2.jpg"
OUTPUT_IMAGE_FILENAME = "./output.jpg"

# Open source image 
im = Image.open(INPUT_IMAGE_FILENAME)

# adjustable blur radius 
diam = 2 * RADIUS

w, h = im.size
step = int(h / 20)
result = Image.new("RGB", (w, h))

stack = []
for i in range(20):
    blur = im.filter(ImageFilter.GaussianBlur(diam / 2))
    crop = blur.crop((0, step * i, w, step * (i + 1)))
    result.paste(crop, (0, i * step))
    diam += (i < 10) - (i > 10)


# smooth out the whole image

result = result.filter(ImageFilter.GaussianBlur(1))
result.save(OUTPUT_IMAGE_FILENAME)

结果:

而 9 岁 i5 的 运行 时间是:

➜  ~ time python a.py
python a.py  1.03s user 0.35s system 131% cpu 1.049 total