Skip to content

Commit a027e2a

Browse files
committed
Copy image file model, views, and tests
1 parent eb4ddfe commit a027e2a

3 files changed

Lines changed: 97 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# General imports
2+
import PIL.Image
3+
from PIL.ExifTags import TAGS
4+
import numpy as np
5+
6+
# ETS imports
7+
from traits.api import cached_property, Dict, File, HasStrictTraits, Property
8+
9+
10+
class ImageFile(HasStrictTraits):
11+
""" Model to hold an image file.
12+
"""
13+
filepath = File
14+
15+
metadata = Property(Dict, depends_on="filepath")
16+
17+
def to_array(self):
18+
if not self.filepath:
19+
return np.array([])
20+
21+
with PIL.Image.open(self.filepath) as img:
22+
return np.asarray(img)
23+
24+
@cached_property
25+
def _get_metadata(self):
26+
if not self.filepath:
27+
return {}
28+
29+
with PIL.Image.open(self.filepath) as img:
30+
exif = img._getexif()
31+
32+
if exif:
33+
return {TAGS[k]: v for k, v in exif.items()
34+
if k in TAGS}
35+
else:
36+
return {}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from os.path import dirname, join, split
2+
import unittest
3+
4+
from image_folder import ImageFolder
5+
6+
import ets_tutorial
7+
8+
TUTORIAL_DIR = dirname(ets_tutorial.__file__)
9+
SAMPLE_IMG_DIR = join(TUTORIAL_DIR, "..", "sample_images")
10+
11+
12+
class TestImageFolder(unittest.TestCase):
13+
def test_filters_images(self):
14+
img_folder = ImageFolder(directory=SAMPLE_IMG_DIR)
15+
self.assertEqual(
16+
sorted([split(img.filepath)[1] for img in img_folder.images]),
17+
sorted(["IMG-0311_xmas_2020.JPG", "owls.jpg"])
18+
)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# General imports
2+
from matplotlib.figure import Figure
3+
4+
# ETS imports
5+
from traits.api import Instance, observe
6+
from traitsui.api import Item, ModelView, View
7+
8+
# Local imports
9+
from ets_tutorial.util.mpl_figure_editor import MplFigureEditor
10+
from pycasa.model.image_file import ImageFile
11+
12+
13+
class ImageFileView(ModelView):
14+
""" ModelView for an image file object.
15+
"""
16+
model = Instance(ImageFile)
17+
18+
figure = Instance(Figure)
19+
20+
view = View(
21+
Item("model.filepath", style="readonly", show_label=False),
22+
Item("figure", editor=MplFigureEditor(), show_label=False)
23+
)
24+
25+
@observe("model.filepath")
26+
def build_mpl_figure(self, event):
27+
figure = Figure()
28+
axes = figure.add_subplot(111)
29+
axes.imshow(self.model.to_array())
30+
self.figure = figure
31+
32+
33+
if __name__ == '__main__':
34+
from os.path import dirname, join
35+
import ets_tutorial
36+
37+
TUTORIAL_DIR = dirname(ets_tutorial.__file__)
38+
SAMPLE_IMG_DIR = join(TUTORIAL_DIR, "..", "sample_images")
39+
SAMPLE_IMG1 = join(SAMPLE_IMG_DIR, "IMG-0311_xmas_2020.JPG")
40+
41+
image_file = ImageFile(filepath=SAMPLE_IMG1)
42+
view = ImageFileView(model=image_file)
43+
view.configure_traits()

0 commit comments

Comments
 (0)