本文将从多个方面对Python照片转手绘进行详细阐述。
一、OpenCV库的安装和基本使用
1、安装OpenCV库:
pip install opencv-python
2、导入OpenCV库:
import cv2
3、使用OpenCV库进行图像读取和处理:
img = cv2.imread('photo.jpg') gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
二、边缘检测和图像滤波
1、边缘检测:
edges = cv2.Canny(gray_img, 100, 200)
2、图像滤波:
blur_img = cv2.blur(gray_img, (5, 5))
三、转为手绘风格
1、将边缘图和模糊图进行合并:
cartoon_img = cv2.bitwise_and(blur_img, blur_img, mask=edges)
2、通过调整参数增强手绘效果:
details = cv2.detailEnhance(cartoon_img, sigma_s=30, sigma_r=0.1)
四、保存和展示结果
1、保存图片:
cv2.imwrite('cartoon.jpg', details)
2、展示结果:
cv2.imshow('Cartoon Image', details) cv2.waitKey(0) cv2.destroyAllWindows()
通过以上步骤,我们可以用Python将一张照片转换成带有手绘效果的图像。