Skip to content

Instantly share code, notes, and snippets.

@andreemidio
Created September 20, 2019 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreemidio/7cf1f8712f4904c8ad0e20f017b18aa9 to your computer and use it in GitHub Desktop.
Save andreemidio/7cf1f8712f4904c8ad0e20f017b18aa9 to your computer and use it in GitHub Desktop.
from PIL import Image
import os, sys
path = "C:\\Users\\Andre\\Desktop\\HelmetDetection\\dowloadImages\\downloads"
#path = "C:\\Users\\Andre\\Desktop\\HelmetDetection\\dowloadImages\\downloads\\ hard hat"
dirs = os.listdir( path )
final_size = 60
def resize_aspect_fit():
for item in dirs:
if item == '.DS_Store':
continue
if os.path.isfile(path+item):
im = Image.open(path+item)
f = os.path.splitext(path+item)
size = im.size
ratio = float(final_size) / max(size)
new_image_size = tuple([int(x*ratio) for x in size])
im = im.resize(new_image_size, Image.ANTIALIAS)
new_im = Image.new("RGB", (final_size, final_size))
new_im.paste(im, ((final_size-new_image_size[0])//2, (final_size-new_image_size[1])//2))
new_im.save(f + 'resized.jpg', 'JPEG', quality=90)
resize_aspect_fit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment