Shuffle the images in any directory with Python and save them in another folder with serial numbers.

Shuffle the images in any directory with Python and save them in another folder with serial numbers.

Shuffle images in directories with Jupyter-notebook.

shuffleimage.ipynb


#Code to shuffle and save images in a folder
import os
from random import shuffle
from glob import glob
from shutil import copy


data_dir_path = "Paste the absolute path of the directory containing the image here" #Folder path containing the image you want to shuffle

base_path = os.path.join(data_dir_path ,"*.png ")

#Create a directory to save shuffled images
make_dir_path = data_dir_path +"_shuffle" #Destination directory path
os.makedirs(make_dir_path, exist_ok=True) 

#Shuffle the images in the folder and save them in the destination directory
files = sorted(glob(base_path))
shuffle(files)
n = 1 #File name start number

#Save to the directory where the images were created with serial numbers
for file in files:
    copy(file,'{}/{}.jpg'.format(make_dir_path,str(n).zfill(5)))
    n += 1

print("Destination directory%Create r"%os.path.basename(make_dir_path))
print("Number of shuffled images: %d Save completed\n"%(int(len(files))+1))

Quote

Get file name / folder name / extension from path string in Python, combine | note.nkmk.me python relative path How to get the location of the parent directory-Beginner introductory sample [Python] Copy, move, delete folders and files (shutil module) | Hibiki Programming Notes [Python] Python counting the number of files in the current directory

Since this is my first post, I plan to rewrite it ^^

Recommended Posts

Shuffle the images in any directory with Python and save them in another folder with serial numbers.
[Python3] Save the mean and covariance matrix in json with pandas
Install the Python module in any directory
Create and edit spreadsheets in any folder on Google Drive with python
[Python] Get the files in a folder with Python
Archive and compress the entire directory with python
[Python] Get the numbers in the graph image with OCR
Automate background removal for the latest portraits in a directory with Python and API
Temporarily save a Python object and reuse it in another Python
Recursively get the Excel list in a specific folder with python and write it to Excel.
Collect tweets using tweepy in Python and save them in MongoDB
Save images on the web to Drive with Python (Colab)
Output the contents of ~ .xlsx in the folder to HTML with Python
Specify or create a python folder and then save the screenshot.
Operate Firefox with Selenium from python and save the screen capture
Testing with random numbers in Python
Save the binary file in Python
Working with DICOM images in Python
Operate Paints Chainer with Selenium to automatically color images in the directory
Fill the string with zeros in python and count some characters from the string
Replace the directory name and the file name in the directory together with a Linux command.
A memo organized by renaming the file names in the folder with python
How to get the date and time difference in seconds with python
I set the environment variable with Docker and displayed it in Python
Get and convert the current time in the system local timezone with python
Check and receive Serial port in Python (Port check)
Display Python 3 in the browser with MAMP
Capturing images with Pupil, python and OpenCV
Dealing with "years and months" in Python
Importing and exporting GeoTiff images with Python
Read text in images with python OCR
Let's play with Python Receive and save / display the text of the input form
[Python] How to save images on the Web at once with Beautiful Soup
Save Twitter's tweets with Geo in CSV and plot them on Google Map.
How to get a list of files in the same directory with python
Load the network modeled with Rhinoceros in Python ③
Convert PDFs to images in bulk with Python
12. Save the first column in col1.txt and the second column in col2.txt
[Automation] Extract the table in PDF with Python
About the difference between "==" and "is" in python
Determine the numbers in the image taken with the webcam
Load the network modeled with Rhinoceros in Python ②
How to get the files in the [Python] folder
Solving the Lorenz 96 model with Julia and Python
Load the network modeled with Rhinoceros in Python ①
Split Python images and arrange them side by side
Extract images and tables from pdf with python to reduce the burden of reporting
I want to replace the variables in the python template file and mass-produce it in another file.
[Note] How to write QR code and description in the same image with python
[Python] Explain the difference between strftime and strptime in the datetime module with an example
In Python3.8 and later, the inverse mod can be calculated with the built-in function pow.
It is easy to execute SQL with Python and output the result in Excel
Create a docx file with thumbnails of photos in the folder pasted with the Python python-docx library and the free software "Reduction only."
Introduction to AI creation with Python! Part 1 I tried to classify and predict what the numbers are from the handwritten number images.