Introduction to Python Image Inflating Image inflating with ImageDataGenerator

Image padding in Python

Introducing ImageDataGenerator, which is useful for padding images with python. See https://keras.io/ja/preprocessing/image/ for more information on the functions.

Source code

data_augumentation.py


# -*- coding: utf-8 -*-
from keras.preprocessing.image import load_img, img_to_array
from keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
import numpy as np
import os
import glob
import argparse
import cv2
from scipy import ndimage


def data_augumentation(input, output, size, ex, ran):
    files = glob.glob(input + '/*.' + ex)
    if os.path.isdir(output) == False:
        os.mkdir(output)

    for i, file in enumerate(files):
        img = load_img(file)
        img = img.resize((size, size))
        x = img_to_array(img)
        x = np.expand_dims(x, axis=0)
        datagen = ImageDataGenerator(
            channel_shift_range=50,
            rotation_range=180,
            zoom_range=0.5,
            horizontal_flip=True,
            vertical_flip=True,
            width_shift_range=0.1,
            height_shift_range=0.1,
            )

        g = datagen.flow(x, batch_size=1, save_to_dir=output, save_prefix='img', save_format='jpg')
        for i in range(ran):
            batch = g.next()

def main():
    parser = argparse.ArgumentParser(description='output mixed images')
    parser.add_argument('--size', '-s', type=int, default=256, help='size to resize images')
    parser.add_argument('--out', '-o', default='./', help='Path to the folder containing images')
    parser.add_argument('--input', '-i', default='./', help='Path to the folder containing images')
    parser.add_argument('--range', '-r', default=9,type = int, help='data_augumentation range')
    parser.add_argument('--extension', '-e', default='jpg', help='File extension to images')
    args = parser.parse_args()
    os.makedirs(args.out, exist_ok=True)
    data_augumentation(args.input, args.out, args.size, args.extension, args.range)


if __name__ == '__main__':
    main()

How to use the function

> python data_augumentation.py --size 512 --out outdir --input inputdir --range 3 --e png
Command line arguments Contents
--size Specify the resolution of the output image
--out Specify output folder
--input Specify input folder
--range How many images should be inflated
--extension Specify the extension in the input folder

As mentioned above, I hope it will be helpful for inflating the image. Feel free to ask any questions! Thank you for reading. If you like, please use LGTM etc.!

Recommended Posts

Introduction to Python Image Inflating Image inflating with ImageDataGenerator
Introduction to image analysis opencv python
HTML email with image to send with python
[Introduction to Python] Let's use foreach with Python
[Python] Introduction to CNN with Pytorch MNIST
Sample to convert image to Wavelet with Python
Introduction to Python language
Introduction to OpenCV (python)-(2)
Image processing with Python
[Python] Easy introduction to machine learning with python (SVM)
Introduction to Artificial Intelligence with Python 1 "Genetic Algorithm-Theory-"
Markov Chain Chatbot with Python + Janome (1) Introduction to Janome
Markov Chain Chatbot with Python + Janome (2) Introduction to Markov Chain
Convert PDF to image (JPEG / PNG) with Python
Introduction to Artificial Intelligence with Python 2 "Genetic Algorithm-Practice-"
Introduction to Tornado (1): Python web framework started with Tornado
How to crop an image with Python + OpenCV
Introduction to formation flight with Tello edu (Python)
Introduction to Python with Atom (on the way)
Introduction to Generalized Linear Models (GLM) with Python
[Introduction to Udemy Python3 + Application] 9. First, print with print
Post an article with an image to WordPress with Python
Image processing with Python (Part 2)
opencv-python Introduction to image processing
Introduction to Python Django (2) Win
How to scrape image data from flickr with python
[Introduction to Python] How to iterate with the range function?
Image editing with python OpenCV
Connect to Wikipedia with Python
Post to slack with Python 3
[Chapter 5] Introduction to Python with 100 knocks of language processing
An introduction to Python distributed parallel processing with Ray
Introduction to Mathematics Starting with Python Study Memo Vol.1
Sorting image files with Python (2)
Reading Note: An Introduction to Data Analysis with Python
Introduction to RDB with sqlalchemy Ⅰ
Sorting image files with Python (3)
Convert the image in .zip to PDF with Python
Introduction to serial communication [Python]
Image processing with Python (Part 1)
Tweet with image in Python
Sorting image files with Python
[Chapter 3] Introduction to Python with 100 knocks of language processing
Image processing with Python (Part 3)
Switch python to 2.7 with alternatives
Write to csv with Python
[Chapter 2] Introduction to Python with 100 knocks of language processing
[Introduction to Python] <list> [edit: 2020/02/22]
Introduction to Python (Python version APG4b)
An introduction to Python Programming
Introduction to Python For, While
[Chapter 4] Introduction to Python with 100 knocks of language processing
[Python] Image processing with scikit-image
Introduction to her made with Python ~ Tinder automation project ~ Episode 6
[Ev3dev] How to display bmp image on LCD with python
20200329_Introduction to Data Analysis with Python Second Edition Personal Summary
Introduction to her made with Python ~ Tinder automation project ~ Episode 5
[Let's play with Python] Image processing to monochrome and dots
Introduction to Python for VBA users-Calling Python from Excel with xlwings-
[Raspi4; Introduction to Sound] Stable recording of sound input with python ♪
[Introduction to Python] How to get data with the listdir function