2011-05-16 152 views
17

我有一些颜色情节,我需要保存在灰度。有没有简单的方法来做到这一点,而不改变绘图格式?保存matplotlib阴谋灰度

+4

围绕这个问题进行了一些讨论,请参阅[将颜色转换为BW /灰度](http://old.nabble.com /convert-figure-from-color-to-BW-grayscale-td29756144.html)。不知道在最新版本的matplotlib中'set_gray'属性是否可用。 – chl 2011-05-16 18:29:43

+0

之后你可以使用imagemagick:'convert -type Grayscale old.png new.png'我认为已经有一些关于SO cf的东西[link](http://stackoverflow.com/questions/3823752/display-image-as-灰度使用matplotlib) – 2011-05-17 20:53:27

回答

4

其实这是之前询问过的。这是一个相当不错的答案,这对谷歌自带2日(今天的):
Display image as grayscale using matplotlib

而且该解决方案非常相似,苏基的...

哦,我无聊,所以我张贴在这里也是一个完全成熟的代码:

import numpy as np 
import pylab as p 
xv=np.ones(4)*.5 
yv=np.arange(0,4,1) 
xv1=np.ones(4)*-.5 
yv1=np.arange(0,4,1) 

#red vertical line on the right 
yv2=np.arange(0,1.5,0.1) 
xv2=np.ones_like(yv2)*.7 

#red vertical line on the left 
yv3=np.arange(0,2,0.01) 
xv3=np.ones_like(yv3)*-0.7 

### 
xc=np.arange(-1.4,2,0.05) 
yc=np.ones_like(xc)*1 

fig = p.figure() 
ax1 = fig.add_subplot(111) 
#adjustprops = dict(left=0.12, bottom=0.2, right=0.965, top=0.96, wspace=0.13, hspace=0.37) 
#fig.subplots_adjust(**adjustprops) 
ax1.plot(xv,yv, color='blue', lw=1, linestyle='dashed') 
ax1.plot(xv1,yv1, 'green', linestyle='dashed') 
ax1.plot(np.r_[-1:1:0.2],np.r_[-1:1:0.2],'red') 
ax1.plot(xc,yc, 'k.', markersize=3) 

p.savefig('colored_image.png') 

import matplotlib.image as mpimg 
import matplotlib.cm as cm 
import Image 

figprops = dict(figsize=(10,10), dpi=100) 
fig1 = p.figure(**figprops) 
#fig1 = p.figure() 
#ax1 = fig.add_subplot(111) 
adjustprops = dict() 
image=Image.open('colored_image.png').convert("L") 
arr=np.asarray(image) 
p.figimage(arr,cmap=cm.Greys_r) 
p.savefig('grayed.png') 
p.savefig('grayed.pdf',papertype='a4',orientation='portrait') 

这将产生颜色的曲线,比读它,将其转换为灰度,并保存为PNG和PDF格式。

+1

这个答案不是很好,因为它需要栅格化绘图和任何字体。光栅化转换为.png时发生。最好将pdf写入文件系统,然后将其转换为灰度,也许使用ghostscript。 “导入图像” – 2015-04-25 05:10:56

+0

不再有效。而是使用“从PIL导入图像” – warunapww 2017-09-08 21:28:27

6

目前很难直接从matplotlib做到,但是在“将来”他们打算支持set_gray(True)对该图的调用(请参阅邮件列表线程here)。

你最好的选择将是其保存在颜色,并将其转换,无论是在与PIL蟒蛇:

import Image 
Image.open('color.png').convert('L').save('bw.png') 

或者在命令行中使用ImageMagick的:

convert -type Grayscale color.png bw.png 
+0

像其他答案一样,这个答案不是很令人满意,因为它栅格化的情节。 – 2015-04-25 05:11:58

0

,并增加Mu Mind的解决方案

如果因为某种原因想要避免将它写入文件中,可以使用StringIO类似于文件:


import Image 
import pylab 
from StringIO import StringIO 

pylab.plot(range(10),[x**2 for x in range(10)]) 

IO = StringIO() 
pylab.savefig(IO,format='png') 
IO.seek(0) 

#this, I stole from Mu Mind solution 
Image.open(IO).convert('L').show() 
+0

像其他答案一样,这个答案不太令人满意,因为它栅格化了情节。 – 2015-04-25 05:12:20

1

我也在为这个问题苦苦挣扎。据我所知,matplotlib不支持直接转换为灰度,但您可以保存一个颜色pdf,然后用ghostscript将其转换为灰度:

gs -sOutputFile = gray.pdf -sDEVICE = pdfwrite -sColorConversionStrategy = Gray -dProcessColorModel =/DeviceGray -dNOPAUSE -dBATCH -dAutoRotatePages =/None color.pdf

+0

为什么不只是使用灰度样式表? http://matplotlib.org/examples/style_sheets/plot_grayscale.html – 2015-04-25 05:16:01

+0

这实际上并没有将彩色图转换为灰度。它只是使一些元素灰色默认。我绘制了我想要导出为网页的两种颜色并转换为灰色进行打印的情况。 – 2015-04-25 05:48:20

+0

您可以使用上下文管理器中的样式表。用这些函数将你的调用封装到非数据处理绘图功能中。 – 2015-04-25 05:58:08