批量处理图片成手画模式

好久不见. 提交于 2020-01-27 01:22:19
from PIL import  Image
import numpy as np
import os
import cv2 as cv

def mulhanpicture(path):
    filelist = os.listdir(path)
    for item in filelist:
        if item.endswith('.png') or item.endswith('.jpg'):
            item = path + '/' + item
            item_save = item[:-4] + '_hand' + item[-4:]
            a = np.asarray(Image.open(item,).convert('L')).astype('float')
            depth = 5  #改值可改变程度(0-100)
            grad = np.gradient(a)
            grad_x,grad_y = grad
            grad_x = grad_x*depth/100.0
            grad_y = grad_y*depth/100.0
            A = np.sqrt(grad_x**2+grad_y**2+1.0)
            unix = grad_x/A
            uniy = grad_y/A
            uniz = 1.0/A
            vecaz = np.pi/2.2
            vecel = np.pi/4.0
            dx = np.cos(vecel)*np.cos(vecaz)
            dy = np.cos(vecel)*np.sin(vecaz)
            dz = np.sin(vecel)
            b = 255*(dx*unix+dy*uniy+dz*uniz)
            b = b.clip(0,255)
            im = Image.fromarray(b.astype('uint8'))
            im.show()
            cv.waitKey(30)
            im.save(item_save)

mulhanpicture('F:\picture2')

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!