~ubuntu-branches/ubuntu/utopic/lazygal/utopic

« back to all changes in this revision

Viewing changes to lazygal/genmedia.py

  • Committer: Package Import Robot
  • Author(s): Michal Čihař, Michal Čihař, Jakub Wilk
  • Date: 2013-06-06 12:05:08 UTC
  • mfrom: (1.2.13)
  • Revision ID: package-import@ubuntu.com-20130606120508-e3g94vl8w9pw7za7
Tags: 0.8-1
[ Michal Čihař ]
* New upstream release.
  - Uses new Genshi templates (Closes: #696682).
  - Correctly handles wronly encoded artist in EXIF (Closes: #696648).
  - Uses GExiv2 (LP: #1074028).
* Depend on GExiv2 instead of pyexiv2.
* Bump standards to 3.9.4.
* Use debhelper 9.

[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
import os
20
20
import logging
21
21
 
22
 
import Image as PILImage
 
22
from PIL import Image as PILImage
23
23
# lazygal has her own ImageFile class, so avoid trouble
24
 
import ImageFile as PILImageFile
25
 
PILImageFile.MAXBLOCK = 1024*1024 # default is 64k, not enough for big pics
 
24
from PIL import ImageFile as PILImageFile
 
25
PILImageFile.MAXBLOCK = 1024 * 1024  # default is 64k, not enough for big pics
26
26
 
27
27
import make
28
28
import genfile
29
29
import eyecandy
30
30
import mediautils
31
 
from lazygal import pyexiv2api as pyexiv2
 
31
from lazygal.pygexiv2 import GExiv2
32
32
 
33
33
 
34
34
THUMB_SIZE_NAME = 'thumb'
77
77
            self.build()
78
78
        except IOError:
79
79
            if not self.source_media.broken:
80
 
                logging.error(_("  %s is BROKEN, skipped")\
 
80
                logging.error(_("  %s is BROKEN, skipped")
81
81
                              % self.source_media.filename)
82
82
                self.source_media.broken = True
83
83
 
87
87
            self.stamp_build()
88
88
 
89
89
    def resize(self, im):
90
 
        self.source_media.get_size() # Probe brokenness
 
90
        self.source_media.get_size()  # Probe brokenness
91
91
        if self.source_media.broken:
92
92
            raise IOError()
93
93
 
101
101
        while not calibrated:
102
102
            try:
103
103
                im.save(self.path, quality=self.webgal.quality,
104
 
                                   **self.webgal.save_options)
 
104
                        **self.webgal.save_options)
105
105
            except IOError, e:
106
106
                if str(e).startswith('encoder error'):
107
107
                    PILImageFile.MAXBLOCK = 2 * PILImageFile.MAXBLOCK
149
149
        return PILImage.open(self.source_media.path)
150
150
 
151
151
    TRANSPOSE_METHODS = {
152
 
        90  : PILImage.ROTATE_90,
153
 
        180 : PILImage.ROTATE_180,
154
 
        270 : PILImage.ROTATE_270,
 
152
        90 : PILImage.ROTATE_90,
 
153
        180: PILImage.ROTATE_180,
 
154
        270: PILImage.ROTATE_270,
155
155
    }
156
156
 
157
157
    def resize(self, im):
158
 
        self.source_media.get_size() # Probe brokenness
 
158
        self.source_media.get_size()  # Probe brokenness
159
159
        if self.source_media.broken:
160
160
            raise IOError()
161
161
 
179
179
    )
180
180
 
181
181
    def copy_metadata(self):
182
 
        imgtags = pyexiv2.ImageMetadata(self.source_media.path)
183
 
        imgtags.read()
184
 
        dest_imgtags = pyexiv2.ImageMetadata(self.path)
185
 
        dest_imgtags.read()
186
 
        imgtags.copy(dest_imgtags)
 
182
        imgtags = GExiv2.Metadata(self.source_media.path)
 
183
        dest_imgtags = GExiv2.Metadata(self.path)
 
184
        for tag in imgtags.get_exif_tags():
 
185
            dest_imgtags[tag] = imgtags[tag]
187
186
 
188
187
        new_size = self.get_size()
189
 
        dest_imgtags['Exif.Photo.PixelXDimension'] = new_size[0]
190
 
        dest_imgtags['Exif.Photo.PixelYDimension'] = new_size[1]
 
188
        dest_imgtags['Exif.Photo.PixelXDimension'] = str(new_size[0])
 
189
        dest_imgtags['Exif.Photo.PixelYDimension'] = str(new_size[1])
191
190
 
192
191
        if self.get_rotation() != 0:
193
192
            # Smaller image has been rotated in order to be displayed correctly
194
193
            # in a web browser. Fix orientation tag accordingly.
195
 
            dest_imgtags['Exif.Image.Orientation'] = 1
 
194
            dest_imgtags['Exif.Image.Orientation'] = '1'
196
195
 
197
196
        # Those are removed from published pics due to pivacy concerns,
198
197
        # unless explicitly told to keep them in. Option to retain GPS
200
199
        if not self.webgal.keep_gps:
201
200
            for tag in self.PRIVATE_IMAGE_TAGS:
202
201
                try:
203
 
                    del dest_imgtags[tag]
 
202
                    dest_imgtags.clear_tag(tag)
204
203
                except KeyError:
205
204
                    pass
206
205
        try:
207
 
            dest_imgtags.write()
208
 
        except ValueError, e:
 
206
            dest_imgtags.save_file()
 
207
        except Exception, e:
209
208
            logging.error(_("Could not copy metadata in reduced picture: %s") % e)
210
209
 
211
210
    def save(self, im):
226
225
        try:
227
226
            thumb = mediautils.VideoThumbnailer(self.source_media.path).get_thumb()
228
227
        except mediautils.TranscodeError, e:
229
 
            logging.error(_("  creating %s thumbnail failed, skipped")\
 
228
            logging.error(_("  creating %s thumbnail failed, skipped")
230
229
                          % self.source_media.filename)
231
230
            logging.info(str(e))
232
231
            self.clean_output()
248
247
 
249
248
        # Use already generated thumbs for better performance (lighter to
250
249
        # rotate, etc.).
251
 
        thumbs = [image.thumb\
 
250
        thumbs = [image.thumb
252
251
                  for image in webgal_dir.get_all_medias_tasks()
253
252
                  if image.thumb and not image.media.broken]
254
253
 
259
258
            albumpic_path = os.path.join(webgal_dir.source_dir.path,
260
259
                                         webgal_dir.source_dir.album_picture)
261
260
            if not os.path.isfile(albumpic_path):
262
 
                logging.error(_("Supplied album picture %s does not exist.")\
 
261
                logging.error(_("Supplied album picture %s does not exist.")
263
262
                              % webgal_dir.source_dir.album_picture)
264
263
 
265
 
            md_dirpic_thumb = webgal_dir._add_size_qualifier(\
266
 
                                           webgal_dir.source_dir.album_picture,
267
 
                                           THUMB_SIZE_NAME)
 
264
            md_dirpic_thumb = webgal_dir._add_size_qualifier(
 
265
                webgal_dir.source_dir.album_picture, THUMB_SIZE_NAME)
268
266
            md_dirpic_thumb = os.path.join(webgal_dir.path, md_dirpic_thumb)
269
267
        else:
270
268
            md_dirpic_thumb = None
310
308
            width, height = self.newsizer.dest_size(self.source_video.get_size())
311
309
            mediautils.WebMTranscoder(self.source_video.path, width, height).convert(self.path)
312
310
        except mediautils.TranscodeError, e:
313
 
            logging.error(_("  transcoding %s failed, skipped")\
 
311
            logging.error(_("  transcoding %s failed, skipped")
314
312
                          % self.source_video.filename)
315
313
            logging.info(str(e))
316
314
            self.source_video.broken = True