Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying a grayscale Image

My aim:

  1. Read an image into the PIL format.
  2. Convert it to grayscale.
  3. Plot the image using pylab.

Here is the code i'm using:

from PIL import Image
from pylab import *
import numpy as np

inputImage='C:\Test\Test1.jpg'
##outputImage='C:\Test\Output\Test1.jpg'

pilImage=Image.open(inputImage)
pilImage.draft('L',(500,500))
imageArray= np.asarray(pilImage)

imshow(imageArray)

##pilImage.save(outputImage)

axis('off')

show()

My Problem: The image get's displayed like the colours are inverted.

This is the Original Image

This is how it appears in the Python Window

But I know that the image is getting converted to grayscale, because when I write it to the disk it is appearing as a grayscale image.(Just as I expect).

I feel that the problem is somewhere in the numpy conversion.

I've just started programming in Python for Image Processing. And Tips and Guideline will also be appreciated.

like image 683
Colenso Castellino Avatar asked Jan 01 '13 15:01

Colenso Castellino


1 Answers

You want to over-ride the default color map:

imshow(imageArray, cmap="Greys_r")

Here's a page on plotting images and pseudocolor in matplotlib .

like image 143
YXD Avatar answered Oct 06 '22 01:10

YXD