首页 > 解决方案 > 如何将这个简单的 Photoshop 工作流程转换为 PIL

问题描述

Photoshop中有一个简单的技巧,您可以将彩色图像转换为线条图。

在 Photoshop 中,过程如下:https://youtu.be/aPn55fF-Ntk?t=110视频在某些部分可能有点 NSFW,但我链接到了重要的时间戳,即 SFW。

不想看视频的总结:

1)将您的图像转换为灰度并制作副本

2)将顶部副本的模式更改为颜色减淡

3)反转顶部图像

4)添加高斯模糊

5)合并2层

我的代码在下面,我被困在第 4 步。我不确定如何在 PIL 中重新创建该步骤,因为我不知道 Photoshop 在做什么。我不确定高斯模糊应用于何处,例如,我是否需要将高斯应用于原件和副本,然后将它们加在一起?我对此感到非常困惑。

import numpy as np
from PIL import Image
from PIL import ImageFilter
import PIL.ImageOps 

def dodge(front,back):
    # The formula comes from http://www.adobe.com/devnet/pdf/pdfs/blend_modes.pdf
    result=back*256.0/(256.0-front) 
    result[result>255]=255
    result[front==255]=255
    return result.astype('uint8')

fname = 'C:/Users/Luke Chen/Desktop/test.JPG'

img = Image.open(fname,'r').convert('L') # turn image to grayscale

arr = np.asarray(img)

img_blur = img.filter(ImageFilter.BLUR) 

blur = np.asarray(img_blur)

result = dodge(front=blur, back=arr) # color doge copy

result = Image.fromarray(result, 'L')

result = PIL.ImageOps.invert(result) # invert the color doge copy

# toDO Do something with gaussian blur?? and merge both images. 

result.show()

标签: pythoncomputer-visionphotoshop

解决方案


更新的答案

我昨天很忙,但今天早上有一些时间,所以这里有一种使用 Python 和numpy. 这可能有点笨拙,因为我只是 Python 的初学者,但它可以工作,你可以看到如何做一些事情,并且可以摆弄它来让它做你想做的事情。

#!/usr/local/bin/python3

import numpy as np
from PIL import Image, ImageFilter

# Load image and convert to Lightness
i0=Image.open('image.png').convert('L')

# Copy original image and Gaussian blur the copy
i1=i0.copy().filter(ImageFilter.GaussianBlur(3))

# Convert both images to numpy arrays to do maths - this part is probably clumsy
i0=np.array(i0,dtype=np.float32) / 255
i1=np.array(i1,dtype=np.float32) / 255
result=i0/i1
result*=255
result=np.clip(result,0,255)
result=result.astype(np.uint8)
result=Image.fromarray(result, 'L')

result.save("result.png")

在此处输入图像描述

原始答案

在这里做太多 Python 和 numpy 有点晚了,但我可以向您展示如何获得效果,并告诉您使用ImageMagicknumpy的步骤,明天可能会做。

所以,从这个开始:

在此处输入图像描述

然后,在终端中使用ImageMagick ,运行以下命令:

convert image.png -colorspace gray \( +clone -blur 0x3 \) -compose dividesrc -composite  result.png

在此处输入图像描述

所以,如果我解释了那个命令,你可以做这些numpy事情,或者我明天再做。它说... “加载输入图像并将其转换为灰度。制作灰度图像的副本并仅模糊副本。将原始图像除以模糊副本。保存结果。” .


推荐阅读