# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

from numpy import expand_dims
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from matplotlib import pyplot
from PIL import Image
import glob
import os
import shutil

i = 0
image_list = []
base_dir = '/home/asabuncuoglu13/Desktop/edu-repo/marti/'
data_dir = base_dir + 'data/images'
val_dir = base_dir + 'data/validation'
if os.path.exists(data_dir):
    shutil.rmtree(data_dir)
if os.path.exists(val_dir):
    shutil.rmtree(val_dir)
os.makedirs(data_dir)
os.makedirs(val_dir)

datagen = ImageDataGenerator(
    rotation_range=20,
    zoom_range=0.15,
    width_shift_range=0.2,
    height_shift_range=0.2,
    brightness_range=(0.6, 1.0),
    shear_range=0.15,
    horizontal_flip=True,
    fill_mode="nearest")

for filename in sorted(glob.glob(base_dir + 'media/cards-en/*.png')):
    bname = os.path.basename(filename)[:-4]
    print(bname)
    # create new folder for each augmented image
    alt_dir = data_dir + '/' + bname + '/'
    alt_val_dir = val_dir + '/' + bname + '/'
    os.makedirs(alt_dir)
    os.makedirs(alt_val_dir)

    im = Image.open(filename)
    im = im.resize((224, 224), Image.ANTIALIAS)

    # convert to numpy array
    data = img_to_array(im)
    # expand dimension to one sample
    samples = expand_dims(data, 0)
    # create image data augmentation generator
    dflow = datagen.flow(samples, batch_size=1, save_to_dir=alt_dir,
                         save_prefix="aug", save_format="png",)

    k = 0
    for batch in dflow:
        k += 1
        if k > 50:
            break  # otherwise the generator would loop indefinitely

    vflow = datagen.flow(samples, batch_size=1, save_to_dir=alt_val_dir,
                         save_prefix="val", save_format="png",)

    j = 0
    for batch in vflow:
        j += 1
        if j > 10:
            break  # otherwise the generator would loop indefinitely

for filename in sorted(glob.glob(base_dir + 'media/real-en/*.jpg')):
    bname = os.path.basename(filename)[:-4]
    print(bname)
    alt_dir = data_dir + '/' + bname + '/'
    alt_val_dir = val_dir + '/' + bname + '/'

    im = Image.open(filename)
    im = im.resize((224, 224), Image.ANTIALIAS)

    # convert to numpy array
    data = img_to_array(im)
    # expand dimension to one sample
    samples = expand_dims(data, 0)
    # create image data augmentation generator
    dflow = datagen.flow(samples, batch_size=1, save_to_dir=alt_dir,
                         save_prefix="aug", save_format="png",)

    k = 0
    for batch in dflow:
        k += 1
        if k > 50:
            break  # otherwise the generator would loop indefinitely

    vflow = datagen.flow(samples, batch_size=1, save_to_dir=alt_val_dir,
                         save_prefix="val", save_format="png",)

    j = 0
    for batch in vflow:
        j += 1
        if j > 10:
            break  # otherwise the generator would loop indefinitely



StarcoderdataJupyterScriptsDedupFiltered