[Python3] Code that can be used when you want to change the extension of an image at once

Accumulation in the sense of memorandum and OUTPUT

Purpose of creation

I regularly wanted to convert all the work extensions, so I created it to simplify my work.

Creation environment

・ Windows10 ・ Anaconda3 ・ Python3.7 ・ Jupyter Notebook

document

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

Loading the library

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.')

Task

・ Not functionalized ・ It will take time if the number of images increases (untested)

Summary

Working time has been reduced from 1 hour to 1 minute. Also, I think there is a better way to write it.

Recommended Posts

[Python3] Code that can be used when you want to change the extension of an image at once
[Python3] Code that can be used when you want to cut out an image in a specific size
[Python3] Code that can be used when you want to resize images in folder units
Python Note: When you want to know the attributes of an object
If you want to make a Windows application (exe) that can be actually used now using only Python
Scripts that can be used when using bottle in Python
[Python] A program to find the number of apples and oranges that can be harvested
[Subprocess] When you want to execute another Python program in Python code
Python standard module that can be used on the command line
When you want to use multiple versions of the same Python library (virtual environment using venv)
[Python] A program that calculates the number of socks to be paired
Be careful of the type when making an image mask with Numpy
Article that can be a human resource who understands and masters the mechanism of API (with Python code)
How to build an environment when you want to use python2.7 after installing Anaconda3
[Python] I want to know the variables in the function when an error occurs!
You will be an engineer in 100 days --Day 29 --Python --Basics of the Python language 5
The story of IPv6 address that I want to keep at a minimum
You will be an engineer in 100 days --Day 33 --Python --Basics of the Python language 8
You will be an engineer in 100 days --Day 26 --Python --Basics of the Python language 3
Summary of scikit-learn data sources that can be used when writing analysis articles
How to install a Python library that can be used by pharmaceutical companies
What you want to memorize with the basic "string manipulation" grammar of python
You will be an engineer in 100 days --Day 32 --Python --Basics of the Python language 7
I want to crop the image along the contour instead of the rectangle [python OpenCV]
When you want to erase characters at once in terminal or line unit
You will be an engineer in 100 days --Day 28 --Python --Basics of the Python language 4
When you want to update the chrome driver.
List of tools that can be used to easily try sentiment analysis of Japanese sentences in Python (try with google colab)
A convenient function memo to use when you want to enter the debugger if an error occurs when running a Python script.
Summary of statistical data analysis methods using Python that can be used in business
Introduction of automatic image collection package "icrawler" (0.6.3) that can be used during machine learning
[Introduction to Python] Basic usage of the library scipy that you absolutely must know
Software that saves the set character string to the clipboard when you click the [Python] button
[Python] You can save an object to a file by using the pickle module.
I want to be notified of the connection environment when the Raspberry Pi connects to the network
Python note: When the pip command cannot be used
Python knowledge notes that can be used with AtCoder
ANTs image registration that can be used in 5 minutes
[Python] tkinter Code that is likely to be reused
[Python] pandas Code that is likely to be reused
I felt that I ported the Python code to C ++ 98.
The programming language you want to be able to use
Limits that can be analyzed at once with MeCab
Simple statistics that can be used to analyze the effect of measures on EC sites and codes that can be used in jupyter notebook
Can be used with AtCoder! A collection of techniques for drawing short code in Python!
Summary of situations where plotly express can be used [When can you use it from matplotlib? ]
How to change the MAC address of an automatically created virtual NIC to be event driven
[Python] When you want to import and use your own package in the upper directory
A Python script that goes from Google search to saving the Search results page at once
Golang: The matter that you want to execute the process at exactly 00:00 or 30 minutes at the specified interval.
Summary of Python implementation know-how and tips that AI engineers want to be careful about
A Python script that allows you to check the status of the server from your browser
How to set variables that can be used throughout the Django app-useful for templates, etc.-
I want to output while converting the value of the type (e.g. datetime) that is not supported when outputting json with python
I tried to make a script that traces the tweets of a specific user on Twitter and saves the posted image at once
Solution when you want to use cv_bridge with python3 (virtualenv)
[Django] A memorandum when you want to communicate asynchronously [Python3]
I tried to find the entropy of the image with python
[Python] Change the Cache-Control of the object uploaded to Cloud Storage
Processing of python3 that seems to be usable in paiza
An introduction to object orientation-let's change the internal state of an object