Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import os
- from PIL import Image
- from PIL import ImageChops
- from PIL import ImageFile
- ImageFile.LOAD_TRUNCATED_IMAGES = True
- import math
- def trim(im):
- bg = Image.new(im.mode, im.size, im.getpixel((0,0)))
- diff = ImageChops.difference(im, bg)
- diff = ImageChops.add(diff, diff, 2.0, -100)
- bbox = diff.getbbox()
- if bbox:
- return im.crop(bbox)
- dir = os.fsencode('./18')
- images = [1 for file in os.listdir(dir)]
- n = len(images)
- size = 6000
- basic_height = 300
- background = Image.new('RGBA', (size, size), (0, 0, 0, 255))
- x = 1
- y = 1
- i = 0
- for file in os.listdir(dir):
- i += 1
- print(n, i, file)
- image = Image.open(os.path.join(dir, os.fsencode(file)))
- curr_x, curr_y = image.size
- image.thumbnail((math.floor(curr_x / curr_y * basic_height), basic_height), Image.ANTIALIAS)
- if x + image.size[0] > size:
- x = 1
- y += basic_height
- background.paste(image, (x, y))
- x += image.size[0]
- if x > size:
- x = 1
- y += basic_height
- if y > size:
- print('NOT ENOUGH PLACE')
- trim(background).save('result_18.png')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement