<jupyter_start><jupyter_text>Image CropperThis notebook crops images stored in Google Drive to be 200 x 200 px for use with a GAN. The size could be changed. Its meant to be used in Google Colab.<jupyter_code>#mounts google drive
#skip this cel if you want to use a local repo

from google.colab import drive
drive.mount("/content/gdrive") 
import os
import glob
from PIL import Image, ImageDraw, ImageFilter
thumb_width = 200

directory_name="YOUR/INPUT/DIRECTORY/HERE"
export_directory="YOUR/EXPORT/DIRECTORY/HERE""

#creates a smaller image
def smaller(image):
  new_height = 200
  new_width  = new_height * image.width / image.height
  return image.resize((int(new_width), new_height), Image.ANTIALIAS)


#finds the image center

def crop_center(pil_img, crop_width, crop_height):
    img_width, img_height = pil_img.size
    return pil_img.crop(((img_width - crop_width) // 2,
                         (img_height - crop_height) // 2,
                         (img_width + crop_width) // 2,
                         (img_height + crop_height) // 2))
    
#crops the image

count=0
for image in os.listdir(directory_name):
  count=count+1
  image_name=directory_name+"/"+image
  im = Image.open(image_name)
  im_small= smaller(im)
  im_thumb = crop_center(im_small, thumb_width, thumb_width)
  im_out="image_{0:0=4d}.jpg".format(count)
  im_out_whole=export_directory+im_out
  im_thumb.save(im_out_whole, quality=100)<jupyter_output><empty_output><jupyter_text>Once the former cel is done you can export the whole thing as a zip file and download it off Colab<jupyter_code>#zips the whole thing for download

import shutil
shutil.make_archive("cropped_image", 'zip', export_directory)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0919674039580908, 'fraction_numerical': 0.013969732246798603, 'mean_word_length': 4.209090909090909, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '162110', 'license': 'MIT', 'n_tokens_mistral': 592, 'n_tokens_neox': 563, 'n_words': 157, 'path': 'Google Drive Image Cropper/Google_Drive_Image_Cropper.ipynb', 'repo_name': 'pointandclickinterface/image-processing', 'size': '1718'}