我如何以编程方式将Photoshop滤镜像素化>结晶化到图像上?

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

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

“

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

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

php api unity3d photoshop image-manipulation
2个回答
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,枕头。


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)
© www.soinside.com 2019 - 2024. All rights reserved.