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

Simple drawing board implementation method for Python image processing

coldplay.xixi
Release: 2020-08-07 15:55:23
forward
3020 people have browsed it

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()
Copy after login

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!

source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template