Accumulation in the sense of memorandum and OUTPUT (this is the end of the system created at once)
There were times when I wanted to convert all the working sizes, so I created it to simplify my work.
・ Windows10 ・ Anaconda3 ・ Python3.7 ・ Jupyter Notebook
① Enter the name of the folder you want to resize (the directory one above png_folder) ② If there is no folder, create resize_folder in current_folder ③ Specify the width and height and resize the image (Error will occur if no input is made) ④ If the same folder name already exists, an error will occur to prevent erroneous operation.
All Necessary Libraries.py
import numpy as np
import os
import pathlib
from pathlib import Path
from glob import glob
from PIL import Image
from tqdm import tqdm
PG
image_resize_code
#Enter the folder name
folder_name = input('Enter the folder name :')
#get path
p = Path('C:/Users/H3051411/OUT/' + folder_name + '/_png_folder')
new_folder_name = '_resize_folder'
new_folder_path = os.path.join(p, new_folder_name)
#Get new path
new_p = Path(new_folder_path)
#If the folder does not exist, resize it
if not os.path.exists(new_folder_path):
#Create a new folder
os.makedirs(new_folder_path)
#Specifying width and height
width = int(input('withsize_input: '))
height = int(input('height_size_input: '))
#Get files in list
files = list(p.glob('*.*'))
#Resize process
for f in tqdm(files):
#Get image file
img = Image.open(f)
#Resize processing with the highest quality
img_resize = img.resize((width, height), Image.LANCZOS)
#Get the file name
imgname = os.path.basename(f)
# print(imgname) #For confirmation
#Set new file name
newfname ='resize_' + imgname
# print(newfname) #For confirmation
#Save the file(path specification)
img_resize.save(new_p/newfname)
else:
#Returns Error if the file exists
print('Error:resize_folder already exists.')
・ Not functionalized ・ 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