欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python如何將圖片轉(zhuǎn)換素描畫

 更新時間:2020年09月08日 11:54:04   作者:崔笑顏  
這篇文章主要介紹了python如何將圖片轉(zhuǎn)換素描畫,幫助大家更好的用python處理圖片,感興趣的朋友可以了解下

代碼如下

# -*- 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('轉(zhuǎn)換中......')
  img_rgb = cv2.imread(src_image_name)
  img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
  # 讀取圖片時直接轉(zhuǎn)換操作
  # 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('轉(zhuǎn)換完成!!!\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如何將圖片轉(zhuǎn)換素描畫的詳細內(nèi)容,更多關于python圖片轉(zhuǎn)換素描畫的資料請關注腳本之家其它相關文章!

相關文章

  • Python實現(xiàn)的tab文件操作類分享

    Python實現(xiàn)的tab文件操作類分享

    這篇文章主要介紹了Python實現(xiàn)的tab文件操作類分享,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下
    2014-11-11
  • python實現(xiàn)連接mongodb的方法

    python實現(xiàn)連接mongodb的方法

    這篇文章主要介紹了python實現(xiàn)連接mongodb的方法,涉及Python使用pymongo模塊的基本技巧,需要的朋友可以參考下
    2015-05-05
  • Python測試開源工具splinter安裝與使用教程

    Python測試開源工具splinter安裝與使用教程

    Splinter是一個使用Python測試Web應用程序的開源工具,可以自動化瀏覽器操作,使用Splinter可以使用pyhton腳本來實現(xiàn),具體安裝及操作方法跟隨小編一起看看吧
    2021-07-07
  • Python random模塊(獲取隨機數(shù))常用方法和使用例子

    Python random模塊(獲取隨機數(shù))常用方法和使用例子

    這篇文章主要介紹了Python random模塊(獲取隨機數(shù))常用方法和使用例子,需要的朋友可以參考下
    2014-05-05
  • 深入了解Django View(視圖系統(tǒng))

    深入了解Django View(視圖系統(tǒng))

    這篇文章主要介紹了簡單了解Django View(視圖系統(tǒng)),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-07-07
  • Python實現(xiàn)多任務版的udp聊天器

    Python實現(xiàn)多任務版的udp聊天器

    這篇文章主要為大家詳細介紹了Python實現(xiàn)多任務版的udp聊天器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • 在python中按照特定順序訪問字典的方法詳解

    在python中按照特定順序訪問字典的方法詳解

    今天小編就為大家分享一篇在python中按照特定順序訪問字典的方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • Python matplotlib生成圖片背景透明的示例代碼

    Python matplotlib生成圖片背景透明的示例代碼

    這篇文章主要介紹了Python matplotlib生成圖片背景透明的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-08-08
  • Python 手動導包的實現(xiàn)

    Python 手動導包的實現(xiàn)

    本文主要介紹了Python 手動導包的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-03-03
  • 對Python 窗體(tkinter)文本編輯器(Text)詳解

    對Python 窗體(tkinter)文本編輯器(Text)詳解

    今天小編就為大家分享一篇對Python 窗體(tkinter)文本編輯器(Text)詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10

最新評論