[Ev3dev] Create a program that captures the LCD (screen) using python

Introduction

This article assumes that you have ev3dev installed on your EV3 and have an SSH connection. If you have not built the environment, please refer to this article.

mindstorm-Let's control EV3 with Linux! Install ev3dev OS and SSH connection

What to make

Create a program that captures the screen (LCD) of the intelligent block of mindstorm-EV3 and saves it as an image. Use Python's ev3dev API ev3dev-lang-python.

Please refer to here for the environment construction of ev3dev-lang-python.

Let's control EV3 motors and sensors with Python

program

screenshot.py


#!/usr/bin/env python3


import sys
from subprocess import call
#Library for handling image data
from PIL import Image

#Image name to save, by default"screenshot.png "
out_name = sys.argv[1] if len(sys.argv) > 1 else "screenshot.png "

#Capture the framebuffer and convert it to a PNG image
call(["fbgrab", out_name]);

#Load the screenshot image
image = Image.open(out_name)

#Convert image to RGB format
image = image.convert("RGB")

#Format image
image = image.resize(tuple(i * 2 for i in image.size), Image.NEAREST)


pixel_data = image.load()

#Image color conversion
for y in range(image.size[1]):
    for x in range(image.size[0]):
        if pixel_data[x, y] == (255, 255, 255):
            pixel_data[x, y] = (173, 181, 120)

# Save the image again
image.save(out_name);

call(["fbgrab", out_name])The screenshot image obtained by is a black and white image, so you need to convert the white pixels to light green to represent the true screen color. The following part of the program realizes this process.

for y in range(image.size[1]):
    for x in range(image.size[0]):
        if pixel_data[x, y] == (255, 255, 255):
            pixel_data[x, y] = (173, 181, 120)

Run

You can specify the save destination image file in the first argument.

robot@ev3dev:$ python screenshot.py test.png

I got an image like this test.png

Recommended Posts

[Ev3dev] Create a program that captures the LCD (screen) using python
A program that plays rock-paper-scissors using Python
[Python] A program that rounds the score
[Python] A program that counts the number of valleys
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 1 ~
[Python] A program that finds the minimum and maximum values without using methods
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 2 ~
[Python] A program that compares the positions of kangaroos.
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 3 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 4 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 5 ~
There is a pattern that the program did not stop when using Python threading
[Python] A program that finds the most common bird types
Create a python GUI using tkinter
[Python] A program that rotates the contents of the list to the left
Create a compatibility judgment program with the random module of python.
[Python] A program that creates stairs with #
Create a graph using the Sympy module
[Python] Create a Batch environment using AWS-CDK
[Python] Create a LineBot that runs regularly
[Python] I tried to make a simple program that works on the command line using argparse.
[Python] A program that calculates the number of chocolate segments that meet the conditions
[Python] A program that calculates the number of socks to be paired
Create a record with attachments in KINTONE using the Python requests module
Create a GIF file using Pillow in Python
A program that removes duplicate statements in Python
Create a web map using Python and GDAL
Python: Create a class that supports unpacked assignment
Create a Mac app using py2app and Python3! !!
Create a MIDI file in Python using pretty_midi
Create a GUI on the terminal using curses
A shell program that displays the Fibonacci sequence
[Python] A program that calculates the difference between the total numbers on the diagonal line.
[Python] A program that calculates the number of updates of the highest and lowest records
A story that was convenient when I tried using the python ip address module
Create a Python module
Cut a part of the string using a Python slice
A note on the library implementation that explores hyperparameters using Bayesian optimization in Python
[Python] A program that finds the shortest number of steps in a game that crosses clouds
[Python] Create a ValueObject with a complete constructor using dataclasses
A program that summarizes the transaction history csv data of SBI SECURITIES stocks [Python3]
In Python, create a decorator that dynamically accepts arguments Create a decorator
Python program that looks for the same file name
[Ev3dev] Let's understand the mechanism of LCD (screen) control
A memo that I touched the Datastore with python
[Python] Mask the image into a circle using Pillow
Create a Python environment
Try to create a waveform (audio spectrum) that moves according to the sound with python
[Python] Create a Tkinter program distribution file with cx_Freeze
A note that runs an external program in Python and parses the resulting line
Using a Python program with fluentd's exec_filter Output Plugin
Create a company name extractor with python using JCLdic
[Python / C] I made a device that wirelessly scrolls the screen of a PC remotely.
A Python program that converts ical data into text
A little bit from Python using the Jenkins API
Create a dictionary by searching the table using sqlalchemy
Using a python program with fluentd's exec Output Plugin
Create a web page that runs a model that increases the resolution of the image using gradio, which makes it easy to create a web screen
I wrote a Python script that exports all my posts using the Qiita API v2
A story that makes it easy to estimate the living area using Elasticsearch and Python
A program that automatically resizes the iOS app icon to the required image size in Python