Ejemplo n.º 1
0
 def getRawImageFromPath(cls, aMediaCollection, path):
     """Return a raw image to represent the media content of the given file.
             
     Is a class method to be useful during import.
     
     Credits for JPG/EXIF rotation: 
     https://jdhao.github.io/2019/07/31/image_rotation_exif_info/
     
     Model.MediaCollection aMediaCollection
     String path
     Return wx.Image or None
     """
     (_, extension) = os.path.splitext(path)
     extension = extension[1:].lower(
     )  # normalize by removing leading dot and converting to lowercase
     imageType = None
     rawImage = None
     rotation = 'N'  # N: normal, L: left, R: right, M: mirror
     if ((extension == 'jpg') or (extension == 'jpeg')):
         imageType = wx.BITMAP_TYPE_JPEG
         metadata = cls.getMetadataFromPath(path)
         rotation = cls.getRotationFromMetadata(metadata)
     elif (extension == 'png'):
         imageType = wx.BITMAP_TYPE_PNG
     elif (extension == 'gif'):
         imageType = wx.BITMAP_TYPE_GIF
     elif (extension == 'tif'):
         imageType = wx.BITMAP_TYPE_TIF
     if (imageType):
         try:
             rawImage = wx.Image(path, imageType)
         except Exception as exc:
             Logger.warning(
                 'Image.getRawImageFromPath(): Failed to load media "%s" due to\n%s'
                 % (path, exc))
             try:
                 rawImage = wx.Image(
                     os.path.join(Installer.getLibraryPath(),
                                  Image.PreviewImageFilename),
                     wx.BITMAP_TYPE_JPEG)
             except Exception as exc:
                 Logger.error(
                     'Image.getRawImageFromPath(): Failed to load default image due to\n%s'
                     % exc)
             rotation = 'N'
         if (rotation != 'N'):
             Logger.debug('Image.getRawImageFromPath(): Rotating %s' %
                          rotation)
             if (rotation == 'R'):
                 rawImage = rawImage.Rotate90(True)
             elif (rotation == 'L'):
                 rawImage = rawImage.Rotate90(False)
             else:  # must be 'M'
                 rawImage = rawImage.Rotate180()
     else:
         Logger.warning(
             'Image.getRawImageFromPath(): Illegal type in "%s"!' % path)
     return (rawImage)
Ejemplo n.º 2
0
 def getRawImageFromPath(cls, aMediaCollection, path):
     """Return a raw image to represent the media content of the given file.
     
     Model.MediaCollection aMediaCollection
     String path 
     Return wx.Image or None
     """
     rawImage = None
     ffmpeg = aMediaCollection.getConfiguration(
         Movie.ConfigurationOptionFfmpeg)
     if (ffmpeg):
         try:
             Logger.debug('Movie.getRawImageFromPath(): Using "%s"' %
                          ffmpeg)
             duration = cls.getDurationFromPath(aMediaCollection, path)
             if (duration):
                 target = max(
                     0,
                     min(duration * Movie.CaptureFramePosition,
                         duration - 0.1))
             else:
                 target = 5
                 logging.warning(
                     'Movie.getRawImageFromPath(): Cannot determine duration, using %s secs as offset for "%s"'
                     % (target, path))
             targetString = "{:.3f}".format(target)
             logging.debug(
                 'Movie.getRawImageFromPath(): Duration is %s, target frame is %s'
                 % (duration, target))
             args = [
                 ffmpeg,
                 "-ss",
                 targetString,
                 "-i",
                 path,
                 "-map",
                 "v:0",  # first video stream
                 "-frames:v",
                 "1",  # 1 frame
                 "-f",
                 "mjpeg",  # motion jpeg (aka. jpeg since 1 frame) output
                 "pipe:"  # pipe output to stdout
             ]
             #                 proc = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
             #                 (output, _) = proc.communicate()
             #                 if (proc.returncode):
             #                     raise subprocess.CalledProcessError(proc.returncode, args)
             cp = subprocess.run(args,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE)  # Python 3
             if (cp.returncode):
                 raise subprocess.CalledProcessError(cp.returncode, args)
             if (not cp.stdout):
                 raise subprocess.CalledProcessError(-2, args)
             stream = BytesIO(cp.stdout)
             #                 rawImage = wx.ImageFromStream(stream, type=wx.BITMAP_TYPE_JPEG)
             rawImage = wx.Image(stream,
                                 type=wx.BITMAP_TYPE_JPEG)  # wxPython 4
         except Exception as e:
             Logger.error(
                 'Movie.getRawImageFromPath(): Cannot retrieve frame from "%s" due to error:\n%s'
                 % (path, e))
             rawImage = None
     else:
         Logger.warning(
             'Movie.getRawImageFromPath(): No ffmpeg specified with option "%s"'
             % Movie.ConfigurationOptionFfmpeg)
         rawImage = None
     if (rawImage == None):
         try:
             rawImage = wx.Image(
                 os.path.join(Installer.getLibraryPath(),
                              Movie.PreviewImageFilename),
                 wx.BITMAP_TYPE_JPEG)
         except Exception as exc:
             Logger.error(
                 'Movie.getRawImageFromPath(): Cannot load default movie image due to\n%s'
                 % exc)
             rawImage = None
     return (rawImage)