Your array of "binary values" is an array of bytes?
If so, you can do (using Pillow) after resizing it:
from PIL import Image
im = Image.fromarray(arr)
And then im.show() to see it.
If your array has only 0's and 1's (1-bit depth or b/w) you may have to multiply it to 255
im = Image.fromarray(arr * 255)
Here an example:
>>> arr = numpy.random.randint(0,256, 100*100) #example of a 1-D array
>>> arr.resize((100,100))
>>> im = Image.fromarray(arr)
>>> im.show()

Edit (2018):
This question was written in 2011 and Pillow changed ever since requiring to use the mode='L' parameter when loading with fromarray.
Also on comments below it was said arr.astype(np.uint8) was needed as well, but I have not tested it
Your array of "binary values" is an array of bytes?
If so, you can do (using Pillow) after resizing it:
from PIL import Image
im = Image.fromarray(arr)
And then im.show() to see it.
If your array has only 0's and 1's (1-bit depth or b/w) you may have to multiply it to 255
im = Image.fromarray(arr * 255)
Here an example:
>>> arr = numpy.random.randint(0,256, 100*100) #example of a 1-D array
>>> arr.resize((100,100))
>>> im = Image.fromarray(arr)
>>> im.show()

Edit (2018):
This question was written in 2011 and Pillow changed ever since requiring to use the mode='L' parameter when loading with fromarray.
Also on comments below it was said arr.astype(np.uint8) was needed as well, but I have not tested it
Using PIL is not really needed, you can plot the array directly with pyplot (see below). To save to a file, you could use plt.imsave('fname.png', im).

Code below.
import numpy as np
import matplotlib.pyplot as plt
x = (np.random.rand(1754**2) < 0.5).astype(int)
im = x.reshape(1754, 1754)
plt.gray()
plt.imshow(im)
You can also use plt.show(im) to display image in new window.
You want to use the mode 'L' instead of 'LA' as the parameter to the convert() method. 'LA' leaves an alpha channel and then the numpy.asarray doesn't work as you intended. If you need the alpha channel, then you will need a different method to convert to a numpy array. Otherwise, use mode 'L'.
It looks like you're using a version of PIL prior to 1.1.6, where they introduced the methods so that numpy would know what to do with an Image. So you're just getting img_as_np as a one-element array containing an Image object (which is what Out[4] is showing you).
You instead need to do something like np.asarray(img.getdata()), which will give you a num_pixels x num_channels array of integers between 0 and 255 (at least for the png I tried). You may want to do
img_as_np = np.asarray(img.getdata()).reshape(img.size[1], img.size[0], -1)
to lay it out like the image (transposed). You might also want to divide by 255 to get float values between 0 and 1, if that's the format you're expecting (as does e.g. matplotlib's imshow).
Python: Converting a numpy matrix to a grayscale image - Stack Overflow
How to convert grayscale values in matrix to an image in Python? - Stack Overflow
Converting an RGB image to grayscale and manipulating the pixel data in python - Stack Overflow
python - Converting an image to grayscale using numpy - Stack Overflow
You can display and save an image with matplotlib
import numpy
from matplotlib import pyplot as plt
x = numpy.random.rand(10, 10)*255
plt.imshow(x, cmap='gray', interpolation='nearest', vmin=0, vmax=255)
plt.savefig('text.png')
plt.show()
Unfortunately fromarray has no docstring, but what you are trying should work if your "matrix" is a numpy array (or otherwise implements the array interface) and you additionally set the mode to 'L' (as the second argument to fromarray).
Here is a working code:
def grayConversion(image):
grayValue = 0.07 * image[:,:,2] + 0.72 * image[:,:,1] + 0.21 * image[:,:,0]
gray_img = grayValue.astype(np.uint8)
return gray_img
orig = cv2.imread(r'C:\Users\Jackson\Desktop\drum.png', 1)
g = grayConversion(orig)
cv2.imshow("Original", orig)
cv2.imshow("GrayScale", g)
cv2.waitKey(0)
cv2.destroyAllWindows()
You can use a dot product:
gray_image = image.dot([0.07, 0.72, 0.21])
Or even just do the whole operation manually:
b = image[..., 0]
g = image[..., 1]
r = image[..., 2]
gray_image = 0.21 * r + 0.72 * g + 0.07 * b
Don't forget to convert back to 0-255:
gray_image = np.min(gray_image, 255).astype(np.uint8)
This is easily done with PIL/Pillow. Create a new grayscale image of the proper size, and convert each array value to the range 0-255 and copy it in. I added a resize at the end to make the pixels easier to see.
from PIL import Image
array1 = [[0.48, 0.4, 0.56, 0.32, 0.52],
[0.36, 0.56, 0.56, 0.68, 0.56],
[0.32, 0.72, 0.88, 0.44, 0.56],
[0.56, 0.64, 0.76, 0.52, 0.48],
[0.32, 0.88, 0.56, 0.52, 0.4 ]]
width1 = 5
height1 = 5
im = Image.new('L', (width1, height1))
pix = im.load()
for y in range(height1):
for x in range(width1):
pix[x, y] = int(array1[y][x] * 255.9999)
im = im.resize((width1 * 10, height1 * 10), resample=Image.NEAREST)
im.save(r'c:\temp\temp.png')

PIL can create a PIL Image directly from an array:
from PIL import Image
import numpy as np
# Make our randomness repeatable, and generate a 5x4 array of pixels
np.random.seed(42)
px = np.random.random((4,5))
That looks like this:
array([[0.37454012, 0.95071431, 0.73199394, 0.59865848, 0.15601864],
[0.15599452, 0.05808361, 0.86617615, 0.60111501, 0.70807258],
[0.02058449, 0.96990985, 0.83244264, 0.21233911, 0.18182497],
[0.18340451, 0.30424224, 0.52475643, 0.43194502, 0.29122914]])
Now multiply up by 255, convert to np.uint8 and make into PIL Image
p = Image.fromarray((px*255.9999).astype(np.uint8))
# We are finished, but enlarge for illustrative purposes
p = p.resize((50, 40), resample=Image.Resampling.NEAREST)
p.save('result.png')

Two issues. Firstly, you are not converting to greyscale. Secondly, the whole point of numpy is to vectorise and avoid for loops because they are slow.
So starting with this image:

You want something like this:
#!/usr/local/bin/python3
import numpy as np
from PIL import Image
# Load coins and convert to greyscale
grey = np.array(Image.open('coins.png').convert('L'))
# Threshold at 128
thresholded=((grey>128)*255).astype(np.uint8)
# Save
Image.fromarray(thresholded).save('result.png')

Sruthi V is right. enumerate() returns a tuple.
So instead of
if item < 128
it should be
if item[1] < 128
Or you could just remove enumerate() entirely if you aren't using it. It seems it will work fine without it.
As of OpenCV 2 and 3, LoadImage() has been discontinued. Please use the latest version of OpenCV and imread() instead.
Usage-
import cv2
img = cv2.imread("new.bmp", 0) #since the image is grayscale, we need only one channel and the value '0' indicates just that
for i in range (img.shape[0]): #traverses through height of the image
for j in range (img.shape[1]): #traverses through width of the image
print img[i][j]
Documentation: http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imread#imread
First of all Use latest Opencv 3. to convert the image to grayscale and print the pixel values follow this code
import cv2
im = cv2.imread("\Test.jpg")
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
#You can directly use gray = cv2.imread("\Test.jpg", 0) for input grayscale image
print(gray)