Skip to content

Instantly share code, notes, and snippets.

@CnrLwlss
Created April 1, 2021 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CnrLwlss/e5cca45cb6b803cbd4ac7a7f03eff93c to your computer and use it in GitHub Desktop.
Save CnrLwlss/e5cca45cb6b803cbd4ac7a7f03eff93c to your computer and use it in GitHub Desktop.
Converting arrays to images using Python
from PIL import Image
import numpy as np
# Generate a 100x100 array of random floats between 0.0 and 1.0
random_floats = np.random.rand(100,100)
# Scale these up to lie in the range of 8-bit integers (between 0.0 and 255.0)
random_floats_8bit = random_floats * 255.0
# Round these float values to the nearest integer
random_round = np.round(random_floats_8bit)
# Convert these rounded floats to actual 8 bit integers
random_8bit = np.array(random_round,dtype=np.uint8)
# Convert this array of 8bit integers into an image
random_image = Image.fromarray(random_8bit)
# Let's have a look at the image we've just created
random_image.show()
# It's really tiny so let's make it bigger (zoom in)
random_zoom = random_image.resize((5000,5000), Image.NEAREST)
# Let's have a look at this new, zoomed-in image
random_zoom.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment