I wrote a python script that crops the specified coordinates from many images.
When I screen the web conference screen, I do a full screen screenshot so as not to be late for the discussion, but when I share it with people later, I want to erase unnecessary information on the screen such as the toolbar, so I made it. If it's about a few, you can do it with a Mac preview, but it's often on the order of about 100, so I thought it would be convenient if you could do it all at once with a program.
macOS Catalina 10.15.7 python 3.8.0
pip install Pillow
This time I will use Pillow. You can also do it with OpenCV.
# -*- coding: utf-8 -*-
import sys
from PIL import Image
import os
#Image storage location before cropping
ORIGINAL_FILE_DIR = "./org_data/"
#Storage destination of cropped image
TRIMMED_FILE_DIR = "./cut_data/"
#Returns a cropped image object, specifying the image path, upper left and lower right coordinates.
def trim(path, left, top, right, bottom):
im = Image.open(path)
im_trimmed = im.crop((left,top,right,bottom))
return im_trimmed
if __name__ == '__main__':
#If there is no storage destination for the cropped image, create it
if os.isdir(TRIMMED_FILE_DIR) == False:
os.makedirs(TRIMMED_FILE_DIR)
#Upper left coordinates to trim
left, top = 0, 50
#Top right coordinates to trim
right, bottom = 2880, 1800
#Get image file name
files = os.listdir(ORIGINAL_FILE_DIR)
#Only files with a specific extension are used. Match the extension of the file to be actually processed
files = [name for name in files if name.split(".")[-1] in ["png","jpg"]]
for val in files:
#Path to the original image
path = ORIGINAL_FILE_DIR + val
#Get a cropped image object
im_trimmed = trim(path, left, top, right, bottom)
#Save to the trimmed directory. At the beginning of the file name"cut_"Is on
im_trimmed.save(TRIMMED_FILE_DIR+"cut_"+val, quality=95) #quality is not recommended if it is greater than 95
When actually using it, prepare the storage destination of the image before and after trimming, and specify the directory with a relative path in the source code. Then run the program.
It is troublesome to select the area each time when taking a screenshot, so I think that there is a considerable demand for full-screen screenshots and cutting later. I screened the web conferencing screen, trimmed it this way, and then pasted it into PowerPoint with python. I hope it helps you.
Recommended Posts