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

所以这不是通常的像素效果,像素不是方形的。
有没有我可以使用的API?我一直在找这个,但是我有点迷路了。
发布于 2019-04-24 04:05:02
哦,我刚刚注意到你用的是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')它会变成这样:

如下所示:

如果你有500个水晶,就是这样的:

通过减少到256种颜色和一个托盘图像,为每种颜色找到最接近的颜色,然后简单地在LUT中查找它们,可能可以提高速度。也许这是一份不时之需的工作。
关键词:Python,voronoi,水晶,结晶,Photoshop,滤镜,图像,图像处理,块状,毛皮,枕头。
发布于 2019-11-27 15:44:44
下面是如何在Python语言中使用Photoshop api应用晶体滤镜的方法,取自示例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)https://stackoverflow.com/questions/55814409
复制相似问题