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.
OS:Ubuntu 20.04LTS Language: Python 3.8.2 The version of Pillow is 7.0.0.
Use this when you want to divide a horizontally long image in half vertically. Use it when reading spread materials on your smartphone.
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)
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.
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