~osomon/pyexiv2/pyexiv2-0.3

16 by Olivier Tilloy
Added a very simple python script that demonstrates how to use pygtk to display the thumbnail data extracted from a picture using the method Image.getThumbnailData().
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
4
# ******************************************************************************
5
#
241.2.1 by Olivier Tilloy
Updated copyright headers for 2010.
6
# Copyright (C) 2007-2010 Olivier Tilloy <olivier@tilloy.net>
16 by Olivier Tilloy
Added a very simple python script that demonstrates how to use pygtk to display the thumbnail data extracted from a picture using the method Image.getThumbnailData().
7
#
8
# This file is part of the pyexiv2 distribution.
9
#
10
# pyexiv2 is free software; you can redistribute it and/or
11
# modify it under the terms of the GNU General Public License
12
# as published by the Free Software Foundation; either version 2
13
# of the License, or (at your option) any later version.
14
#
15
# pyexiv2 is distributed in the hope that it will be useful,
16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
# GNU General Public License for more details.
19
#
20
# You should have received a copy of the GNU General Public License
21
# along with pyexiv2; if not, write to the Free Software
22
# Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
23
#
243.1.2 by Olivier Tilloy
Adapted the pygtk thumbnail example.
24
# Author: Olivier Tilloy <olivier@tilloy.net>
16 by Olivier Tilloy
Added a very simple python script that demonstrates how to use pygtk to display the thumbnail data extracted from a picture using the method Image.getThumbnailData().
25
#
26
# ******************************************************************************
27
28
import sys
29
import gtk
243.1.2 by Olivier Tilloy
Adapted the pygtk thumbnail example.
30
from pyexiv2 import ImageMetadata
31
16 by Olivier Tilloy
Added a very simple python script that demonstrates how to use pygtk to display the thumbnail data extracted from a picture using the method Image.getThumbnailData().
32
33
if __name__ == '__main__':
243.1.2 by Olivier Tilloy
Adapted the pygtk thumbnail example.
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)
273 by Damien Moore
PyGTK example application: connect to the 'destroy' signal to exit when closing the window.
49
    app.connect('destroy', lambda app: gtk.main_quit())
243.1.2 by Olivier Tilloy
Adapted the pygtk thumbnail example.
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)
331 by Olivier Tilloy
Cosmetics: replaced non-breakable spaces by normal white spaces in comments.
65
    # Get the resulting pixbuf and build an image to be displayed
243.1.2 by Olivier Tilloy
Adapted the pygtk thumbnail example.
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