Skip to content

Commit 8c9c378

Browse files
Merge pull request #32 from jonathanrocher/feature/add_step_5.1
Feature: add step 5.1
2 parents 80bbcfe + e4493b6 commit 8c9c378

24 files changed

+508
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Second real version of the pycasa ETS pyface application!
2+
Building on the application state 4.2, this version simply adds an editor and a
3+
view for a folder to be visualized.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
__version__ = "0.0.1"

stage5.1_fuller_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()

stage5.1_fuller_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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# General imports
2+
import os
3+
import PIL.Image
4+
from PIL.ExifTags import TAGS
5+
import numpy as np
6+
7+
# ETS imports
8+
from traits.api import cached_property, Dict, File, HasStrictTraits, Property
9+
10+
SUPPORTED_FORMATS = [".png", ".jpg", ".jpeg"]
11+
12+
13+
class ImageFile(HasStrictTraits):
14+
""" Model to hold an image file.
15+
"""
16+
filepath = File
17+
18+
metadata = Property(Dict, depends_on="filepath")
19+
20+
def to_array(self):
21+
file_ext = os.path.splitext(self.filepath)[1].lower()
22+
if not self.filepath or file_ext not in SUPPORTED_FORMATS:
23+
return np.array([])
24+
25+
with PIL.Image.open(self.filepath) as img:
26+
return np.asarray(img)
27+
28+
@cached_property
29+
def _get_metadata(self):
30+
file_ext = os.path.splitext(self.filepath)[1].lower()
31+
if not self.filepath or file_ext not in SUPPORTED_FORMATS:
32+
return {}
33+
34+
with PIL.Image.open(self.filepath) as img:
35+
exif = img._getexif()
36+
37+
if exif:
38+
return {TAGS[k]: v for k, v in exif.items()
39+
if k in TAGS}
40+
else:
41+
return {}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
# General imports
3+
import glob
4+
from os.path import expanduser, isdir, split
5+
6+
import pandas as pd
7+
8+
# ETS imports
9+
from traits.api import (
10+
Directory, HasStrictTraits, Instance, List, observe,
11+
)
12+
13+
# Local imports
14+
from pycasa.model.image_file import ImageFile
15+
16+
SUPPORTED_FORMATS = [".png", ".jpg", ".jpeg", ".PNG", ".JPG", ".JPEG"]
17+
18+
19+
class ImageFolder(HasStrictTraits):
20+
""" Model for a folder of images.
21+
"""
22+
directory = Directory(expanduser("~"))
23+
24+
images = List(Instance(ImageFile))
25+
26+
def __init__(self, **traits):
27+
super(ImageFolder, self).__init__(**traits)
28+
if not isdir(self.directory):
29+
msg = f"The provided directory isn't a real directory: " \
30+
f"{self.directory}"
31+
raise ValueError(msg)
32+
33+
@observe("directory")
34+
def _get_images(self, event):
35+
self.images = [
36+
ImageFile(filepath=file)
37+
for fmt in SUPPORTED_FORMATS
38+
for file in glob.glob(f"{self.directory}/*{fmt}")
39+
]
40+
41+
def create_metadata_df(self):
42+
return pd.DataFrame(
43+
[img.metadata for img in self.images],
44+
index=[split(img.filepath)[1] for img in self.images]
45+
)

stage5.1_fuller_application/pycasa/model/tests/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)