python如何将图片转换素描画


Posted in Python onSeptember 08, 2020

代码如下

# -*- 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)

效果如下

python如何将图片转换素描画

以上就是python如何将图片转换素描画的详细内容,更多关于python图片转换素描画的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
python中string模块各属性以及函数的用法介绍
May 30 Python
python使用代理ip访问网站的实例
May 07 Python
Django实现登录随机验证码的示例代码
Jun 20 Python
python实现微信防撤回神器
Apr 29 Python
基于python实现高速视频传输程序
May 05 Python
用uWSGI和Nginx部署Flask项目的方法示例
May 05 Python
Jacobi迭代算法的Python实现详解
Jun 29 Python
OpenCV 轮廓检测的实现方法
Jul 03 Python
django之状态保持-使用redis存储session的例子
Jul 28 Python
python 哈希表实现简单python字典代码实例
Sep 27 Python
pytorch中的weight-initilzation用法
Jun 24 Python
用Python实现一个打字速度测试工具来测试你的手速
May 28 Python
Python自动化之UnitTest框架实战记录
Sep 08 #Python
Python Opencv实现单目标检测的示例代码
Sep 08 #Python
python获取本周、上周、本月、上月及本季的时间代码实例
Sep 08 #Python
Python 使用Opencv实现目标检测与识别的示例代码
Sep 08 #Python
Python requests接口测试实现代码
Sep 08 #Python
Python unittest装饰器实现原理及代码
Sep 08 #Python
Python selenium环境搭建实现过程解析
Sep 08 #Python
You might like
理解php原理的opcodes(操作码)
2010/10/26 PHP
PHP函数学习之PHP函数点评
2012/07/05 PHP
php中static 静态变量和普通变量的区别
2016/12/01 PHP
laravel 去掉index.php伪静态的操作方法
2019/10/12 PHP
php 比较获取两个数组相同和不同元素的例子(交集和差集)
2019/10/18 PHP
javascript 清除输入框中的数据
2009/04/13 Javascript
js 火狐下取本地路径实现思路
2013/04/02 Javascript
js阻止冒泡及jquery阻止事件冒泡示例介绍
2013/11/19 Javascript
深入理解JavaScript是如何实现继承的
2013/12/12 Javascript
js 数组操作之pop,push,unshift,splice,shift
2014/01/29 Javascript
JavaScript中的单引号和双引号报错的解决方法
2014/09/01 Javascript
分享我的jquery实现下拉菜单心的
2015/11/29 Javascript
JQuery学习总结【二】
2016/12/01 Javascript
微信小程序 限制1M的瘦身技巧与方法详解
2017/01/06 Javascript
Angular 2使用路由自定义弹出组件toast操作示例
2019/05/10 Javascript
vue cli3 调用百度翻译API翻译页面的实现示例
2019/09/13 Javascript
微信小程序录音实现功能并上传(使用node解析接收)
2020/02/26 Javascript
微信小程序实现点击导航条切换页面
2020/11/19 Javascript
[04:15]DOTA2-DPC中国联赛 正赛 Ehome vs Aster 选手采访
2021/03/11 DOTA
深入理解NumPy简明教程---数组2
2016/12/17 Python
python3+PyQt5泛型委托详解
2018/04/24 Python
python3.x 将byte转成字符串的方法
2018/07/17 Python
Python基于内置库pytesseract实现图片验证码识别功能
2020/02/24 Python
Python xlrd/xlwt 创建excel文件及常用操作
2020/09/24 Python
Selenium关闭INFO:CONSOLE提示的解决
2020/12/07 Python
html5 Canvas画图教程(8)—canvas里画曲线之bezierCurveTo方法
2013/01/09 HTML / CSS
美国滑板店:Tactics
2020/11/08 全球购物
关爱留守儿童倡议书
2014/04/15 职场文书
优秀家长事迹材料
2014/05/17 职场文书
运动会方队口号
2014/06/07 职场文书
税务干部个人整改措施思想汇报
2014/10/10 职场文书
2014年机关作风建设工作总结
2014/10/23 职场文书
我的生日感言
2015/08/03 职场文书
使用python+pygame开发消消乐游戏附完整源码
2021/06/10 Python
浅析Python实现DFA算法
2021/06/26 Python
vue项目配置sass及引入外部scss文件
2022/04/14 Vue.js