python - 如何将这个简单的 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()
解决方案
更新的答案
我昨天很忙,但今天早上有一些时间,所以这里有一种使用 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
事情,或者我明天再做。它说... “加载输入图像并将其转换为灰度。制作灰度图像的副本并仅模糊副本。将原始图像除以模糊副本。保存结果。” .
推荐阅读
- php - 在 WooCommerce 中通过 Url 变量 (GET) 应用费用
- r - fit_transform(tfidf, lsa) 返回错误
- javascript - Firebase 函数向主题发送消息
- tensorflow - ValueError:未知层:QuantizeLayer
- python - 如何配置 paho mqtt 订阅以高速获取数据,例如每秒 1000 条消息
- javascript - 谷歌脚本`SlidesApp.openById`或`SlidesApp.openByUrl`返回空对象`{}`
- ssl - server.xml 中的密码配置
- ruby - 我们如何对大哈希进行分页?
- javascript - Sinon 存根抛出“TypeError:无法重新定义属性”
- c++ - typeid() 返回错误的类型