[PYTHON] Dot according to the image

Overview

Plot the touch coordinates recorded in the log on the corresponding screen image. Since the penance of ... occurred, we will automate it.

The x-axis and y-axis are drawn, and the size of the image also changes. [Python --Plot Python3 coordinates on image | teratail] [5]

I was able to see the plotted image with plt.imshow. ... If you save with>, the number of pixels will change and even an extra axis will be attached, and a margin will appear around it.

On the contrary, is it rare that it is more convenient? Case. As I learned later, it seems that it can be done without Pillow (PIL). [Link] [4]

code

import matplotlib.pyplot as plt
from PIL import Image

#Figure and ax preparation...Routine processing of matplotlib
fig = plt.figure(figsize=(8, 6))
fig.patch.set_facecolor('white') #<--This doesn't have to be. The background color of the entire figure is set, but it will be painted with image.
ax = fig.add_subplot(1, 1, 1)

#If it's an image(0,0)Comes to the upper left,Invert because the positive direction of the y-axis points down_yaxis()
# ax.invert_yaxis()I thought, but I don't need it.

#x and y are the touch coordinates obtained from the log. Here, the upper left, center, and lower right are tentatively specified.
x = [0, 400, 200]
y = [0, 300, 150]

#Plot touch coordinates
ax.scatter(x, y, c='red', s=20)


#Put a screen image in the background
im = Image.open("aaaa.png ")
ax.imshow(im)

#Save the plot image
plt.savefig("aaaa_plot.png ")

Original image (aaaa.png) ・ ・ ・ 400x300 image made appropriately with paint aaaa.png

After plotting (aaaa_plot.png) aaaa_plot.png

Code 2 --No Pillow

import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
import pathlib

#Load the image
f = cbook.get_sample_data(pathlib.Path("aaaa.png ").absolute())
im = plt.imread(f)

#Figure and ax preparation...Routine processing of matplotlib
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(1, 1, 1)

#I don't know why, but ax.invert_yaxis()The y-axis points down without calling

#x and y are the touch coordinates obtained from the log. Here, the upper left, center, and lower right are tentatively specified.
x = [0, 400, 200]
y = [0, 300, 150]

#Plot touch coordinates
ax.scatter(x, y, c='red', s=20)

#Put a screen image in the background
ax.imshow(im)

#Save the plot image
plt.savefig("aaaa_plot2.png ")

Remarks

When I thought that this penance had a feeling of déjà vu, ↓ was close. [Export the part that matches the regular expression to Excel + Plot --Qiita] [2] At this time, it was not necessary to match it with the image + I chose Excel for the convenience of other people viewing the raw data.

reference

[matplotlib --How to display images and arrays with imshow --Pynote] [1] [A very summary of matplotlib-Qiita] [3] [python --matplotlib: Incompatible with plotting coordinates in image imshow --stackoverrun] [4] [AxesDivider — Matplotlib 2.0.2 documentation][6] [1]:https://www.pynote.info/entry/matplotlib-imshow [2]:https://qiita.com/int_main_void/items/f59d9e9f2a8d8f9c6b51 [3]:https://qiita.com/nkay/items/d1eb91e33b9d6469ef51 [4]:https://stackoverrun.com/ja/q/10393572 [5]:https://teratail.com/questions/139505 [6]:https://matplotlib.org/mpl_toolkits/axes_grid/users/axes_divider.html

Recommended Posts

Dot according to the image
Mouse over Matplotlib to display the corresponding image
Dynamically generate sqlalchemy filter according to the conditions
Try adding fisheye lens distortion to the image
Python constants like None (according to the reference)
Crop the image to rounded corners with pythonista
I tried to correct the keystone of the image
The road to Pythonista
The road to Djangoist
Upload the image downloaded by requests directly to S3
Let's apply the brand image color to the matplotlib colormap!
I tried to detect the iris from the camera image
Save the graph drawn by pyqtgraph to an image
Convert the image in .zip to PDF with Python
Display / update the graph according to the input with PySimpleGui
Coloring points according to the distance from the regression curve
Automatically select BGM according to the content of the conversation
Resize the image to the specified size and blacken the margins
Python OpenCV tried to display the image in text.
I tried to compress the image using machine learning
I tried to find the entropy of the image with python
opencv-python Introduction to image processing
[python] Change the image file name to a serial number
How to use the generator
The road to download Matplotlib
Remove the frame from the image
I made a dot picture of the image of Irasutoya. (part1)
Display the time in Django according to the user's region (UTC)
Change the volume of Pepper according to the surrounding environment (sound)
Convert the image data (png) at hand to a .pbm image
I made a dot picture of the image of Irasutoya. (part2)
How to use the decorator
The image is a slug
How to increase the axis
How to start the program
Display the image of the camera connected to the personal computer on the GUI.
[Python] Try to graph from the image of Ring Fit [OCR]
I tried to build the SD boot image of LicheePi Nano
I tried to process the image in "sketch style" with OpenCV
Try to edit a new image using the trained StyleGAN2 model
Consider the speed of processing to shift the image buffer with numpy.ndarray
[Go] Create a CLI command to change the extension of the image
I tried to process the image in "pencil style" with OpenCV
I tried to cut out a still image from the video
Python script for KiCad-Pre-place footprint almost according to the schematic layout
Programming to fight in the world ~ 5-1
Programming to fight in the world ~ 5-5,5-6
How to calculate the autocorrelation coefficient
Road to the Wizard: 4th Step
Mount the volume attached to CentOS 7
Leave the troublesome processing to Python
Programming to fight in the world 5-3
How to use the optparse module
Django1.11.1 Image uploader Easy to stumble
Detect mosaic points in the image
Change the Flyweight pattern to Pythonic (?) (2)
Programming to fight in the world-Chapter 4
Workflow to convert formula (image) to python
In the python command python points to python3.8
How to read the SNLI dataset
How to get the Python version