~osomon/pyexiv2/pyexiv2-0.3

« back to all changes in this revision

Viewing changes to src/pygtk-example.py

Check typeName for nullness.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 
4
4
# ******************************************************************************
5
5
#
6
 
# Copyright (C) 2007 Olivier Tilloy <olivier@tilloy.net>
 
6
# Copyright (C) 2007-2010 Olivier Tilloy <olivier@tilloy.net>
7
7
#
8
8
# This file is part of the pyexiv2 distribution.
9
9
#
21
21
# along with pyexiv2; if not, write to the Free Software
22
22
# Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
23
23
#
24
 
#
25
 
# File:      pyqt-example.py
26
 
# Author(s): Olivier Tilloy <olivier@tilloy.net>
 
24
# Author: Olivier Tilloy <olivier@tilloy.net>
27
25
#
28
26
# ******************************************************************************
29
27
 
30
28
import sys
31
29
import gtk
32
 
import pyexiv2
 
30
from pyexiv2 import ImageMetadata
 
31
 
33
32
 
34
33
if __name__ == '__main__':
35
 
        """
36
 
        Example of how to combine pygtk and pyexiv2 to display thumbnail data.
37
 
 
38
 
        Minimalistic example of how to load and display with pygtk the thumbnail
39
 
        data extracted from an image using the method Image.getThumbnailData().
40
 
        The path to the image file from which the thumbnail data should be extracted
41
 
        should be passed as the only argument of the script.
42
 
 
43
 
        It is of course assumed that you have pygtk installed.
44
 
        """
45
 
        if (len(sys.argv) != 2):
46
 
                print 'Usage: ' + sys.argv[0] + ' path/to/picture/file/containing/jpeg/thumbnail'
47
 
                sys.exit(1)
48
 
        else:
49
 
                app = gtk.Window(gtk.WINDOW_TOPLEVEL)
50
 
 
51
 
                # Load the image, read the metadata and extract the thumbnail data
52
 
                image = pyexiv2.Image(sys.argv[1])
53
 
                image.readMetadata()
54
 
                ttype, tdata = image.getThumbnailData()
55
 
 
56
 
                # Create a GTK pixbuf loader to read the thumbnail data
57
 
                pbloader = gtk.gdk.PixbufLoader()
58
 
                pbloader.write(tdata)
59
 
                # Get the resulting pixbuf and build an image to be displayed
60
 
                pixbuf = pbloader.get_pixbuf()
61
 
                pbloader.close()
62
 
                imgwidget = gtk.Image()
63
 
                imgwidget.set_from_pixbuf(pixbuf)
64
 
 
65
 
                # Show the application's main window
66
 
                # Note: closing the window will not terminate the application as no
67
 
                # appropriate signal has been defined.
68
 
                app.add(imgwidget)
69
 
                imgwidget.show()
70
 
                app.show()
71
 
                gtk.main()
 
34
    """
 
35
    Example of how to combine pygtk and pyexiv2 to display thumbnail data.
 
36
 
 
37
    Minimalistic example of how to load and display with pygtk the thumbnail
 
38
    data extracted from an image.
 
39
    The path to the image file from which the thumbnail data should be extracted
 
40
    should be passed as the only argument of the script.
 
41
 
 
42
    It is of course assumed that you have pygtk installed.
 
43
    """
 
44
    if (len(sys.argv) != 2):
 
45
        print 'Usage: ' + sys.argv[0] + ' path/to/picture/file/containing/jpeg/thumbnail'
 
46
        sys.exit(1)
 
47
 
 
48
    app = gtk.Window(gtk.WINDOW_TOPLEVEL)
 
49
    app.connect('destroy', lambda app: gtk.main_quit())
 
50
 
 
51
    # Load the image, read the metadata and extract the thumbnail data
 
52
    metadata = ImageMetadata(sys.argv[1])
 
53
    metadata.read()
 
54
    previews = metadata.previews
 
55
    if not previews:
 
56
        print "This image doesn't contain any thumbnail."
 
57
        sys.exit(1)
 
58
 
 
59
    # Get the largest preview available
 
60
    preview = previews[-1]
 
61
 
 
62
    # Create a pixbuf loader to read the thumbnail data
 
63
    pbloader = gtk.gdk.PixbufLoader()
 
64
    pbloader.write(preview.data)
 
65
    # Get the resulting pixbuf and build an image to be displayed
 
66
    pixbuf = pbloader.get_pixbuf()
 
67
    pbloader.close()
 
68
    imgwidget = gtk.Image()
 
69
    imgwidget.set_from_pixbuf(pixbuf)
 
70
 
 
71
    # Show the application's main window
 
72
    app.add(imgwidget)
 
73
    imgwidget.show()
 
74
    app.show()
 
75
    sys.exit(gtk.main())
 
76