WeChat jump python Automatic Code explanation 1.0, python1.0

Source: Internet
Author: User

Next Hop python Automatic Code explanation 1.0, python1.0

The automatic hop code is as follows:

There are several python files in the "plug-in" of the hop-on python, one of which is obtained, and the mouse clicks twice on the image. The python window will print the mouse position twice, and will keep up with the distance between the two points in the line.

In this function, I first remove the screenshot and then explain how to calculate the distance between two clicks on an image.

First, we need to use the graphics module, PIL:

from PIL import Imageimg = Image.open('0.jpg')

Then, use the matplotlib module to provide a plot object:

import matplotlib.pyplot as pltfig = plt.figure()

Add the tag of the opened image to this object:

plt.imshow(img, animated = True)

Then use the canvas. mpl_connect function of matplotlib to connect the clicked action with the image. The second parameter of this function should be written by ourselves.

fig.canvas.mpl_connect('button_press_event', on_press)

In this custom on_press function, we need to get two points and then calculate the distance.
Then we need to have variables to store two points, temporary storage points, to calculate the number of clicks, horizontal and vertical coordinates
Use the global variables cor = [0, 0], coords = [], click_count = 0, ix, iy

global ix,iy global click_count global cor ix,iy = event.xdata, event.ydata coords = [] coords.append((ix,iy)) print("now = ", coords) cor.append(coords) click_count += 1

Store the vertex in the temporary coords, print the current position, and then put the temporary vertex in the global variable cor. The number of clicks is + 1.

 if click_count > 1:  click_count = 0  cor1 = cor.pop()  cor2 = cor.pop()  distance = (cor1[0][0] - cor2[0][0]) **2 + (cor1[0][1] - cor2[0][1]) **2  distance = distance ** 0.5  print("distance = ", distance)

When the number of clicks is greater than 1, it indicates that two points have been saved.
Here we use the stack pop () method to get two vertices, which are respectively placed in cor1 and cor2, so cor1 and cor2 are two vertices.
Then calculate the distance.

Complete code:

import numpy as npfrom matplotlib.animation import FuncAnimationimport matplotlib.pyplot as pltfrom PIL import Imagedef on_press(event): global ix,iy global click_count global cor ix,iy = event.xdata, event.ydata coords = [] coords.append((ix,iy)) print("now = ", coords) cor.append(coords) click_count += 1 if click_count > 1:  click_count = 0  cor1 = cor.pop()  cor2 = cor.pop()  distance = (cor1[0][0] - cor2[0][0]) **2 + (cor1[0][1] - cor2[0][1]) **2  distance = distance ** 0.5  print("distance = ", distance)cor = [0,0]click_count = 0fig = plt.figure()img = Image.open('0.jpg')#updata = Trueplt.imshow(img, animated= True)fig.canvas.mpl_connect('button_press_event', on_press)plt.show()

Final effect:

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.