Accumulation in the sense of memorandum and OUTPUT
I regularly wanted to convert all the work extensions, so I created it to simplify my work.
・ Windows10 ・ Anaconda3 ・ Python3.7 ・ Jupyter Notebook
① Enter the name of the folder whose extension you want to change ② If there is no folder, create [extension] _folder in current_folder. ③ 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, new_folder_name = Path('C:/Users/H3051411/OUT/' + folder_name), '_png_folder'
#New png from current path_Create folder
new_folder_path = os.path.join(p, new_folder_name)
#print(new_folder_path)(Check if necessary)
#If there is no folder, copy and create
if not os.path.exists(new_folder_path):
#Get the files in the directory
shutil.copytree(p, new_folder_path)
#Convert new path extension to png file
new_p = Path(new_folder_path)
files = list(new_p.glob('*.*'))
for i,f in tqdm(enumerate(files)):
print('Number of image conversions:{0}/{1}'.format(i+1,len(files))
shutil.move(f, f.with_name(f.stem + ".png "))
else:
print('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