| 1 |
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# This software is provided without warranty under the terms of the BSD |
| 5 |
# license included in LICENSE.txt and may be redistributed only under |
| 6 |
# the conditions described in the aforementioned license. The license |
| 7 |
# is also available online at http://www.enthought.com/licenses/BSD.txt |
| 8 |
# |
| 9 |
# Thanks for using Enthought open source! |
| 10 |
|
| 11 |
""" |
| 12 |
Visualization |
| 13 |
============= |
| 14 |
|
| 15 |
Traits allows you to instantly create a graphical user interface |
| 16 |
for your ``HasTraits`` classes. If you have TraitsUI and a suitable |
| 17 |
GUI toolkit (such as PyQt5 or PySide2) installed in your Python |
| 18 |
environment then you can create a dialog view of an instance of |
| 19 |
your class by simply doing:: |
| 20 |
|
| 21 |
>>> image.configure_traits() |
| 22 |
|
| 23 |
This gives you a default UI out of the box with no further effort, |
| 24 |
but it usually is not what you would want to provide for your users. |
| 25 |
|
| 26 |
With a little more effort using the features of TraitsUI, you can |
| 27 |
design a dialog which is more pleasing:: |
| 28 |
|
| 29 |
from traitsui.api import Item, View |
| 30 |
|
| 31 |
class Image(HasStrictTraits): |
| 32 |
... |
| 33 |
|
| 34 |
view = View( |
| 35 |
Item('filename'), |
| 36 |
Item('sample_id', label='Sample ID'), |
| 37 |
Item('operator'), |
| 38 |
Item('date_acquired'), |
| 39 |
Item('scan_width', label='Width (m):'), |
| 40 |
Item('scan_height', label='Height (m):') |
| 41 |
) |
| 42 |
|
| 43 |
TraitsUI can be used as the building block for complete scientific |
| 44 |
applications, including 2D and 3D plotting. |
| 45 |
|
| 46 |
""" |
| 47 |
|
| 48 |
import os |
| 49 |
import datetime |
| 50 |
|
| 51 |
from PIL import Image as PILImage |
| 52 |
import numpy as np |
| 53 |
|
| 54 |
from traits.api import ( |
| 55 |
Array, Date, File, Float, HasStrictTraits, Property, Str, Tuple, |
| 56 |
cached_property, observe |
| 57 |
) |
| 58 |
from traitsui.api import Item, View |
| 59 |
|
| 60 |
|
| 61 |
class Image(HasStrictTraits): |
| 62 |
""" An SEM image stored in a file. """ |
| 63 |
|
| 64 |
#: The filename of the image. |
| 65 |
filename = File(exists=True) |
| 66 |
|
| 67 |
#: The ID of the sample that is being imaged. |
| 68 |
sample_id = Str() |
| 69 |
|
| 70 |
#: The name of the operator who acquired the image. |
| 71 |
operator = Str("N/A") |
| 72 |
|
| 73 |
#: The date the image was acquired. |
| 74 |
date_acquired = Date() |
| 75 |
|
| 76 |
#: The size of the image. |
| 77 |
scan_size = Tuple(Float, Float) |
| 78 |
|
| 79 |
#: The width of the image. |
| 80 |
scan_width = Property(Float, depends_on='scan_size') |
| 81 |
|
| 82 |
#: The height of the image. |
| 83 |
scan_height = Property(Float, depends_on='scan_size') |
| 84 |
|
| 85 |
#: The image as a 2D numpy array |
| 86 |
image = Array(shape=(None, None), dtype='uint8') |
| 87 |
|
| 88 |
#: The area of each pixel. |
| 89 |
pixel_area = Property(Float, depends_on='scan_height,scan_width,image') |
| 90 |
|
| 91 |
#: The histogram of pixel intensities. |
| 92 |
histogram = Property(Array, depends_on='image') |
| 93 |
|
| 94 |
def threshold(self, low=0, high=255): |
| 95 |
""" Compute a threshold mask for the array. """ |
| 96 |
return (self.image >= low) & (self.image <= high) |
| 97 |
|
| 98 |
# Trait observers |
| 99 |
|
| 100 |
@observe('filename') |
| 101 |
def read_image(self, event): |
| 102 |
pil_image = PILImage.open(self.filename).convert("L") |
| 103 |
self.image = np.array(pil_image) |
| 104 |
|
| 105 |
# Trait default methods |
| 106 |
|
| 107 |
def _date_acquired_default(self): |
| 108 |
return datetime.date.today() |
| 109 |
|
| 110 |
def _scan_size_default(self): |
| 111 |
return (1e-5, 1e-5) |
| 112 |
|
| 113 |
# Trait property methods |
| 114 |
|
| 115 |
def _get_scan_width(self): |
| 116 |
return self.scan_size[0] |
| 117 |
|
| 118 |
def _set_scan_width(self, value): |
| 119 |
self.scan_size = (value, self.scan_size[1]) |
| 120 |
|
| 121 |
def _get_scan_height(self): |
| 122 |
return self.scan_size[1] |
| 123 |
|
| 124 |
def _set_scan_height(self, value): |
| 125 |
self.scan_size = (self.scan_size[0], value) |
| 126 |
|
| 127 |
def _get_pixel_area(self): |
| 128 |
if self.image.size > 0: |
| 129 |
return self.scan_height * self.scan_width / self.image.size |
| 130 |
else: |
| 131 |
return 0 |
| 132 |
|
| 133 |
@cached_property |
| 134 |
def _get_histogram(self): |
| 135 |
hist, bins = np.histogram( |
| 136 |
self.image, |
| 137 |
bins=256, |
| 138 |
range=(0, 256), |
| 139 |
density=True, |
| 140 |
) |
| 141 |
return hist |
| 142 |
|
| 143 |
# TraitsUI view declaration |
| 144 |
|
| 145 |
view = View( |
| 146 |
Item('filename'), |
| 147 |
Item('sample_id', label='Sample ID'), |
| 148 |
Item('operator'), |
| 149 |
Item('date_acquired'), |
| 150 |
Item('scan_width', label='Width (m):'), |
| 151 |
Item('scan_height', label='Height (m):'), |
| 152 |
Item( |
| 153 |
'pixel_area', |
| 154 |
format_func=lambda area: "{:0.3f} µm²".format(area * 1e18), |
| 155 |
style='readonly', |
| 156 |
), |
| 157 |
) |
| 158 |
|
| 159 |
|
| 160 |
# --------------------------------------------------------------------------- |
| 161 |
# Demo code |
| 162 |
# --------------------------------------------------------------------------- |
| 163 |
|
| 164 |
this_dir = os.path.dirname(__file__) |
| 165 |
image_dir = os.path.join(this_dir, "images") |
| 166 |
filename = os.path.join(image_dir, "sample_0001.png") |
| 167 |
|
| 168 |
# load the image |
| 169 |
image = Image( |
| 170 |
filename=filename, |
| 171 |
operator="Hannes", |
| 172 |
sample_id="0001", |
| 173 |
date_acquired=datetime.datetime.today(), |
| 174 |
scan_size=(1e-5, 1e-5), |
| 175 |
) |
| 176 |
|
| 177 |
demo = image |
| 178 |
|
| 179 |
if __name__ == "__main__": |
| 180 |
demo.configure_traits() |