明霞山资源网 Design By www.htccd.com

本文实例为大家分享了python openCV自制绘画板的具体代码,供大家参考,具体内容如下

import numpy as np
import cv2
def nothing(x):
  pass
cv2.namedWindow('image')
img = np.zeros((512,512,3),np.uint8)
cv2.createTrackbar('R','image',0,255,nothing)
cv2.createTrackbar('G','image',0,255,nothing)
cv2.createTrackbar('B','image',0,255,nothing)
drawing = False
mode = True
ix,iy = -1,-1
def drawing_fragment(event,x,y,flags,param):
  r = cv2.getTrackbarPos('R','image')
  g = cv2.getTrackbarPos('G','image')
  b = cv2.getTrackbarPos('B','image')
  color = (b,g,r)
  global drawing,ix,iy,mode
  if event == cv2.EVENT_LBUTTONDOWN:
    drawing = True
    ix = x
    iy = y
  elif event == cv2.EVENT_MOUSEMOVE:
    if drawing:
      if mode:
        cv2.rectangle(img,(ix,iy),(x,y),color,-1)
      else:
        cv2.circle(img,(x,y),5,color,-1)
  elif event == cv2.EVENT_LBUTTONUP:
    drawing = False

cv2.setMouseCallback('image',drawing_fragment)
while True:
  cv2.imshow('image',img)
  k = cv2.waitKey(2)
  if k == 27:
    break
  elif k == ord('q'):
    mode = not mode
cv2.destroyAllWindows()

测试效果

python openCV自制绘画板

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
python,画板

明霞山资源网 Design By www.htccd.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
明霞山资源网 Design By www.htccd.com