<jupyter_start><jupyter_text>001.02 // data augmentation [augment card and dataset images]<jupyter_code>import os
import shutil
import numpy as np
from PIL import Image, ImageEnhance, ImageOps
card_dir = '../data/card_data/cards/'
img_dir = '../data/images/'
card_names = []

for suit in ['c', 'd', 'h', 's']:
    for rank in range(1, 14):
        card_names.append(f'{suit}{rank:02}')
        
len(card_names)
# set up card directory

for c in card_names:
    os.mkdir(f'{card_dir}{c}')
images = sorted([i.split('.png')[0] for i in os.listdir(card_dir) if '.png' in i])

for i in images:
    if i in card_names:
        shutil.move(f'{card_dir}{i}.png', f'{card_dir}{i}/{i}.png')
# adjust card brightness

def adjust_brightness(path_in, path_out, card, file_type):
    c = 1
    for n in np.linspace(0.75, 1.5, 10):
        im = Image.open(f'{path_in}{card}{file_type}')
        im = ImageEnhance.Brightness(im).enhance(n)
        im.save(f'{path_out}{card}.B{c:02}{file_type}')
        im.close()
        c += 1
[adjust_brightness(f'{card_dir}{c}/', f'{card_dir}{c}/', c, '.png') for c in card_names];
# # adjust card contrast

# def adjust_contrast(path_in, path_out, card, file_type):
#     c = 1
#     for n in np.linspace(0.75, 1.5, 5):
#         im = Image.open(f'{path_in}{card}{file_type}')
#         im = ImageEnhance.Contrast(im).enhance(n)
#         im.save(f'{path_out}{card}.CT{c:02}{file_type}')
#         im.close()
#         c += 1
# # adjust card color

# def adjust_color(path_in, path_out, card, file_type):
#     c = 1
#     for n in np.linspace(0.75, 1.5, 5):
#         im = Image.open(f'{path_in}{card}{file_type}')
#         im = ImageEnhance.Color(im).enhance(n)
#         im.save(f'{path_out}{card}.CO{c:02}{file_type}')
#         im.close()
#         c += 1
# convert images to grayscale

os.mkdir('../data/images_gs/')

filenames = sorted([i.split('.jpg')[0] for i in os.listdir(img_dir) if '.jpg' in i])

def grayscale(path_in, path_out, file, file_type):
    im = Image.open(f'{path_in}{file}{file_type}')
    im = ImageOps.grayscale(im)
    im.save(f'{path_out}{file}{file_type}')
    im.close()
[grayscale(img_dir, f'../data/images_gs/', f, '.jpg') for f in filenames];<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.184670551322277, 'fraction_numerical': 0.01927386822052891, 'mean_word_length': 3.4023668639053253, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '79116', 'license': 'MIT', 'n_tokens_mistral': 926, 'n_tokens_neox': 891, 'n_words': 183, 'path': 'code/001.02_data_augmentation.ipynb', 'repo_name': 'rrrrhys/blackjack', 'size': '2231'}