Reputation: 83695
I have a simple black and white only gif image (400x400px let's say).
I need to get all pixels from that image and find whether they are black or white. I need to create a dictionary with the information about the pixels and their colors then.
I'm pretty new to python so I am kinda struggling with this. But here goes my script so far:
#!/usr/bin/env python
import os
import Image
os.chdir("D:/python-projects")
aImage = Image.open("input.gif")
aPixelsBlackOrWhiteDictionary = {}
# now I need to fill the dictionary with values such as
# "X,Y": 0
# "X,Y": 1
# where X,Y are coordinates and 0/1 i the pixel color (b/w)
Basically I want the final dictionary to be something like this:
"0,0" : 0 # pixel with X=0,Y=0 coordinates is black
"1,0" : 1 # pixel with X=1,Y=0 coordinates is White
EDIT:
When I try:
print aImage[0, 0]
I get an error:
Traceback (most recent call last):
File "D:\python-projects\backprop.py", line 15, in <module>
print aImage[0, 0]
File "C:\Python26\lib\site-packages\pil-1.1.7-py2.6-win32.egg\Image.py", line
512, in __getattr__
raise AttributeError(name)
AttributeError: __getitem__
Upvotes: 7
Views: 28184
Reputation: 1
If you can ensure that your image is in black&white mode, you may do:
aPixelsBlackOrWhiteDictionary = {}
for y in range(0,aImage.size[1]):
for x in range(0,aImage.size[0]):
aPixelsBlackOrWhiteDictionary[ (x,y) ] = aImage.getpixel( (x,y) )
This uses the getpixel
mentioned in other answers and the key of your dictionary will be the tuple (x,y)
print aPixelsBlackOrWhiteDictionary
{(0, 1): 1, (1, 2): 1, (3, 2): 0, (0, 0): 0, (3, 3): 1, (3, 0): 0, (3, 1): 1, (2, 1): 0, (0, 2): 0, (2, 0): 1, (1, 3): 0, (2, 3): 0, (2, 2): 1, (1, 0): 1, (0, 3): 1, (1, 1): 0}
verify that getpixel
is returning ones and zeros
Upvotes: 0
Reputation: 25834
You should be using getpixel
rather than using indexing operators. Note that this may be very slow. You would be better off using getdata
, which returns all of pixels as a sequence.
Upvotes: 9
Reputation: 343
If you wanted to see if a thumb was mono you could try this:-
def is_mono(image, variance=5):
img_dta = list(im.getdata())
colour_test = lambda r,g,b : abs(r-g) > variance or abs(g-b) > variance
if any([colour_test(r,g,b) for (r,g,b) in img_dta]): return False
return True
url1 = 'http://farm5.static.flickr.com/4145/5090947066_0d9d45edf4_s.jpg' # Mono
url2 = 'http://farm5.static.flickr.com/4087/5090362043_03c2da75d4_s.jpg' # Colour
for url in (url1, url2):
dta = urllib.urlopen(url).read()
im = Image.open(StringIO(dta))
print is_mono(im)
>
True
False
Upvotes: 1
Reputation: 77271
Try:
pix = aImage.load()
print pix[x, y]
Also note that you can use tuples as dictionary keys, you can use mydict[(x, y)] instead of mydict["x,y"].
This pixel information is already stored in the image, why store it in a dict?
Upvotes: 8
Reputation: 9224
Are you sure you want to do that? It would be horrendously inefficient to use a dictionary to store this data.
I would think a numpy array would be much more appropriate...
Upvotes: -1