Python 用灰阶数据绘制Enthow-Canopy Chaco-img_图
我终于开始尝试查科了,所以这个问题可能很幼稚。目前,我正在尝试绘制一个非常大的8位aka灰度aka单通道图像,类型为numpy.uint8。看起来不管我做什么,图像都是彩色的。下面是我基于Chaco附带的image_plot.py示例编写的代码:Python 用灰阶数据绘制Enthow-Canopy Chaco-img_图,python,enthought,chaco,Python,Enthought,Chaco,我终于开始尝试查科了,所以这个问题可能很幼稚。目前,我正在尝试绘制一个非常大的8位aka灰度aka单通道图像,类型为numpy.uint8。看起来不管我做什么,图像都是彩色的。下面是我基于Chaco附带的image_plot.py示例编写的代码: #!/usr/bin/env python """ Draws an simple RGB image - Left-drag pans the plot. - Mousewheel up and down zooms the plot in an
#!/usr/bin/env python
"""
Draws an simple RGB image
- Left-drag pans the plot.
- Mousewheel up and down zooms the plot in and out.
- Pressing "z" brings up the Zoom Box, and you can click-drag a rectangular
region to zoom. If you use a sequence of zoom boxes, pressing alt-left-arrow
and alt-right-arrow moves you forwards and backwards through the "zoom
history".
"""
# Major library imports
from numpy import zeros, uint8
# Enthought library imports
from enable.api import Component, ComponentEditor
from traits.api import HasTraits, Instance
from traitsui.api import Item, Group, View
# Chaco imports
from chaco.api import ArrayPlotData, Plot, ImageData
from chaco.tools.api import PanTool, ZoomTool
from chaco.tools.image_inspector_tool import ImageInspectorTool, \
ImageInspectorOverlay
#===============================================================================
# # Create the Chaco plot.
#===============================================================================
def _create_plot_component():
# Create some uint8 image data
imageSize = 10000
image = zeros((imageSize,imageSize), dtype=uint8)
for x in range(0,imageSize):
image[x,x] = 255
print image.shape
print type(image[0][0])
# Create a plot data obect and give it this data
pd = ArrayPlotData()
pd.set_data("imagedata", image)
# Create the plot
plot = Plot(pd, default_origin="top left")
plot.x_axis.orientation = "top"
img_plot = plot.img_plot("imagedata")[0]
# Tweak some of the plot properties
plot.bgcolor = "white"
# Attach some tools to the plot
plot.tools.append(PanTool(plot, constrain_key="shift"))
plot.overlays.append(ZoomTool(component=plot,
tool_mode="box", always_on=False))
# imgtool = ImageInspectorTool(img_plot)
# img_plot.tools.append(imgtool)
# plot.overlays.append(ImageInspectorOverlay(component=img_plot,
# image_inspector=imgtool))
return plot
#===============================================================================
# Attributes to use for the plot view.
size = (600, 600)
title="Simple image plot"
bg_color="lightgray"
#===============================================================================
# # Demo class that is used by the demo.py application.
#===============================================================================
class Demo(HasTraits):
plot = Instance(Component)
traits_view = View(
Group(
Item('plot', editor=ComponentEditor(size=size,
bgcolor=bg_color),
show_label=False),
orientation = "vertical"),
resizable=True, title=title
)
def _plot_default(self):
return _create_plot_component()
demo = Demo()
if __name__ == "__main__":
demo.configure_traits()
#--EOF---
您看到的只是伪彩色:2D图像需要某种方式从像素值转换为彩色灰度在这种情况下仍然是彩色的。默认的colormap是彩色的,但是您可以指定colormap关键字参数来获取灰度图像。下面是一个简化的示例:
import numpy as np
from enable.api import Component, ComponentEditor
from traits.api import HasTraits, Instance
from traitsui.api import UItem, View
from chaco.api import ArrayPlotData, Plot, gray
class Demo(HasTraits):
plot = Instance(Component)
traits_view = View(
UItem('plot', editor=ComponentEditor(size=(600, 600))),
resizable=True, title="Simple image plot"
)
def _plot_default(self):
image = np.random.random_integers(0, 255, size=(100, 100))
image = image.astype(np.uint8)
data = ArrayPlotData(imagedata=image)
plot = Plot(data, default_origin="top left")
plot.img_plot("imagedata", colormap=gray)
return plot
demo = Demo()
demo.configure_traits()
您看到的只是伪彩色:2D图像需要某种方式从像素值转换为彩色灰度在这种情况下仍然是彩色的。默认的colormap是彩色的,但是您可以指定colormap关键字参数来获取灰度图像。下面是一个简化的示例:
import numpy as np
from enable.api import Component, ComponentEditor
from traits.api import HasTraits, Instance
from traitsui.api import UItem, View
from chaco.api import ArrayPlotData, Plot, gray
class Demo(HasTraits):
plot = Instance(Component)
traits_view = View(
UItem('plot', editor=ComponentEditor(size=(600, 600))),
resizable=True, title="Simple image plot"
)
def _plot_default(self):
image = np.random.random_integers(0, 255, size=(100, 100))
image = image.astype(np.uint8)
data = ArrayPlotData(imagedata=image)
plot = Plot(data, default_origin="top left")
plot.img_plot("imagedata", colormap=gray)
return plot
demo = Demo()
demo.configure_traits()
我希望上面的代码会产生一个大部分是黑色的图像,对角线上有一条白线。我希望上面的代码会产生一个大部分是黑色的图像,对角线上有一条白线。