I wrote a script that splits the image in two

Introduction

I was studying Python at AtCoder. As I learned how to write it little by little, I wanted to write a practical script, so I decided to make what I needed.

environment

OS:Ubuntu 20.04LTS Language: Python 3.8.2 The version of Pillow is 7.0.0.

Purpose / use

Use this when you want to divide a horizontally long image in half vertically. Use it when reading spread materials on your smartphone.

code

imagehalf


import glob
from PIL import Image
import os

files = glob.iglob('/home/user/images/*.jpg')

for f in files:
    img = Image.open(f)
    x, y = img.size

    box = img.crop((0, 0, x//2, y))
    title, ext = os.path.splitext(f)
    box.save(title + 'half1' + ext, quality=75)

    box = img.crop((x//2+1, 0, x, y))
    title, ext = os.path.splitext(f)
    box.save(title + 'half2' + ext, quality=75)

    os.remove(f)

Code description

Create a directory called "images" on your home directory and use it for work. Detects all files with the extension .jpg stored in images. Starting from the upper left of the image, get the intermediate coordinates in the x-axis direction and divide the image in half. Save the left side of the image divided in half as "original file name + half1 + extension". Save the right side of the image divided in half as "original file name + half2 + extension". Delete the original image.

※Caution I think that the script file will work if it is placed in the same level as the working directory and in the home directory. The image file from which it was split will be deleted, not the Trash. It cannot be revived.

in conclusion

Since this is the first script I created, the writing style and environment settings may be incorrect. If you have a smarter way of writing, please point it out. Please forgive the responsibility of this script.

Recommended Posts

I wrote a script that splits the image in two
A memo that I wrote a quicksort in Python
I wrote a PyPI module that extends the parameter style in Python's sqlite3 module
I wrote a script to revive the gulp watch that will die soon
I wrote a script to get a popular site in Japan
I wrote a script to combine the divided ts files
I wrote the queue in Python
A Python script that compares the contents of two directories
I wrote a Python script that exports all my posts using the Qiita API v2
I made a program that solves the spot the difference in seconds
I wrote a function to load a Git extension script in Python
I wrote a script to extract a web page link in Python
Cut out A4 print in the image
I wrote the sliding wing in creation.
AtCoder writer I wrote a script to aggregate the contests for each writer
I wrote a corpus reader that reads the results of MeCab analysis
I wrote a class in Python3 and Java
A program that searches for the same image
I wrote a design pattern in kotlin Prototype
I wrote a script to upload a WordPress plugin
I wrote the hexagonal architecture in go language
I wrote a Japanese parser in Japanese using pyparsing.
I made a LINE BOT that returns a terrorist image using the Flickr API
Note that I understand the least squares algorithm. And I wrote it in Python.
I wrote a design pattern in kotlin Factory edition
I wrote a design pattern in kotlin Builder edition
I wrote a design pattern in kotlin Singleton edition
I wrote a design pattern in kotlin Adapter edition
I made a script to put a snippet in README.md
I wrote a design pattern in kotlin, Iterator edition
Try loading the image in a separate thread (OpenCV-Python)
What's in that variable (when running a Python script)
A memo that I touched the Datastore with python
I wrote a design pattern in kotlin Template edition
Note that I was addicted to npm script not passing in the verification environment
I replaced the Windows PowerShell cookbook with a python script.
I wrote python in Japanese
I tried the super-resolution algorithm "PULSE" in a Windows environment
Mezzanine introduction memo that I got stuck in the flow
A Python script that saves a clipboard (GTK) image to a file.
A script that opens the URLs written in CSV in order and takes a full screen screenshot
I wrote the basic operation of Seaborn in Jupyter Lab
Let's create a script that registers with Ideone.com in Python.
A note for embedding the scripting language in a bash script
Note 2 for embedding the scripting language in a bash script
A word that I was interested in as a programming beginner
Creating a Python script that supports the e-Stat API (ver.2)
I wrote a code that exceeds 100% recovery rate in horse racing prediction using LightGBM (Part 2)
I tried "a program that removes duplicate statements in Python"
Process the files in the folder in order with a shell script
I made a dot picture of the image of Irasutoya. (part1)
I wrote it in Go to understand the SOLID principle
I tried to create a Python script to get the value of a cell in Microsoft Excel
Is there a bias in the numbers that appear in the Fibonacci numbers?
I wrote the basic operation of Numpy in Jupyter Lab.
I wrote a doctest in "I tried to simulate the probability of a bingo game with Python"
A set of script files that do wordcloud in Python3
A script that displays the running CloudFormation stack like a mannequin
I tried to make a script that traces the tweets of a specific user on Twitter and saves the posted image at once
I made a Line bot that guesses the gender and age of a person from an image
I analyzed the image of the Kyudo scoring book (a booklet that records the results of the hits). (Google Colaboratory)