Updating image plot size

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Updating image plot size

Tony S Yu
I was playing around with the image processing example (https://github.com/enthought/enaml/tree/master/examples/image_processing) and ran into some issues with how image sizes are updated.

When I initialize the model (`ProcessorModel` in the official example, `ImageModel` below) with the desired image, both the image and overlay drawn correctly. On the other hand, if I initialize without an image (so that the model defaults to a 100 x 100 array), then the image is drawn correctly, but once the overlay is drawn, the image shrinks to 100 x 100.

It seems that something in the plot is "remembering" in initial image shape, and the overlay uses that initial shape. Thoughts/suggestions?

Thanks,
-Tony

To test, run the code below; press the filter button to show the overlay.

#~~~
import numpy as np

import traits.api as tts
import chaco.api as chaco

from skimage.io import imread
from skimage.util import img_as_float


class ImageModel(tts.HasTraits):

    original_image = tts.Array()
    filename = tts.File

    plot_data = tts.Instance(chaco.ArrayPlotData)
    plot_object = tts.Instance(chaco.Plot)

    image_plot = tts.Any

    def __init__(self, *args, **kwargs):
        super(ImageModel, self).__init__(*args, **kwargs)
        self.plot_data = chaco.ArrayPlotData(original_image=self.original_image)
        p = chaco.Plot(self.plot_data, default_origin='top left', padding=0)
        self.image_plot = p.img_plot('original_image', colormap=chaco.bone)[0]
        self.plot_object = p

    def _original_image_default(self):
        return np.zeros((100, 100))


    def _filename_changed(self):
        self.original_image = img_as_float(imread(self.filename, flatten=True))
        self.plot_data['original_image'] = self.original_image


def add_overlay(model):
    shape = model.original_image.shape + (4,)
    overlay = np.zeros(shape, dtype=np.uint8)
    overlay[::20, :, 0] = 255
    overlay[::20, :, 3] = 255

    model.plot_data['overlay'] = overlay
    model.plot_object.img_plot('overlay')


enamldef Main(MainWindow):
    attr model
    title = 'Envision'
    Container:
        EnableCanvas:
            constraints = [width >= 600, height >= 400]
            component << model.plot_object
        PushButton:
            text = 'Filter'
            clicked ::
                model = add_overlay(model)


if __name__ == '__main__':
    # with these two lines, the overlay shows up correctly.
    # image = img_as_float(imread('dc_metro.jpg'))[..., 0]
    # image_model = ImageModel(original_image=image)

    # with these two lines, the default image size (0, 0) is used for overlay.
    image_model = ImageModel()
    image_model.filename = 'dc_metro.jpg'

    window = Main(model=image_model)
    window.show()

_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev