如何以编程方式将Photoshop滤镜Pixelate> Crystallize应用于图像?

问题描述 投票:2回答:1

我需要通过一些API或库将结晶像素滤镜应用于图像。此效果应如下所示:

所以这不是通常的像素效果,像素不是方形的。

我可以使用任何API吗?我一直在寻找这个,但我有点失落。

非常感谢你!

php api unity3d photoshop image-manipulation
1个回答
2
投票

哎呀,我刚注意到你用PHP标记而不是Python - 对不起!我现在将它留作参考,可能会在另一天做一个PHP版本。

我对此进行了快速尝试并且运行良好:

#!/usr/bin/env python3

import numpy
import random
import math
import sys
from PIL import Image

def crystallize(im, cnt):
    # Make output image same size
    res = np.zeros_like(im)
    h, w = im.shape[:2]
    # Generate some randomly placed crystal centres
    nx = np.random.randint(0,w,cnt,dtype=np.uint16)
    ny = np.random.randint(0,h,cnt,dtype=np.uint16)
    # Pick up colours at those locations from source image
    sRGB = []
    for i in range(cnt):
        sRGB.append(im[ny[i],nx[i]])

    # Iterate over image
    for y in range(h):
        for x in range(w):
            # Find nearest crystal centre...
            dmin = sys.float_info.max
            for i in range(cnt):
                d = (y-ny[i])*(y-ny[i]) + (x-nx[i])*(x-nx[i])
                if d < dmin:
                    dmin = d
                    j = i
            # ... and copy colour of original image to result
            res[y,x,:] = sRGB[j]
    return res

# Open image, crystallize and save
im  = Image.open('duck.jpg')
res = crystallize(np.array(im),200)
Image.fromarray(res).save('result.png')

它变成了这样:

enter image description here

进入这个:

enter image description here

或者如果你去买500颗水晶:

enter image description here


通过减少到256色和托盘化图像,找到最接近的颜色,然后简单地在LUT中查找它们,可以提高速度。也许是下雨天的工作......


关键词:Python,voronoi,水晶,结晶,Photoshop,过滤器,图像,图像处理,Numpy,PIL,枕头。

© www.soinside.com 2019 - 2024. All rights reserved.