Friend "Make a tool to pick up an appropriate image file from a folder containing a large number of image files, copy it to the same level as the folder, and then rename the image file to the folder name." I'm crazy
Each folder such as "Kizuna AI" and "Mirai Akari" contains a large number of images and has a file name such as 001 ~.
Target folder
images
├ KizunaAI
│ ├ 001.jpg
│ └ ....ipg
│
├ MiraiAkari
│ ├ 001.jpg
│ └ ....ipg
└ ....
Copy an appropriate image file from each image folder, place it in the same hierarchy as the folder, and rename the file name to the name of the folder to which it belonged.
Target folder
images
├ KizunaAI
│ ├ 001.jpg
│ └ ....ipg
├ KizunaAI.jpg
├ MiraiAkari
│ ├ 001.jpg
│ └ ....ipg
├ MiraiAkari.jpg
└ ....
that's all! !! !! !!
I implemented it in my Anaconda environment for the time being. I said, "I've been asking for it again, so I wonder what kind of serious content it is."
main.py
from PIL import Image
import os, glob
rootpath = "./images"
files = os.listdir(rootpath)
#Get all directories under rootpath
dirs = [f for f in files if os.path.isdir(os.path.join(rootpath, f))]
#Since it's a big deal, it's displayed on the console
print(dirs) # ['dir1', 'dir2']
#Run for all directories
for dir in dirs :
#Scan all files
files = glob.glob(rootpath +"/"+ dir + "/*.*")
#Pull out the extension
targetFile = files[0]
root, ext = os.path.splitext(targetFile)
#Image open
img = Image.open(targetFile)
#Rename and save directly under the root path
img.save(rootpath + '/' + dir + ext)
Anaconda is a little annoying, so I will make it EXE and make it work with one click
It is a great tool that makes an EXE for each Anaconda virtual environment. In other words, it seems that even people who do not have a Python environment can run it with this. I'm in trouble to spit out an EXE that exceeds 300MB for a code that is less than 1KB, but I forgive it because it is convenient.
AnacondaPronpt
conda install -c conda-forge pyinstaller
As an aside, I prefer to install with conda.
Be hurry up
AnacondaPronpt
pyinstaller main.py --onefile
I "done (not saying no bugs)" Friend "I'll do it!"
My friend "If there is a" [] "in the folder name, it won't work, but that's it." I "Fa !?" My friend "Well, I'll do it manually because there are only a few." I "Gusei"
If I have time, I will post this bug-fixed version as well.
Recommended Posts