Accumulation in the sense of memorandum and OUTPUT
Code to use when you want to crop all working images
・ Windows10 ・ Anaconda3 ・ Python3.7 ・ Jupyter Notebook
① Enter the name of the folder you want to crop ② If there is no folder, create a crop_folder in the current_folder and crop the image. ③ If the same folder name already exists, an error will occur to prevent erroneous operation.
All Necessary Libraries.py
import pathlib
import os
import shutil
import pprint
import numpy as np
from glob import glob
from PIL import Image
from tqdm import tqdm
from pathlib import Path
PG
change_pngextension_code
#Enter the folder name
folder_name = input('Enter the folder name :')
p = Path('C:/Users/H3051411/OUT/' + folder_name)
new_folder_name = '_crop_folder'
#New png from current path_Create folder
new_folder_path = os.path.join(p, new_folder_name)
#If there is no folder, copy and create
if not os.path.exists(new_folder_path):
#Get the files in the directory
os.makedirs(new_folder_path)
#Convert new path extension to png file
new_p = Path(new_folder_path)
files = list(p.glob('*.*'))
for i,f in tqdm(enumerate(files)):
print('Number of image crops:{0}/{1}'.format(i+1,len(files)))
img = Image.open(f)
imgname = os.path.basename(f)
newfname = 'crop_' + imgname
# .crop((Upper left x,Upper left y,Lower right x,Lower right y))Specified by.
#The upper left coordinates are(x, y) = (left, upper), The lower right coordinates are(x, y) = (right, lower)Corresponds to.
img_resize = img.resize((634, 9606), Image.LANCZOS)
img_resize.crop((0, 0, 634, 935)).save(new_p/newfname, quality=95)
else:
print('Folder already exists.')
・ Not functionalized ・ Low versatility because it is cut out to a specific size ・ It will take time if the number of images increases (untested)
Working time has been reduced from 1 hour to 1 minute. Also, I think there is a better way to write it.
Recommended Posts