Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an image from a dictionary using PIL

I have a dictionary that maps coordinate tuples (in the range (0,0) to (199, 199) to grayscale values (integers between 0 and 255.) Is there a good way to create an PIL Image that has the specified values at the specified coordinates? I'd prefer a solution that only uses PIL to one that uses scipy.

like image 971
Max Rosett Avatar asked Apr 25 '26 17:04

Max Rosett


1 Answers

You can try image.putpixel() to change the color of a pixel at a particular position. Example code -

from PIL import Image
from random import randint

d = {(x,y):randint(0,255) for x in range(200) for y in range(200)}
im = Image.new('L',(200,200))

for i in d:
    im.putpixel(i,d[i])

im.save('blah.png')

It gave me a result like -

enter image description here

like image 122
Anand S Kumar Avatar answered Apr 28 '26 06:04

Anand S Kumar