Skip to content

Instantly share code, notes, and snippets.

@morawi
Last active October 5, 2018 15:51
Show Gist options
  • Save morawi/4879f4a8c68c1a51056a25e565b80ccd to your computer and use it in GitHub Desktop.
Save morawi/4879f4a8c68c1a51056a25e565b80ccd to your computer and use it in GitHub Desktop.
Stitching an arbitrary number of images into one with PIL/Pillow and PyTorch
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 5 11:29:33 2018
@author: malrawi
"""
import torchvision
from PIL import Image
import numpy as np
def get_the_data(data_set_name):
folder_of_data = '/home/morawi/data'
the_root = folder_of_data + data_set_name
# other split flags: ‘train’, 'test' ‘train+unlabeled’
unlabeled_set = torchvision.datasets.STL10(root = the_root, split= 'unlabeled',
download=True, transform=None, target_transform = None )
return unlabeled_set
intended_w = 500
intded_h = 200
dataset = get_the_data('STL10')
no_of_images_to_stich = 10
imgs_idx = np.random.randint(0, len(dataset), no_of_images_to_stich) # idx of selected images
one_img = dataset[0][0] # rerurns a tuple, image at idx 0, and label at idx 1
w, h = one_img.size
stiched_image = Image.new("RGB", (no_of_images_to_stich*w, h))
for index in range(no_of_images_to_stich):
img = dataset[imgs_idx[index]][0]
x = index * w
stiched_image.paste(img, (x , 0, x + w , h))
stiched_image = stiched_image.resize([intended_w,intded_h], Image.ANTIALIAS)
stiched_image.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment