白云岛资源网 Design By www.pvray.com

代码如下

# -*- 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图片转换素描画的资料请关注其它相关文章!

白云岛资源网 Design By www.pvray.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
白云岛资源网 Design By www.pvray.com

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?