Skip to content

Commit 3426198

Browse files
Merge pull request #24 from jonathanrocher/feature/add_stage_4.1
Add end state for stage 4.
2 parents 54b87f9 + c7b1cc4 commit 3426198

File tree

18 files changed

+313
-0
lines changed

18 files changed

+313
-0
lines changed

ets_tutorial/__init__.py

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# First real version of the pycasa ETS pyface application!
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
__version__ = "0.0.1"

stage4.1_first_application/pycasa/app/__init__.py

Whitespace-only changes.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# coding=utf-8
2+
""" TaskApplication object for the Pycasa app.
3+
"""
4+
import logging
5+
6+
from pyface.tasks.api import TasksApplication, TaskFactory
7+
from ..ui.tasks.pycasa_task import PycasaTask
8+
9+
logger = logging.getLogger(__name__)
10+
11+
12+
class PycasaApplication(TasksApplication):
13+
""" An application to say hello.
14+
"""
15+
id = "pycasa_application"
16+
17+
name = "Pycasa"
18+
19+
description = "An example Tasks application that explores image files."
20+
21+
def _task_factories_default(self):
22+
return [
23+
TaskFactory(
24+
id='pycasa.pycasa_task_factory',
25+
name="Main Pycasa Task Factory",
26+
factory=PycasaTask
27+
)
28+
]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
from pycasa.app.app import PycasaApplication
3+
4+
5+
def main():
6+
app = PycasaApplication()
7+
app.run()
8+
9+
10+
if __name__ == '__main__':
11+
main()

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

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from os.path import expanduser
2+
from traits.api import Directory, Event, HasStrictTraits
3+
4+
5+
class FileBrowser(HasStrictTraits):
6+
root = Directory(expanduser("~"))
7+
8+
#: Item last double-clicked on in the tree view
9+
requested_item = Event
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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from os.path import dirname, join
2+
from unittest import TestCase
3+
4+
import numpy as np
5+
6+
from pycasa.model.image_file import ImageFile
7+
8+
import ets_tutorial
9+
10+
TUTORIAL_DIR = dirname(ets_tutorial.__file__)
11+
12+
SAMPLE_IMG_DIR = join(TUTORIAL_DIR, "..", "sample_images")
13+
14+
SAMPLE_IMG1 = join(SAMPLE_IMG_DIR, "IMG-0311_xmas_2020.JPG")
15+
16+
17+
class TestImageFile(TestCase):
18+
def test_no_image_file(self):
19+
img = ImageFile()
20+
self.assertEqual(img.metadata, {})
21+
data = img.to_array()
22+
self.assertIsInstance(data, np.ndarray)
23+
self.assertEqual(data.shape, (0,))
24+
25+
def test_image_metadata(self):
26+
img = ImageFile(filepath=SAMPLE_IMG1)
27+
self.assertNotEqual(img.metadata, {})
28+
for key in ['ExifVersion', 'ExifImageWidth', 'ExifImageHeight']:
29+
self.assertIn(key, img.metadata.keys())
30+
data = img.to_array()
31+
expected_shape = (img.metadata['ExifImageHeight'],
32+
img.metadata['ExifImageWidth'], 3)
33+
self.assertEqual(data.shape, expected_shape)

0 commit comments

Comments
 (0)