Last active
April 30, 2019 14:41
-
-
Save lambdan/403ddffa99ae3d1100fbb80bae589e34 to your computer and use it in GitHub Desktop.
Make image grid of posters from IMDB (Python 3, Pillow)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image | |
import os | |
# pip install Pillow | |
##### Settings ##### | |
grid_size = 9,4 # columns x rows | |
workdir = '.' # default: "." - current folder | |
out_file = '_out.png' | |
image_exts = ('.jpg', '.png', '.bmp') | |
############################### | |
# count posters and check lowest common resolution | |
posters = 0 | |
lowest_common_resolution = 0 | |
for f in os.listdir(workdir): | |
if f.lower().endswith(image_exts) and f != out_file: | |
posters += 1 | |
im = Image.open(f) | |
w,h = im.size | |
if lowest_common_resolution == 0 or w < lowest_common_resolution[0] or h < lowest_common_resolution[1]: | |
lowest_common_resolution = w,h | |
print (posters, "posters") | |
print ("lowest common resolution:", lowest_common_resolution) | |
per_row = grid_size[0] | |
rows = grid_size[1] | |
poster_resolution = lowest_common_resolution | |
out_width = poster_resolution[0] * per_row | |
out_height = poster_resolution[1] * rows | |
out_image = Image.new("RGB", (out_width, out_height)) | |
x = 0 | |
y = 0 | |
for f in os.listdir(workdir): | |
if f.lower().endswith(image_exts) and f != out_file: | |
# load image | |
#print f | |
im = Image.open(os.path.join(workdir, f)) | |
if im.size != poster_resolution: | |
print ("resizing",f,"from",im.size,"to",poster_resolution) | |
im = im.resize((poster_resolution)) | |
coords = (x*poster_resolution[0], y*poster_resolution[1]) | |
out_image.paste(im, coords) | |
x += 1 | |
if x == per_row: | |
x = 0 | |
y += 1 | |
print ("saving grid with resolution",out_image.size,"to",out_file) | |
out_image.save(out_file) |
Author
lambdan
commented
Jan 6, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment