How can I apply the Photoshop filter Pixelate > Crystallize to an image programatically?

时间:2019-04-23 15:16:27

标签: php api unity3d photoshop image-manipulation

I need to apply crystallized pixels filter to an image via some API or library. This effect should look like this:

image_example

So this is not the usual pixel effect, the pixels are not square shaped.

Is there any API I can use? I have been looking for this but I am a bit lost.

Thank you very much!

2 个答案:

答案 0 :(得分: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,crystal,crystallize,Photoshop,滤镜,图像,图像处理,Numpy,PIL,Pillow。

答案 1 :(得分:0)

这是在Python中将Photoshop api与Crystallize滤镜一起应用的方法,取自示例https://github.com/lohriialo/photoshop-scripting-python/blob/master/EmbossAction.py

from win32com.client import Dispatch, GetActiveObject

app = GetActiveObject("Photoshop.Application")
fileName = "C:\Github\Test.psd"
docRef = app.Open(fileName)

docRef.ActiveLayer = docRef.ArtLayers.Item(1)

def applyCrystallize(cellSize):
    cellSizeID = app.CharIDToTypeID("ClSz")
    eventCrystallizeID = app.CharIDToTypeID("Crst")
    filterDescriptor = Dispatch('Photoshop.ActionDescriptor')
    filterDescriptor.PutInteger(cellSizeID, cellSize)
    app.ExecuteAction(eventCrystallizeID, filterDescriptor)

applyCrystallize(25)