Home  >  Article  >  Backend Development  >  Simple drawing board implementation method for Python image processing

Simple drawing board implementation method for Python image processing

coldplay.xixi
coldplay.xixiforward
2020-08-07 15:55:022978browse

Simple drawing board implementation method for Python image processing

The example in this article describes the simple sketchpad implementation method of Python image processing. Share it with everyone for your reference, the details are as follows:

Python image processing is also implemented by relying on the Python interface of opencv. The Python language is simple, easy to understand, concise and clear. This time to implement drawing board graffiti, one is to draw rectangles and lines inside. Others can also be expanded. This case is just a routine. The idea is to handle mouse events and adjust the color of the scroll bar. Mouse events include left button press and release event processing.

import cv2
import numpy as np
# null function
def nothing(x):
  pass
Drawing = False
Mode = True
IX,IY = -1,-1
def drawCircle(Event,X,Y,Flags,Param):
  R = cv2.getTrackbarPos('R','Image')
  G = cv2.getTrackbarPos('G','Image')
  B = cv2.getTrackbarPos('B','Image')
#get color value
  Color = (B,G,R);
  global IX,IY,Drawing,Mode
  if Event == cv2.EVENT_LBUTTONDOWN:
    Drawing = True
    IX,IY = X,Y
  elif Event == cv2.EVENT_MOUSEMOVE and Flags == cv2.EVENT_FLAG_LBUTTON:
    if Drawing == True:
      if Mode == True:
        cv2.rectangle(Img,(IX,IY),(X,Y),Color,-1)
      else:
        cv2.circle(Img,(X,Y),3,Color,-1);
  elif Event == cv2.EVENT_LBUTTONUP:
    Drawing = False
#create image with 3 chanels
Img = np.zeros((660,660,3),np.uint8)
#create window
cv2.namedWindow('Image')
#create track bar, range for 0~255
cv2.createTrackbar('R','Image',0,255,nothing)
cv2.createTrackbar('G','Image',0,255,nothing)
cv2.createTrackbar('B','Image',0,255,nothing)
#set mouse ack
cv2.setMouseCallback('Image',drawCircle)
while(1):
  cv2.imshow('Image',Img)
  k = cv2.waitKey(10)&0xFF
  #switch draw mode
  if k == ord('m'):
    Mode = not Mode
  elif k == 27:
    break
#you must destroy all of sources
cv2.destroyAllWindows()

The final rendering is as follows:

Related learning recommendations: python video tutorial

The above is the detailed content of Simple drawing board implementation method for Python image processing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete