The story of making a tool to load an image with Python ⇒ save it as another name

Overview

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

specification

1. 1. Suppose you have a folder like this

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
 └ ....

2. Do it this way

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! !! !! !!

Implementation

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." キャプチャ.PNG

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)

Make it an EXE

Anaconda is a little annoying, so I will make it EXE and make it work with one click

1. 1. Install Pyinstaller

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.

2. Make it an EXE

Be hurry up

AnacondaPronpt


pyinstaller main.py --onefile

I "done (not saying no bugs)" Friend "I'll do it!"

Other

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

The story of making a tool to load an image with Python ⇒ save it as another name
Save the result of the life game as a gif with python
The story of making Python an exe
The story of making a standard driver for db with python.
The story of making a module that skips mail with python
The story of making a university 100 yen breakfast LINE bot with Python
Save screenshot of [Python] [Windows] screen as an image
How to save the feature point information of an image in a file and use it for matching
A memo of misunderstanding when trying to load the entire self-made module with Python3
Don't take an instance of a Python exception class directly as an argument to the exception class!
I tried to find the entropy of the image with python
[python] Change the image file name to a serial number
A story about an amateur making a breakout with python (kivy) ②
A story about an amateur making a breakout with python (kivy) ①
The story of making a question box bot with discord.py
Get the source of the page to load infinitely with python.
Return the image data with Flask of Python and draw it to the canvas element of HTML
I want to cut out only the face from a person image with Python and save it ~ Face detection and trimming with face_recognition ~
[Python] If you create a file with the same name as the module to be imported, an Attribute Error will occur.
A story that struggled to handle the Python package of PocketSphinx
Try to extract a character string from an image with Python3
Prepare a distributed load test environment with the Python load test tool Locust
[Python] What is a slice? An easy-to-understand explanation of how to use it with a concrete example.
Find the white Christmas rate by prefecture with Python and map it to a map of Japan
A story about a person who uses Python addicted to the judgment of an empty JavaScript dictionary
The story of making an immutable mold
The story of blackjack A processing (python)
How to crop the lower right part of the image with Python OpenCV
Try to image the elevation data of the Geographical Survey Institute with Python
[Introduction to Python] How to sort the contents of a list efficiently with list sort
[Python] Create a linebot to write a name and age on an image
The story of making a sound camera with Touch Designer and ReSpeaker
The result of making the first thing that works with Python (image recognition)
Start the webcam to take a still image and save it locally
[python] Send the image captured from the webcam to the server and save it
Be careful of the type when making an image mask with Numpy
I ran GhostScript with python, split the PDF into pages, and converted it to a JPEG image.
<Python> A quiz to batch convert file names separated by a specific character string as part of the file name
Story of making a virtual planetarium [Until a beginner makes a model with a script and manages to put it together]
How to crop an image with Python + OpenCV
Since python is read as "Pichon", it can be executed with "Pichon" (it is a story)
Save an array of numpy to a wav file using the wave module
Image processing? The story of starting Python for
Crop Numpy.ndarray and save it as an image
The story of making a lie news generator
GAE --With Python, rotate the image based on the rotation information of EXIF and upload it to Cloud Storage.
[Python] I tried to get the type name as a string from the type function
The story of making soracom_exporter (I tried to monitor SORACOM Air with Prometheus)
Post an article with an image to WordPress with Python
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
[Python Data Frame] When the value is empty, fill it with the value of another column.
The story of Airflow's webserver and DAG, which takes a long time to load
How to identify the element with the smallest number of characters in a Python list?
A python beginner tried to intern at an IT company [Day 3: Going to the clouds ...]
The result of making a map album of Italy honeymoon in Python and sharing it
A story about how Windows 10 users created an environment to use OpenCV3 with Python 3.5
An easy way to pad the number with zeros depending on the number of digits [Python]
[Python] You can save an object to a file by using the pickle module.
It was a little difficult to do flask with the docker version of nginx-unit
How to check in Python if one of the elements of a list is in another list