python實(shí)現(xiàn)圖片轉(zhuǎn)換成素描和漫畫格式
更新時(shí)間:2020年08月19日 15:19:48 作者:錢甫新
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)圖片轉(zhuǎn)換成素描和漫畫格式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了python實(shí)現(xiàn)圖片轉(zhuǎn)換成素描和漫畫格式的具體代碼,供大家參考,具體內(nèi)容如下
原圖

圖片轉(zhuǎn)換后的成果

源碼
# -*- coding: utf-8 -*-
import cv2
from PIL import Image, ImageOps, ImageFilter
# 轉(zhuǎn)換成漫畫風(fēng)格
def toCarttonStyle(picturePath):
# 設(shè)置輸入輸出路徑和文件名稱
imgInput_FileName = picturePath
imgOutput_FileName = picturePath.split(".")[0] + '_cartoon.' + picturePath.split(".")[1]
# 屬性設(shè)置
num_down = 2 # 縮減像素采樣的數(shù)目
num_bilateral = 7 # 定義雙邊濾波的數(shù)目
# 讀取圖片
img_rgb = cv2.imread(imgInput_FileName)
# 用高斯金字塔降低取樣
img_color = img_rgb
for _ in range(num_down):
img_color = cv2.pyrDown(img_color)
# 重復(fù)使用小的雙邊濾波代替一個(gè)大的濾波
for _ in range(num_bilateral):
img_color = cv2.bilateralFilter(img_color, d=9, sigmaColor=9, sigmaSpace=7)
# 升采樣圖片到原始大小
for _ in range(num_down):
img_color = cv2.pyrUp(img_color)
# 轉(zhuǎn)換為灰度并且使其產(chǎn)生中等的模糊
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY)
img_blur = cv2.medianBlur(img_gray, 7)
# 檢測(cè)到邊緣并且增強(qiáng)其效果
img_edge = cv2.adaptiveThreshold(img_blur, 255,
cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY,
blockSize=9,
C=2)
# 算法處理后,照片的尺寸可能會(huì)不統(tǒng)一
# 把照片的尺寸統(tǒng)一化
height=img_rgb.shape[0]
width = img_rgb.shape[1]
img_color=cv2.resize(img_color,(width,height))
# 轉(zhuǎn)換回彩色圖像
img_edge = cv2.cvtColor(img_edge, cv2.COLOR_GRAY2RGB)
img_cartoon = cv2.bitwise_and(img_color, img_edge)
# 保存轉(zhuǎn)換后的圖片
cv2.imwrite(imgOutput_FileName, img_cartoon)
print('文件轉(zhuǎn)換成漫畫成功,保存在' + imgOutput_FileName)
# 透明度轉(zhuǎn)換 素描轉(zhuǎn)換的一部分
def dodge(a, b, alpha):
# alpha為圖片透明度
return min(int(a * 255 / (256 - b * alpha)), 255)
# 圖片轉(zhuǎn)換為素描
def toSketchStyle(picturePath, blur=25, alpha=1.0):
# 設(shè)置輸入輸出路徑和文件名稱
imgInput_FileName = picturePath
imgOutput_FileName = picturePath.split(".")[0] + '_Sketch.' + picturePath.split(".")[1]
# 轉(zhuǎn)化成ima對(duì)象
img = Image.open(picturePath)
# 將文件轉(zhuǎn)成灰色
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))
# 保存轉(zhuǎn)換后文件
img1.save(imgOutput_FileName)
print('文件轉(zhuǎn)換成漫畫成功,保存在' + imgOutput_FileName)
if __name__ == '__main__':
imgInput_FileName = input('輸入文件路徑:')
while True:
print('1、漫畫風(fēng)格')
print('2、素描風(fēng)格')
userChoose = input('請(qǐng)選擇風(fēng)格(輸入序號(hào)即可):')
if userChoose.__eq__('1'):
toCarttonStyle(imgInput_FileName)
break
elif userChoose.__eq__('2'):
toSketchStyle(imgInput_FileName)
break
else:
print('違法輸入(請(qǐng)輸入序號(hào))')
break
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
Python Pytorch深度學(xué)習(xí)之圖像分類器
今天小編就為大家分享一篇關(guān)于Pytorch圖像分類器的文章,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-10-10
詳細(xì)解析Python中__init__()方法的高級(jí)應(yīng)用
這篇文章主要介紹了詳細(xì)解析Python中__init__()方法的高級(jí)應(yīng)用,包括在映射和elif序列等地方的更為復(fù)雜的用法,需要的朋友可以參考下2015-05-05
python+VTK環(huán)境搭建及第一個(gè)簡(jiǎn)單程序代碼
這篇文章主要介紹了python+VTK環(huán)境搭建及第一個(gè)簡(jiǎn)單程序代碼,簡(jiǎn)單介紹了vtk,然后分享了安裝步驟,最后涉及一個(gè)簡(jiǎn)單的代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12
使用jupyter notebook輸出顯示不完全的問(wèn)題及解決
這篇文章主要介紹了使用jupyter notebook輸出顯示不完全的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
使用IronPython把Python腳本集成到.NET程序中的教程
這篇文章主要介紹了使用IronPython把Python腳本集成到.NET程序中的教程,現(xiàn)在剛剛被微軟開源的.NET重新成為業(yè)界熱點(diǎn)、本文介紹了使Python和.NET交互的IronPython,需要的朋友可以參考下2015-03-03

