python实现图片彩色转化为素描

重要的人越来越少,剩下的越来越重要。拼你想要的,争你没有的。要想人前显贵,就得背后遭罪。最穷不过要饭,不死终会出头!

本文实例为大家分享了Python将图片彩色转化为素描的具体代码,供大家参考,具体内容如下

第一种:

from PIL import Image, ImageFilter, ImageOps
img = Image.open('E:\\picture\\1.png')
def dodge(a, b, alpha):
 return min(int(a*255/(256-b*alpha)), 255)
def draw(img, blur=25, alpha=1.0):
 img1 = img.convert('L') #图片转换成灰色
 img2 = img1.copy()
 img2 = ImageOps.invert(img2)
 for i in range(blur):   #模糊度
  img2 = img2.filter(ImageFilter.BLUR)
 width, height = img1.size
 for x in range(width):
  for y in range(height):
   a = img1.getpixel((x, y))
   b = img2.getpixel((x, y))
   img1.putpixel((x, y), dodge(a, b, alpha))
 img1.show()
 img1.save('E:\\picture\\10.png')
draw(img)

第二种:

from PIL import Image
import os
 
# 图像组成:红绿蓝 (RGB)三原色组成 亮度(255,255,255)
image = "E:\\picture\\123.png"
img = Image.open(image)
img_all = "E:\\picture\\1234.png"
new = Image.new("L", img.size, 255)
width, height = img.size
img = img.convert("L")
# print(img.size)
# print(img.mode) #RBG
#
# img_get = img.getpixel((0, 0))
# print(img_get) #三原色通道
#
# img_L=img.convert('L')
# print(img_L)
# img_get_L=img_L.getpixel((0,0)) #换算 得到灰度值
# print(img_get_L)
 
# 定义画笔的大小
Pen_size = 3
# 色差扩散器
Color_Diff = 6
for i in range(Pen_size + 1, width - Pen_size - 1):
 for j in range(Pen_size + 1, height - Pen_size - 1):
  # 原始的颜色
  originalColor = 255
  lcolor = sum([img.getpixel((i - r, j)) for r in range(Pen_size)]) // Pen_size
  rcolor = sum([img.getpixel((i + r, j)) for r in range(Pen_size)]) // Pen_size
 
  # 通道----颜料
  if abs(lcolor - rcolor) > Color_Diff:
   originalColor -= (255 - img.getpixel((i, j))) // 4
   new.putpixel((i, j), originalColor)
 
  ucolor = sum([img.getpixel((i, j - r)) for r in range(Pen_size)]) // Pen_size
  dcolor = sum([img.getpixel((i, j + r)) for r in range(Pen_size)]) // Pen_size
 
  # 通道----颜料
  if abs(ucolor - dcolor) > Color_Diff:
   originalColor -= (255 - img.getpixel((i, j))) // 4
   new.putpixel((i, j), originalColor)
 
  acolor = sum([img.getpixel((i - r, j - r)) for r in range(Pen_size)]) // Pen_size
  bcolor = sum([img.getpixel((i + r, j + r)) for r in range(Pen_size)]) // Pen_size
 
  # 通道----颜料
  if abs(acolor - bcolor) > Color_Diff:
   originalColor -= (255 - img.getpixel((i, j))) // 4
   new.putpixel((i, j), originalColor)
 
  qcolor = sum([img.getpixel((i + r, j - r)) for r in range(Pen_size)]) // Pen_size
  wcolor = sum([img.getpixel((i - r, j + r)) for r in range(Pen_size)]) // Pen_size
 
  # 通道----颜料
  if abs(qcolor - wcolor) > Color_Diff:
   originalColor -= (255 - img.getpixel((i, j))) // 4
   new.putpixel((i, j), originalColor)
 
new.save(img_all)
 
i = os.system('mshta vbscript createobject("sapi.spvoice").speak("%s")(window.close)' % '您的图片转换好了')
os.system(img_all)

如果报错

可以直接安装
命令pip install Pillow即可

以上就是python实现图片彩色转化为素描。有志者,事竞成,破釜沉舟百二秦关终归楚;苦心人,天不负,卧薪尝胆三千越甲可吞吴。更多关于python实现图片彩色转化为素描请关注haodaima.com其它相关文章!

标签: 转化为 python