我的目标:
以下是我正在使用的代码:
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()
我的问题: 图像显示就像颜色被反转一样。
但我知道图像会转换为灰度图像,因为当我将图像写入磁盘时,它会显示为灰度图像。(正如我所料)。
我觉得这个问题出现在numpy转换的某个地方。
我刚开始使用Python进行图像处理编程。 提示和指南也将受到赞赏。
答案 0 :(得分:13)
您想要覆盖默认的颜色贴图:
imshow(imageArray, cmap="Greys_r")
Here's a page on plotting images and pseudocolor in matplotlib。
答案 1 :(得分:2)
这会生成B& W图像:
pilImage=Image.open(inputImage)
pilImage = pilImage.convert('1') #this convert to black&white
pilImage.draft('L',(500,500))
pilImage.save('outfile.png')
来自convert
方法docs:
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.