python如何将图片转换素描画
作者:崔笑颜 时间:2022-05-26 23:17:49
代码如下
# -*- coding:utf-8 -*-
import cv2
import numpy as np
from tkinter import filedialog, Tk
from os import getcwd
from re import findall
def open_path():
# 图片路径
root = Tk()
root.withdraw()
file_path = (filedialog.askopenfilename(title='选择图片文件', filetypes=[('All Files', '*')]))
return file_path
def 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 zero
blend[col, row] = 255
else:
# shift image pixel value by 8 bits
# divide by the inverse of the mask
tmp = (image[col, row] << 8) / (255 - mask)
# print('tmp={}'.format(tmp.shape))
# make sure resulting value stays within bounds
if tmp.any() > 255:
tmp = 255
blend[col, row] = tmp
return blend
def 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('转换中......')
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)
效果如下
来源:https://cloud.tencent.com/developer/article/1640047
标签:python,图片,转换,素描画
0
投稿
猜你喜欢
一文让你秒懂精通pip并快速体验深度学习应用【建议收藏】
2022-07-08 22:27:56
Python 200行代码实现一个滑动验证码过程详解
2022-08-24 18:24:35
centos下mysql主从复制设置详解
2024-01-20 23:27:06
sqlserver数据库优化解析(图文剖析)
2024-01-17 08:55:37
gchart:基于google图表API的jquery组件全攻略:1、入门
2010-01-25 12:18:00
通用SQL存储过程分页以及asp.net后台调用的方法
2024-01-29 05:03:58
python编写简易聊天室实现局域网内聊天功能
2023-08-30 07:45:52
PHP和JAVA中的重载(overload)和覆盖(override) 介绍
2023-11-01 00:14:33
python实现查找两个字符串中相同字符并输出的方法
2023-09-01 04:03:42
一文掌握Go语言并发编程必备的Mutex互斥锁
2024-05-13 10:43:30
PyCharm使用最多也最常用默认快捷键分享
2023-05-23 20:54:55
在Ubuntu/Linux环境下使用MySQL开放/修改3306端口和开放访问权限
2024-01-14 00:47:02
想取消错误的mysql命令怎么办?
2024-01-13 07:39:38
Go语言学习之反射的用法详解
2023-06-16 06:40:40
Python OrderedDict的使用案例解析
2021-11-20 22:47:25
利用MySQL主从配置实现读写分离减轻数据库压力
2024-01-18 10:35:07
移动测试开发Mitmproxy用于测试抓包神器详解
2023-08-30 23:34:55
sql2005 日志清理 SQL2005压缩清除日志的方法
2024-01-28 12:59:25
豆瓣可以做而且值得做的几件事情
2009-04-24 12:07:00
python pandas库的安装和创建
2023-09-25 22:49:02