Skip to content

Instantly share code, notes, and snippets.

@ciffelia
Created June 26, 2020 14:36
Show Gist options
  • Save ciffelia/9e7e2182f265847dd23b0b0b3e852ec9 to your computer and use it in GitHub Desktop.
Save ciffelia/9e7e2182f265847dd23b0b0b3e852ec9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
import random
import string
from PIL import Image, ImageOps
def random_name(n):
rand_list = [random.choice(string.ascii_lowercase + string.digits) for i in range(n)]
return ''.join(rand_list)
def main():
orig_img = Image.open(sys.argv[1])
new_img = Image.new("RGB", orig_img.size, (255, 255, 255))
new_img.paste(orig_img)
cropped_img = new_img.crop(ImageOps.invert(new_img).getbbox())
# padding = round(min(cropped_img.size) * 0.1)
padding = 10
padded_size = tuple(z + padding * 2 for z in cropped_img.size)
padded_img = Image.new("RGB", padded_size, (255, 255, 255))
padded_img.paste(cropped_img, (padding, padding))
file_name = os.path.dirname(os.path.abspath(__file__)) + "\\" + random_name(8) + ".jpg"
padded_img.save(file_name, quality=85, progressive=True)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment