python如何將圖片轉(zhuǎn)換素描畫
代碼如下
# -*- coding:utf-8 -*-import cv2import numpy as npfrom tkinter import filedialog, Tkfrom os import getcwdfrom re import findalldef open_path(): # 圖片路徑 root = Tk() root.withdraw() file_path = (filedialog.askopenfilename(title=’選擇圖片文件’, filetypes=[(’All Files’, ’*’)])) return file_pathdef dodgeNaive(image, mask): # determine the shape of the input image width, height = image.shape[:2] # prepare output argument with same size as image blend = np.zeros((width, height), np.uint8) for col in range(width): for row in range(height): # do for every pixel if mask[col, row] == 255:# avoid division by zeroblend[col, row] = 255 else:# shift image pixel value by 8 bits# divide by the inverse of the masktmp = (image[col, row] << 8) / (255 - mask)# print(’tmp={}’.format(tmp.shape))# make sure resulting value stays within boundsif tmp.any() > 255: tmp = 255 blend[col, row] = tmp return blenddef dodgeV2(image, mask): return cv2.divide(image, 255 - mask, scale=256)def burnV2(image, mask): return 255 - cv2.divide(255 - image, 255 - mask, scale=256)def rgb_to_sketch(src_image_name): print(’轉(zhuǎn)換中......’) img_rgb = cv2.imread(src_image_name) img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY) # 讀取圖片時(shí)直接轉(zhuǎn)換操作 # 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(’轉(zhuǎn)換完成!!!n’) print(’保存路徑:’ + save_path) cv2.imshow(save_path, img_blend) cv2.waitKey(0) cv2.destroyAllWindows()if __name__ == ’__main__’: print(’請(qǐng)選擇圖片(路徑不要含中文):’) src_image_name = open_path() # 文件路徑 print(src_image_name + ’n’) image_name = ’’.join(findall(r’[^/:*?'<>|rn]+$’, src_image_name)) # 獲取文件名 dst_image_name = ’Sketch_’ + image_name rgb_to_sketch(src_image_name)
效果如下
以上就是python如何將圖片轉(zhuǎn)換素描畫的詳細(xì)內(nèi)容,更多關(guān)于python圖片轉(zhuǎn)換素描畫的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Java向Runnable線程傳遞參數(shù)方法實(shí)例解析2. Java 基于UDP協(xié)議實(shí)現(xiàn)消息發(fā)送3. 10個(gè)提供免費(fèi)PHP腳本下載的網(wǎng)站4. php5.6不能擴(kuò)展redis.so的解決方法5. ASP.NET MVC前臺(tái)動(dòng)態(tài)添加文本框并在后臺(tái)使用FormCollection接收值6. PHP獲取時(shí)間戳等相關(guān)函數(shù)匯總7. python 爬取嗶哩嗶哩up主信息和投稿視頻8. PHP設(shè)計(jì)模式(四)原型模式Prototype實(shí)例詳解【創(chuàng)建型】9. Python編寫nmap掃描工具10. 如何基于python3和Vue實(shí)現(xiàn)AES數(shù)據(jù)加密
