Skip to content

Instantly share code, notes, and snippets.

@jessstringham
Last active April 8, 2018 18:51
Show Gist options
  • Save jessstringham/b2fe92a7f1412f00443b0fb124f08bd9 to your computer and use it in GitHub Desktop.
Save jessstringham/b2fe92a7f1412f00443b0fb124f08bd9 to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
PASTA_SIZE = 16
NUM_IMAGE_SPLITS = 2
IMAGE_LOCATION = 'something.png'
image = mpimg.imread(IMAGE_LOCATION)
def pasta_machine(image, pasta_size, number_of_output_images):
'''Split the `image` into strips of size `pasta_size`, divide into
`number_of_output_images` groups, and glue the strips together, and
return the `number_of_images` images. Crops the input image.
image: numpy array (height, width, channels)
pasta_size: (int): pixels per strip
number_of_output_images: number of images to return
returns: (number_of_output_images, ~height // number_of_output_images, width, channels): note
the height is cropped if needed.
'''
h, w, c = image.shape
# trim the image so that when it is divided into groups of pasta,
# the two new images are the same size.
h = (h // (pasta_size * number_of_output_images)) * pasta_size * number_of_output_images
image = image[:h, :, :]
# split image into strips of width pasta_size, and divide into
# two groups.
image = image.reshape(
h // pasta_size // number_of_output_images,
number_of_output_images,
pasta_size,
w, c)
# now split into the two groups (transpose) and glue the strips together (vstack)
image = np.vstack(image.transpose(0, 2, 1, 3, 4))
# make the first dimension the number_of_output_images images
image = image.transpose(1, 0, 2, 3)
return image
# pasta the image
first_pasta_pass = pasta_machine(image, PASTA_SIZE, NUM_IMAGE_SPLITS)
# admire the image
plt.imshow(np.vstack(first_pasta_pass))
plt.axis('off')
plt.show()
# glue the two images together
double_image = np.hstack(first_pasta_pass)
# rotate the image
rotated_double_image = double_image.transpose(1, 0, 2)
# pasta the double image
rotated_pasta_quad_image = pasta_machine(rotated_double_image, PASTA_SIZE, NUM_IMAGE_SPLITS)
# rotate them back
pasta_quad_image = rotated_pasta_quad_image.transpose(0, 2, 1, 3)
# and glue the two images together
quad_image = np.hstack(pasta_quad_image)
# admire the image
fig = plt.figure(figsize=(12, 12))
plt.imshow(quad_image)
plt.axis('off')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment