Amplify images for machine learning with python

Preparation

pip install pillow

code

augmentImages.py


# ---
#1 Amplify the image file to 20 files and store it in the generated folder
# ---

from PIL import Image, ImageOps
import glob, os

#Specify the file to be amplified
files = glob.glob('images/**/*.JPG', recursive=True)

for i, file in enumerate(files):
    im = Image.open(file)
    print(i + 1, '/', len(files), file, im.format, im.size, im.mode)

    #Create output directory
    new_dir = 'generated/' + os.path.dirname(file)
    os.makedirs(new_dir, exist_ok=True)

    #Resized to be 1000px wide
    resize_ratio = float(1000) / im.width
    im = im.resize((int(im.width * resize_ratio), int(im.height * resize_ratio)))

    #Save original image
    im.save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '.JPG', quality=95)

    #-5〜+Rotate up to 5 degrees and save each
    im.rotate(1).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-p1.JPG', quality=95)
    im.rotate(2).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-p2.JPG', quality=95)
    im.rotate(3).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-p3.JPG', quality=95)
    im.rotate(4).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-p4.JPG', quality=95)
    im.rotate(5).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-p5.JPG', quality=95)
    im.rotate(-1).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-n1.JPG', quality=95)
    im.rotate(-2).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-n2.JPG', quality=95)
    im.rotate(-3).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-n3.JPG', quality=95)
    im.rotate(-4).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-n4.JPG', quality=95)
    im.rotate(-5).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-n5.JPG', quality=95)

    #Invert-5〜+Rotate up to 5 degrees and save each
    ImageOps.mirror(im).rotate(1).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-mp1.JPG', quality=95)
    ImageOps.mirror(im).rotate(2).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-mp2.JPG', quality=95)
    ImageOps.mirror(im).rotate(3).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-mp3.JPG', quality=95)
    ImageOps.mirror(im).rotate(4).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-mp4.JPG', quality=95)
    ImageOps.mirror(im).rotate(5).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-mp5.JPG', quality=95)
    ImageOps.mirror(im).rotate(-1).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-mn1.JPG', quality=95)
    ImageOps.mirror(im).rotate(-2).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-mn2.JPG', quality=95)
    ImageOps.mirror(im).rotate(-3).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-mn3.JPG', quality=95)
    ImageOps.mirror(im).rotate(-4).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-mn4.JPG', quality=95)
    ImageOps.mirror(im).rotate(-5).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-mn5.JPG', quality=95)

Relation

Python code for learning and testing with Custom Vision of Cognitive Service --Qiita

Recommended Posts

Amplify images for machine learning with python
[Python] Collect images with Icrawler for machine learning [1000 images]
[Shakyo] Encounter with Python for machine learning
Machine learning with Python! Preparation
Beginning with Python machine learning
Machine learning with python (1) Overall classification
<For beginners> python library <For machine learning>
"Scraping & machine learning with Python" Learning memo
Machine learning with python (2) Simple regression analysis
Why Python is chosen for machine learning
[Python] Web application design for machine learning
[Python] Read images with OpenCV (for beginners)
An introduction to Python for machine learning
Build AI / machine learning environment with Python
Building a Windows 7 environment for getting started with machine learning with Python
Learning Python with ChemTHEATER 03
Learning Python with ChemTHEATER 05-1
Learning Python with ChemTHEATER 02
Learning Python with ChemTHEATER 01
[Python] Easy introduction to machine learning with python (SVM)
Machine learning starting with Python Personal memorandum Part2
Machine learning starting with Python Personal memorandum Part1
Upgrade the Azure Machine Learning SDK for Python
Collect images for machine learning (Bing Search API)
I started machine learning with Python Data preprocessing
Build a Python machine learning environment with a container
Machine learning learned with Pokemon
Data set for machine learning
Japanese preprocessing for machine learning
Image collection Python script for creating datasets for machine learning
Build an interactive environment for machine learning in Python
Learning flow for Python beginners
Python learning plan for AI learning
Reinforcement learning starting with Python
Python learning memo for machine learning by Chainer from Chapter 2
Python learning memo for machine learning by Chainer Chapters 1 and 2
Preparing to start "Python machine learning programming" (for macOS)
Build a machine learning application development environment with Python
Machine learning Minesweeper with PyTorch
[Python] I made a classifier for irises [Machine learning]
Machine Learning with Caffe -1-Category images using reference model
Python Machine Learning Programming> Keywords
Summary of the basic flow of machine learning with Python
Checkio's recommendation for learning Python
Python Iteration Learning with Cheminformatics
Align the number of samples between classes of data for machine learning with Python
Try machine learning with Kaggle
Memo for building a machine learning environment using Python
Python learning notes for machine learning with Chainer Chapters 11 and 12 Introduction to Pandas Matplotlib
The first step of machine learning ~ For those who want to implement with python ~
I tried to build an environment for machine learning with Python (Mac OS X)
Build an environment for machine learning using Python on MacOSX
I started machine learning with Python Clustering & Dimension Compression & Visualization
For those who want to start machine learning with TensorFlow2
How to use machine learning for work? 03_Python coding procedure
Python learning memo for machine learning by Chainer Chapter 7 Regression analysis
Create a python machine learning model relearning mechanism with mlflow
Machine learning environment settings based on Python 3 on Mac (coexistence with Python 2)
Feature Engineering for Machine Learning Beginning with Part 3 Google Colaboratory-Scaling
Number recognition in images with Python
Web teaching materials for learning Python