Skip to content

Commit 2a6cc87

Browse files
Add to_array for an ImageFile model.
1 parent 9d83000 commit 2a6cc87

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

stage4.1_first_application/pycasa/model/image_file.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
# General imports
12
import PIL.Image
23
from PIL.ExifTags import TAGS
4+
import numpy as np
35

6+
# ETS imports
47
from traits.api import Dict, File, HasStrictTraits, observe
58

69

@@ -11,10 +14,15 @@ class ImageFile(HasStrictTraits):
1114

1215
metadata = Dict
1316

17+
def to_array(self):
18+
with PIL.Image.open(self.filepath) as img:
19+
return np.asarray(img)
20+
1421
@observe("filepath")
1522
def load_metadata(self, event):
16-
img = PIL.Image.open(self.filepath)
17-
exif = img._getexif()
23+
with PIL.Image.open(self.filepath) as img:
24+
exif = img._getexif()
25+
1826
if exif:
1927
self.metadata = {TAGS[k]: v for k, v in exif.items()
2028
if k in TAGS}

stage4.1_first_application/pycasa/ui/image_file_view.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,5 @@ class ImageFileView(ModelView):
2727
def build_mpl_figure(self, event):
2828
figure = Figure()
2929
axes = figure.add_subplot(111)
30-
data = imageio.imread(self.model.filepath)
31-
axes.imshow(data)
30+
axes.imshow(self.model.to_array())
3231
self.figure = figure

0 commit comments

Comments
 (0)