python如何将图片转换素描画
代码如下
#-*-coding:utf-8-*- importcv2 importnumpyasnp fromtkinterimportfiledialog,Tk fromosimportgetcwd fromreimportfindall defopen_path(): #图片路径 root=Tk() root.withdraw() file_path=(filedialog.askopenfilename(title='选择图片文件',filetypes=[('AllFiles','*')])) returnfile_path defdodgeNaive(image,mask): #determinetheshapeoftheinputimage width,height=image.shape[:2] #prepareoutputargumentwithsamesizeasimage blend=np.zeros((width,height),np.uint8) forcolinrange(width): forrowinrange(height): #doforeverypixel ifmask[col,row]==255: #avoiddivisionbyzero blend[col,row]=255 else: #shiftimagepixelvalueby8bits #dividebytheinverseofthemask tmp=(image[col,row]<<8)/(255-mask) #print('tmp={}'.format(tmp.shape)) #makesureresultingvaluestayswithinbounds iftmp.any()>255: tmp=255 blend[col,row]=tmp returnblend defdodgeV2(image,mask): returncv2.divide(image,255-mask,scale=256) defburnV2(image,mask): return255-cv2.divide(255-image,255-mask,scale=256) defrgb_to_sketch(src_image_name): print('转换中......') img_rgb=cv2.imread(src_image_name) img_gray=cv2.cvtColor(img_rgb,cv2.COLOR_BGR2GRAY) #读取图片时直接转换操作 #img_gray=cv2.imread('example.jpg',cv2.IMREAD_GRAYSCALE) img_gray_inv=255-img_gray img_blur=cv2.GaussianBlur(img_gray_inv,ksize=(21,21), sigmaX=0,sigmaY=0) img_blend=dodgeV2(img_gray,img_blur) #cv2.imshow('original',img_rgb) #cv2.imshow('gray',img_gray) #cv2.imshow('gray_inv',img_gray_inv) #cv2.imshow('gray_blur',img_blur) cv2.imwrite(dst_image_name,img_blend) save_path=getcwd()+"\\"+dst_image_name#保存路径 print('转换完成!!!\n') print('保存路径:'+save_path) cv2.imshow(save_path,img_blend) cv2.waitKey(0) cv2.destroyAllWindows() if__name__=='__main__': print('请选择图片(路径不要含中文):') src_image_name=open_path()#文件路径 print(src_image_name+'\n') image_name=''.join(findall(r'[^\\/:*?"<>|\r\n]+$',src_image_name))#获取文件名 dst_image_name='Sketch_'+image_name rgb_to_sketch(src_image_name)
效果如下
以上就是python如何将图片转换素描画的详细内容,更多关于python图片转换素描画的资料请关注毛票票其它相关文章!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。