Skip to content

Instantly share code, notes, and snippets.

@glenrobertson
Created April 3, 2012 00:05
Show Gist options
  • Save glenrobertson/2288152 to your computer and use it in GitHub Desktop.
Save glenrobertson/2288152 to your computer and use it in GitHub Desktop.
get PIL white noise image
from PIL import Image
def get_white_noise_image(width, height):
pil_map = Image.new("RGBA", (width, height), 255)
random_grid = map(lambda x: (
int(random.random() * 256),
int(random.random() * 256),
int(random.random() * 256)
), [0] * width * height)
pil_map.putdata(random_grid)
return pil_map
@hackgoofer
Copy link

if you use python3: pil_map.putdata(list(random_grid))

@SamuelGabriel
Copy link

SamuelGabriel commented Sep 21, 2020

It is much cheaper to do:

from PIL import Image
def get_white_noise_image(w,h):
    pil_map = Image.fromarray(np.random.randint(0,255,(w,h,3),dtype=np.dtype('uint8')))
    return pil_map

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment