aboutsummaryrefslogtreecommitdiffstats
path: root/examples/ethumb/ethumb.py
blob: 88344d9f13936b41aaf77cd288cea2e5cd2bc146 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python
# encoding: utf-8

import os
import sys
import time
import logging

from efl import ecore
from efl import ethumb


script_path = os.path.dirname(os.path.abspath(__file__))

TESTS = [
    ("1. Generating a standard FDO thumbnail (default)...", {}),
    ("2. Generating a large FDO thumbnail...", {
        'thumb_fdo': ethumb.ETHUMB_THUMB_LARGE,
    }),
    ("3. Generating a very large JPG thumbnail...", {
        'thumb_format': ethumb.ETHUMB_THUMB_JPEG,
        'thumb_size': (512, 512),
    }),
    ("4. Generating a cropped thumbnail...", {
        'thumb_aspect': ethumb.ETHUMB_THUMB_CROP,
    }),
    ("5. Generating a rotated thumbnail...", {
        'thumb_orientation': ethumb.ETHUMB_THUMB_ROTATE_180,
        'thumb_size': (256, 256),
        'thumb_aspect': ethumb.ETHUMB_THUMB_KEEP_ASPECT,
    }),
    ("6. Generating a poor quality thumbnail in this folder...", {
        'thumb_orientation': ethumb.ETHUMB_THUMB_ORIENT_NONE,
        'thumb_path': os.path.join(script_path, 'big_poor.jpg'),
        'thumb_format': ethumb.ETHUMB_THUMB_JPEG,
        'thumb_size': (512, 512),
        'thumb_quality': 10,
    }),
]


# parse command line arguments
if len(sys.argv) != 2:
    print("Python Ethumb test application.\n\n" \
          "usage: ethumb.py filename\n")
    exit(1)
filename = sys.argv[1]
print("Original file: %s\n" % filename)


# setup efl logging (you also need to set EINA_LOG_LEVEL=X)
l = logging.getLogger("efl")
h = logging.StreamHandler()
h.setFormatter(logging.Formatter("EFL %(levelname)s %(message)s"))
l.addHandler(h)
l.setLevel(logging.DEBUG)


# create a single Ethumb instance
et = ethumb.Ethumb()
in_progress = False
start_time = 0.0



def generate_cb(et, success):
    global in_progress

    # thumbnail completed
    if success is True:
        # print out the result
        print("   Completed in %.3f seconds." % (time.time() - start_time))
        print("   Thumb: '%s'\n" % et.thumb_path[0])
    else:
        print("   ERROR! aborting.")
        ecore.main_loop_quit()
    in_progress = False


def start_thumb(label, params):
    global in_progress
    global start_time

    # setup ethumb params
    et.file = filename
    for name, val in params.items():
        setattr(et, name, val)

    # print test name and keep track of start time
    print(label)
    start_time = time.time()

    # start the thumbnailing process
    ret = et.generate(generate_cb) # TODO test args and kargs
    if ret is False:
        print("Error starting the thumbnailer!")
        ecore.main_loop_quit()
    else:
        in_progress = True


def idler_cb():
    # Ethumb cannot generate more than one thumb at the same time
    if in_progress:
        return ecore.ECORE_CALLBACK_RENEW

    try:
        # advance to the next test
        label, params = TESTS.pop(0)
        start_thumb(label, params)
    except IndexError:
        # all done, quit.
        print("Test Completed!")
        ecore.main_loop_quit()

    return ecore.ECORE_CALLBACK_RENEW


# start the idler and the main loop
idler = ecore.Idler(idler_cb)
ecore.main_loop_begin()

# free used resource
et.delete()