[Python3] Code that can be used when you want to resize images in folder units

Accumulation in the sense of memorandum and OUTPUT (this is the end of the system created at once)

Purpose of creation

There were times when I wanted to convert all the working sizes, so I created it to simplify my work.

Creation environment

・ Windows10 ・ Anaconda3 ・ Python3.7 ・ Jupyter Notebook

document

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

Loading the library

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

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 resize images in folder units
[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 change the extension of an image at once
Scripts that can be used when using bottle in Python
[Subprocess] When you want to execute another Python program in Python code
I want to create a priority queue that can be updated in Python (2.7)
If you want to make a Windows application (exe) that can be actually used now using only Python
I want to be able to run Python in VS Code
Python standard input summary that can be used in competition pro
Functions that can be used in for statements
I made a familiar function that can be used in statistics with Python
How to install a Python library that can be used by pharmaceutical companies
I want to write in Python! (1) Code format check
Basic algorithms that can be used in competition pros
Japanese can be used with Python in Docker environment
Python knowledge notes that can be used with AtCoder
ANTs image registration that can be used in 5 minutes
Can be used in competition pros! Python standard library
[Python] pandas Code that is likely to be reused
When you want to plt.save in a for statement
I want to do something in Python when I finish
Convert images from FlyCapture SDK to a form that can be used with openCV
Summary of statistical data analysis methods using Python that can be used in business
[Python] Introduction to web scraping | Summary of methods that can be used with webdriver
A mechanism to call a Ruby method from Python that can be done in 200 lines
List of tools that can be used to easily try sentiment analysis of Japanese sentences in Python (try with google colab)
Solution when you want to use cv_bridge with python3 (virtualenv)
Processing of python3 that seems to be usable in paiza
How to set up a simple SMTP server that can be tested locally in Python
Can be used with AtCoder! A collection of techniques for drawing short code in Python!
[Python] Variadic arguments can be used when unpacking iterable elements
Goroutine (parallel control) that can be used in the field
Goroutine that can be used in the field (errgroup.Group edition)
[Python] When you want to import and use your own package in the upper directory
If you want to use field names with hyphens when updating firestore data in python
When you want to hit a UNIX command on Python
How to write when you want to put a number after the group number to be replaced with a regular expression in re.sub of Python
I made it because I want JSON data that can be used freely in demos and prototypes
When you want to replace multiple characters in a string without using regular expressions in python3 series
Do you want to wait for general purpose in Python Selenium?
Mode line when you open the appropriate Python code in Vim
33 strings that should not be used as variable names in python
When you run diff in python and want both returncode and output
New features in Python 3.9 (1)-Union operators can be used in dictionary types
Check if you can connect to a TCP port in Python
Articles to read when Blender Python script code doesn't work in 2.80
Python standard module that can be used on the command line
How to collect images in Python
I wrote a tri-tree that can be used for high-speed dictionary implementation in D language and Python.
Memorandum Regular expression When there are multiple characters in the character string that you want to separate
[Python] Code that can be written with brain death at the beginning when scraping as a beginner
[Python] When the priority is the same in Priority Queue, it can be acquired in the order in which it was added to the queue.
Easy padding of data that can be used in natural language processing
I created a template for a Python project that can be used universally
Python Note: When you want to know the attributes of an object
If you want to count words in Python, it's convenient to use Counter.
Memorandum of means when you want to make machine learning with 50 images
QPS control that can be used in the field (Rate Limit) Limits execution to n times per second
Two document generation tools that you definitely want to use if you write python
A solution to the problem that the Python version in Conda cannot be changed
How to write environment variables that you don't want to put on [GitHub] Python