Last active
January 29, 2021 06:29
-
-
Save jakewilliami/7b82ae277f120d466b525df7df62dce3 to your computer and use it in GitHub Desktop.
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 # this is used to create the images | |
import glob # this is used for reading the directory (folder) of images | |
import random # this is used to choose 25 unique random images | |
from math import ceil, floor | |
distractors = glob.glob("Distractor/*") # this is where you need to put your path to directory | |
cars = glob.glob("Cars/*") | |
faces = glob.glob("Faces/*") | |
hf_pareidolia = glob.glob("Highface pareidolia/*") | |
lf_pareidolia = glob.glob("Lowface pareidolia/*") | |
randtarget = random.choice(cars + faces + hf_pareidolia + lf_pareidolia) | |
listofimages = random.sample(distractors, 25) | |
listofimages.append(randtarget) | |
listofimages = random.sample(listofimages, len(listofimages)) | |
print(listofimages) | |
def create_collage_with_outer_borders(cols, rows, img_width, img_height, padding, listofimages): | |
width = (img_width * cols) + (padding * (cols - 1)) | |
height = (img_height * rows) + (padding * (rows - 1)) | |
width_without_padding = width - padding | |
height_without_padding = height - padding | |
cell_width = width_without_padding//cols | |
cell_height = height_without_padding//rows | |
thumbnail_width = cell_width - padding | |
thumbnail_height = cell_height - padding | |
size = thumbnail_width, thumbnail_height | |
new_im = Image.new('RGBA', (width, height)) | |
ims = [] | |
for p in listofimages: | |
im = Image.open(p) | |
im.thumbnail(size) | |
ims.append(im) | |
i = 0 | |
x = 0 | |
y = 0 | |
for col in range(cols): | |
for row in range(rows): | |
new_im.paste(ims[i], (x + padding, y + padding)) | |
i += 1 | |
x += cell_width | |
y += cell_height | |
x = 0 | |
return new_im | |
def create_collage(images_per_row, img_width, img_height, padding, images): | |
frame_width = (img_width * images_per_row) + (padding * (images_per_row - 1)) | |
cell_width = ceil((frame_width + padding) / images_per_row) | |
new_image_width = cell_width - padding | |
scaling_factor = new_image_width / img_width | |
scaled_img_width = ceil(img_width * scaling_factor) | |
scaled_img_height = ceil(img_height * scaling_factor) | |
cell_height = scaled_img_height + padding | |
number_of_rows = ceil(len(images) / images_per_row) | |
frame_height = cell_height * number_of_rows - padding | |
new_im = Image.new('RGBA', (frame_width, frame_height)) | |
x_cord = 0 | |
for num, im in enumerate(images): | |
if num % images_per_row == 0: | |
x_cord = 0 | |
im = Image.open(im) | |
im.thumbnail((scaled_img_width, scaled_img_height)) | |
y_cord = (num // images_per_row) * cell_height | |
new_im.paste(im, (x_cord, y_cord)) | |
x_cord += cell_width | |
return new_im | |
def create_collage_resize(images_per_row, img_width, img_height, desired_frame_width, desired_frame_height, padding, images): | |
im = create_collage(images_per_row, img_width, img_height, padding, images) | |
return im.resize((desired_frame_width, desired_frame_height)) | |
# --------- | |
img_width, img_height = Image.open(listofimages[0]).size # get size of images (assume all same size) | |
wo_borders = create_collage_with_outer_borders(5, 5, img_width, img_height, 5, listofimages) | |
img = create_collage(5, img_width, img_height, 5, listofimages) | |
img_resized = create_collage_resize(5, img_width, img_height, 530, 530, 5, listofimages) | |
# using PNG because JPEG is lossy | |
wo_borders.save("out_with_borders.png", "PNG", quality = 80, optimize = True, progressive = True) | |
img.save("out.png", "PNG", quality = 80, optimize = True, progressive = True) | |
img_resized.save("out_resized.png", "PNG", quality = 80, optimize = True, progressive = True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment