Python (88) CHROMEBOOK and Python Cut out 100 images into squares | TECH+ Mynavi News Mynavi

Python (88) CHROMEBOOK and Python Cut out 100 images into squares | TECH+ Mynavi News Mynavi

I cut out 100 images into squares in the Python of Chromebook

The compatibility with Linux has been improved by upgrading ChromeOS

It was 2020 that introduced how to set up Linux to Chromebook in the 67th series of this series and move Python.Since then, Chromebook's OS "ChromeOS" has been steadily upgraded, improving the affinity between Linux and ChromeOS, making it easier to use.Previously, it was troublesome to access the web server and Linux files on Linux, but it was no longer necessary.

The Chromebook I use is a 10 -inch IDEAD DUET purchased for around 40,000 yen in 2020.While looking at the materials in the browser, the Python on Linux can be used without any problems.Since the specifications are not so high, there are situations where the main machine feels a little short of power, but it is practical enough if you just move a simple script.It has been a long time since the purchase, but it is usually useful because it can be carried in a small bag.

The only dissatisfaction point is the Japanese input on the Linux side.Of course, if you install IME on the Linux side, you can use Japanese input, but the settings are complicated and you can not set it well, and there are many troubles.That said, there is already a solid IME on the ChromeOS side.Therefore, in situations where text is required, a text editor installed on the ChromeOS side can be used.

Therefore, the creation of programs and sentences will be done on the ChromeOS side, and if you use Linux to run the Python or Linux program, you can use it comfortably.Linux can make up for the unsatisfactory of ChromeOS, and you can supplement the problems on the Linux side with ChromeOS.

Procedure to enable Linux on Chromebook

In addition, it is a method of enabling Linux on Chromebook, but the OS has been upgraded and the procedure has changed a little from the time I introduced last time.So, here's how to easily install Linux on Chromebook.The latest version 97 at the time of writing the manuscript.0.4692.I used 102.

First, open the "Settings App" from the app from the list of apps.

Then, tap "Detailed Settings" on the left side of the screen of the setting app.Next, tap the [turn on] button next to the "Linux development environment" in the developer.

Then, the dialog "Set up the Linux development environment" opens, so press the [Next] button at the bottom right.After checking the next setting screen, tap the "Install" button.Then, the image of Linux is downloaded and the setup is easily completed.

そして、アプリの一覧から「Linuxアプリ > ターミナル」を起動すると、仮想マシンのLinux(Debian)が起動する。

ゼロからはじめるPython(88) ChromebookとPythonで100枚の画像を正方形に切り抜こう | TECH+ マイナビニュース マイナビ

Start the "File" app in ChromeOS and tap the "Linux file" on the left side of the screen to access files in the Linux environment.

Make an empty file with "Touch file name" with Linux, and open the file with the text editor on the ChromeOS side to edit it.The editor I use is a Chrome app called "Text" installed from the Chrome web store.

Resize 100 image files and cut out into a square

The story of Chromebook has become longer, but let's make this program.Since it is a Python program that runs on a multi -platform, it can actually be operated on Win/Mac/Linux, and any OS.

This time, we make a program that resizes JPEG images and cut out into a square.Most of the photos taken with a smartphone camera have a rectangular ratio of 3: 4.However, when posting on a website, there are many situations where it is easy to place design in terms of design.Therefore, the program to be created this time is to cut out 100 images to 640x640 pixels in the Python program for the WEB post.

Here, let's use a Pillow module for image processing.Chromebook has Python installed from the beginning, but does not have an environment for building the PIP command or package of the package manager.So, run the following command to install the necessary packages.

# ChromebookのLinuxを使う場合sudo apt updatesudo apt install python3-pip build-essential libssl-dev libffi-dev python3-dev libjpeg-dev zlib1g-dev

Also, in many Python environments, Pillow is installed from the beginning, but it is not installed on Chromebook.Execute the following command and install Pillow.

# Pillowのインストールpython3 -m pip install Pillow

And the following program is a program that cuts 100 image files into 640x640 square.

import os, globfrom PIL import Image# 画像ディレクトリを指定 --- (*1)in_dir = './in_images'out_dir = './out_images'if not os.path.exists(out_dir): os.mkdir(out_dir)# JPEGファイルを列挙して処理する --- (*2)for file in glob.glob(in_dir+'/*.jpg'): img = Image.open(file) # 画像ファイルを読む # 正方形に切り抜く --- (*3) w, h = img.size # サイズを得る r = h if w < h: r = w img = img.crop(((w - r) // 2, (h - r) // 2,(w + r) // 2, (h + r) // 2)) # リサイズ --- (*4) img = img.resize((640, 640)) # ファイルに保存 --- (*5) savename = os.path.basename(file) img.save(os.path.join(out_dir, savename)) print("saved:", savename)

Let's check the program first.( * 1) specifies the directory of the read image and the output image.( * 2) lists a list of read images.Use Pillow to read the image file and cut out into a square with ( * 3).It is processed to resize to 640x640 pixels with ( * 4) and save it in ( * 5) to the directory of the output image.

To execute the program, copy the image file to the same directory folder as the program.

Then, to execute, execute the following command from the terminal.

python3 resize.py

Then, an image cut out into a square is generated below a folder called out_images.

Next, if you create a program that displays the following HTML, and execute it, HTML will be created, so you can check the image list in the browser.

import globhtml = ""for f in glob.glob("out_images/*.jpg"): html += ""open("a.html","wt").write(html)

summary

This time, I created a program that resizes 100 images into a square and resizes a specified size while explaining how to execute Python on Chromebook.Of course, this kind of work can be cut out by hand using image editing software, but if there are 100 images, it will take a huge amount of time.

For the benchmark, "Time Python3 RESIZE.When I executed "Py", 16.In 3 seconds, 100 images (JPEG images of about 2MB per sheet) were cut out.(Machine specs are CPU: Helio P60T/2GHz, 4GB of memory.) Even though it is a cheap mobile device, it can be moved and moved the image processing program on top of it, so in a good era in a good era.I think it has become.