|
发表于 2017-6-8 17:12:15
|
显示全部楼层
#我把程序装饰了一下,完成以后会说话,哈哈
# -*- coding: utf-8 -*-
#python3 下安装pil要用 pip install pillow命令
from PIL import Image
from random import randint
import os
需要转换的文件名='01-01.jpg'
新文件名='素描'+需要转换的文件名
old = Image.open(需要转换的文件名)
new = Image.new('L', old.size, 255)
w, d = old.size
old = old.convert('L')
# Define the size of the pencil:
PEN_SIZE = 3
COLOR_DIFF = 7
LINE_LEN = 2
for i in range(PEN_SIZE + 1, w - PEN_SIZE - 1):
for j in range(PEN_SIZE + 1, d - PEN_SIZE - 1):
originalcolor = 255
lcolor = sum([old.getpixel((i - r, j))
for r in range(PEN_SIZE)]) // PEN_SIZE
rcolor = sum([old.getpixel((i + r, j))
for r in range(PEN_SIZE)]) // PEN_SIZE
if abs(lcolor - rcolor) > COLOR_DIFF:
originalcolor -= (255 - old.getpixel((i, j))) // 4
for p in range(-LINE_LEN + randint(-1,1), LINE_LEN + randint(-1,1)):
new.putpixel((i, j+p), originalcolor)
ucolor = sum([old.getpixel((i, j - r))
for r in range(PEN_SIZE)]) // PEN_SIZE
dcolor = sum([old.getpixel((i, j + r))
for r in range(PEN_SIZE)]) // PEN_SIZE
if abs(ucolor - dcolor) > COLOR_DIFF:
originalcolor -= (255 - old.getpixel((i, j))) // 4
for p in range(-LINE_LEN + randint(-1,1), LINE_LEN + randint(-1,1)):
new.putpixel((i+p, j), originalcolor)
lucolor = sum([old.getpixel((i - r, j - r))
for r in range(PEN_SIZE)]) // PEN_SIZE
rdcolor = sum([old.getpixel((i + r, j + r))
for r in range(PEN_SIZE)]) // PEN_SIZE
if abs(lucolor - rdcolor) > COLOR_DIFF:
originalcolor -= (255 - old.getpixel((i, j))) // 4
for p in range(-LINE_LEN + randint(-1,1), LINE_LEN + randint(-1,1)):
new.putpixel((i-p, j+p), originalcolor)
rucolor = sum([old.getpixel((i + r, j - r))
for r in range(PEN_SIZE)]) // PEN_SIZE
ldcolor = sum([old.getpixel((i - r, j + r))
for r in range(PEN_SIZE)]) // PEN_SIZE
if abs(rucolor - ldcolor) > COLOR_DIFF:
originalcolor -= (255 - old.getpixel((i, j))) // 4
for p in range(-LINE_LEN + randint(-1,1), LINE_LEN + randint(-1,1)):
new.putpixel((i+p, j+p), originalcolor)
new.save(新文件名)
i= os.system('mshta vbscript:createobject("sapi.spvoice").speak("%s")(window.close)' %'素描已完成')
os.system(新文件名) |
|