Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PIL import Image, ImageOps
- import random
- random_setting = False
- savepng = False
- def concat_images(image_paths, size, shape=None):
- # Open images and resize them
- width, height = size
- images = map(Image.open, image_paths)
- images = [ImageOps.fit(image, size, Image.ANTIALIAS)
- for image in images]
- # Create canvas for the final image with total size
- shape = shape if shape else (1, len(images))
- image_size = (width * shape[1], height * shape[0])
- image = Image.new('RGB', image_size)
- # Paste images into final image
- for row in range(shape[0]):
- for col in range(shape[1]):
- offset = width * col, height * row
- idx = row * shape[1] + col
- image.paste(images[idx], offset)
- return image
- # Get list of image paths
- folder = "/asap3/petra3/gpfs/p06/2018/data/11005475/scratch_cc/Jan/savefig/plotXBIC_singlecell_new"
- image_paths = [os.path.join(folder, f)
- for f in sorted(os.listdir(folder)) if f.endswith('.png')]
- if random_setting:
- # Random selection of images
- image_array = random.choices(image_paths, k=45)
- else:
- image_array = image_paths
- if savepng:
- # Create and save image grid
- now = datetime.now()
- dt_string = now.strftime("%d-%m-%Y_%H_%M_%S")
- image = concat_images(image_array, (400, 400), shape=(5,9))
- image.save(f"image_{dt_string}.png", "PNG")
- concat_images(image_array, (400, 400), shape=(5,9))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement