2013-01-01 20 views
5

Mój cel:Wyświetlanie obrazu w skali szarości

  1. odczytać obrazu do formatu PIL.
  2. Konwersja do skali szarości.
  3. Wykreśl obraz za pomocą pylab.

Oto kod używam:

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() 

Mój problem: Obraz get wyświetlane jak kolory są odwrócone.

This is the Original Image

This is how it appears in the Python Window

Ale wiem, że obraz jest uzyskiwanie konwertowane do skali szarości, bo kiedy piszę go na dysku to pojawia się jako skali szarości. (Tak jak ja się spodziewać).

Czuję, że problem tkwi gdzieś w konwersjach numpy.

Właśnie rozpocząłem programowanie w Pythonie dla przetwarzania obrazu. Doceniamy również wskazówki i wytyczne.

Odpowiedz

13

Chcesz Over-ride domyślną mapę kolorów:

imshow(imageArray, cmap="Greys_r") 

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

+0

próbowałem tego. Dostaję obraz w skali szarości, ale jest to odwrócony obraz w skali szarości. –

+0

OK - zaktualizowano odwrotną mapą kolorów – YXD

+0

To rozwiązało mój problem. Czy możesz mi wyjaśnić, co robi ta część i dlaczego jest potrzebna dla imshow()? A może skieruj mnie na stronę, która może to wyjaśnić. –

2

Daje to B & W obrazu:

pilImage=Image.open(inputImage) 
pilImage = pilImage.convert('1') #this convert to black&white 
pilImage.draft('L',(500,500)) 

pilImage.save('outfile.png') 

Od sposobu convertdocs:

convert 

im.convert(mode) => image 

Returns a converted copy of an image. 
When translating from a palette image, this translates pixels through the palette. 
If mode is omitted, a mode is chosen so that all information in the image and the palette can be represented without a palette. 

When from a colour image to black and white, the library uses the ITU-R 601-2 luma transform: 

    L = R * 299/1000 + G * 587/1000 + B * 114/1000 
When converting to a bilevel image (mode "1"), the source image is first converted to black and white. 
Resulting values larger than 127 are then set to white, and the image is dithered. 
To use other thresholds, use the point method. 
Powiązane problemy