예제 #1
0
파일: browser.py 프로젝트: wazari972/picty
 def drag_get_signal(self, widget, drag_context, selection_data, info,
                     timestamp):
     '''
     callback triggered to set the selection_data payload
     (viewer is the source of the drop)
     '''
     if self.drag_item == None:
         return
     if info == self.TARGET_TYPE_IMAGE:
         selection_data.set('image-filename', 8, self.drag_item.uid)
     if info == self.TARGET_TYPE_XDS:  #drag save is currently disabled (unnecessary?)
         if self.XDS_ATOM in drag_context.targets:
             typ, fmt, dest = drag_context.source_window.property_get(
                 self.XDS_ATOM, self.TEXT_ATOM)
             success = True
             code = self.XDS_SUCCESS if success else self.XDS_ERROR
             selection_data.set(selection_data.target, 8, code)
         self.drag_item = None
     if info == self.TARGET_TYPE_URI_LIST:
         size = None
         if settings.dragdrop_resize > 0:
             size = (settings.dragdrop_max_size, settings.dragdrop_max_size)
         if not self.drag_item.selected:
             path = imagemanip.get_jpeg_or_png_image_file(
                 self.drag_item, self.active_collection, size,
                 settings.dragdrop_strip_metadata,
                 settings.dragdrop_apply_edits)
             print 'path', path
             if path is None:
                 return
             uri = io.get_uri(
                 path
             )  #I don't know why, but nautilius expects uris enclosed in quotes
             selection_data.set_uris([uri])
         else:
             uris = []
             i = 0
             while i < len(self.active_view):
                 item = self.active_view(i)
                 if item.selected:
                     path = imagemanip.get_jpeg_or_png_image_file(
                         item, self.active_collection, size,
                         settings.dragdrop_strip_metadata,
                         settings.dragdrop_apply_edits)
                     if path is None:
                         continue
                     uri = io.get_uri(path)
                     uris.append(uri)
                 i += 1
             print 'uris', uris
             selection_data.set_uris(uris)
예제 #2
0
def update_thumb_date(item,
                      collection,
                      cache=None,
                      interrupt_fn=None,
                      remove_old=True):
    '''
    sets the internal date of the cached thumbnail image to that of the image file
    if the thumbnail name the thumbnail name will be updated
    if no thumbnail is present it will be created
    interrupt_fn - callback that returns False if job should be interrupted
    remove_old - if the item name has changed, removes the old thumbnail
    affects mtime, thumb, thumburi members of item
    '''
    itemfile = collection.get_path(item)
    item.mtime = io.get_mtime(itemfile)
    if item.thumburi:
        oldthumburi = item.thumburi
        if not item.thumb:
            load_thumb(item, collection)
        uri = io.get_uri(itemfile)
        if cache == None:
            thumb_factory.save_thumbnail(item.thumb, uri, int(item.mtime))
            item.thumburi = thumb_factory.lookup(uri, int(item.mtime))
        else:
            if not os.path.exists(cache):
                os.makedirs(cache)
            item.thumburi = os.path.join(
                cache, muuid(item.uid + str(int(item.mtime)))) + '.png'
            item.thumb.save(item.thumburi, "png")
        if remove_old and oldthumburi != item.thumburi:
            io.remove_file(oldthumburi)
        return True
    return make_thumb(item, collection, interrupt_fn, cache=cache)
예제 #3
0
파일: imagemanip.py 프로젝트: spillz/picty
def update_thumb_date(item,collection,cache=None,interrupt_fn=None,remove_old=True):
    '''
    sets the internal date of the cached thumbnail image to that of the image file
    if the thumbnail name the thumbnail name will be updated
    if no thumbnail is present it will be created
    interrupt_fn - callback that returns False if job should be interrupted
    remove_old - if the item name has changed, removes the old thumbnail
    affects mtime, thumb, thumburi members of item
    '''
    itemfile=collection.get_path(item)
    item.mtime=io.get_mtime(itemfile)
    if item.thumburi:
        oldthumburi=item.thumburi
        if not item.thumb:
            load_thumb(item,collection)
        uri = io.get_uri(itemfile)
        if cache==None:
            thumb_factory.save_thumbnail(item.thumb,uri,int(item.mtime))
            item.thumburi=thumb_factory.lookup(uri,int(item.mtime))
        else:
            if not os.path.exists(cache):
                os.makedirs(cache)
            item.thumburi=os.path.join(cache,muuid(item.uid+str(int(item.mtime))))+'.png'
            item.thumb.save(item.thumburi,"png")
        if remove_old and oldthumburi!=item.thumburi:
            io.remove_file(oldthumburi)
        return True
    return make_thumb(item,collection,interrupt_fn,cache=cache)
예제 #4
0
파일: viewer.py 프로젝트: wazari972/picty
 def drag_get_signal(self, widget, drag_context, selection_data, info,
                     timestamp):
     '''
     callback triggered to set the selection_data payload
     (viewer is the source of the drop)
     '''
     if self.item is None:
         return
     if info == self.TARGET_TYPE_IMAGE:
         selection_data.set('image-filename', 8, self.item.uid)
     if info == self.TARGET_TYPE_URI_LIST:
         #TODO: this doesn't work because protocal isn't local
         #            if drag_context.protocol == gtk.gdk.DRAG_PROTO_LOCAL:
         #                self.collection.get_path(self.item)
         #                uri=io.get_uri(path)
         #                selection_data.set_uris([uri])
         #                print 'Internal Drag Drop from viewer using uri',uri
         #                print 'Protocol',drag_context.protocol, gtk.gdk.DRAG_PROTO_LOCAL
         #                return
         size = None
         if settings.dragdrop_resize:
             size = (settings.dragdrop_max_size, settings.dragdrop_max_size)
         path = imagemanip.get_jpeg_or_png_image_file(
             self.item, self.collection, size,
             settings.dragdrop_strip_metadata,
             settings.dragdrop_apply_edits)
         if path is None:
             return
         uri = io.get_uri(path)
         selection_data.set_uris([uri])
         print 'Drag Drop from viewer using uri', uri
예제 #5
0
파일: viewer.py 프로젝트: eephyne/picty
    def drag_get_signal(self, widget, drag_context, selection_data, info, timestamp):
        '''
        callback triggered to set the selection_data payload
        (viewer is the source of the drop)
        '''
        if self.item is None:
            return
        if info == self.TARGET_TYPE_IMAGE:
            selection_data.set('image-filename', 8, self.item.uid)
        if info == self.TARGET_TYPE_URI_LIST:
            #TODO: this doesn't work because protocal isn't local
#            if drag_context.protocol == gtk.gdk.DRAG_PROTO_LOCAL:
#                self.collection.get_path(self.item)
#                uri=io.get_uri(path)
#                selection_data.set_uris([uri])
#                print 'Internal Drag Drop from viewer using uri',uri
#                print 'Protocol',drag_context.protocol, gtk.gdk.DRAG_PROTO_LOCAL
#                return
            size=None
            if settings.dragdrop_resize:
                size=(settings.dragdrop_max_size,settings.dragdrop_max_size)
            path = imagemanip.get_jpeg_or_png_image_file(self.item,self.collection,size,settings.dragdrop_strip_metadata,settings.dragdrop_apply_edits)
            if path is None:
                return
            uri=io.get_uri(path)
            selection_data.set_uris([uri])
            print 'Drag Drop from viewer using uri',uri
예제 #6
0
파일: imagemanip.py 프로젝트: spillz/picty
def load_thumb(item,collection,cache=None):
    '''
    load thumbnail from a cache location (optionally using the
    thumbnailing methods provided in gnome.ui if cache is None)
    affects thumbnail, thumburi members of item
    '''
    ##todo: rename load_thumb_from_cache
    ##note that loading thumbs embedded in image files is handled in load_thumb_from_preview_icon, load_embedded_thumb and load_metadata
    if item.thumb==False:
        return False
    image=None
    try:
        if item.thumburi:
            if settings.is_windows:
                image=Image.open(item.thumburi)
                image.thumbnail((128,128),pil_load_thumb_flags)
                image =  image_to_pixbuf(image)
            else:
                image=gtk.gdk.pixbuf_new_from_file(item.thumburi)
                s=(image.get_width(),image.get_height())
                if s[0]>128 or s[1]>128:
                    m=max(s)
                    w=s[0]*128/m
                    h=s[1]*128/m
                    image=image.scale_simple(w,h,gtk.gdk.INTERP_BILINEAR) #todo: doesn't this distort non-square images?
        else:
            if cache!=None:
                thumburi=os.path.join(cache,muuid(item.uid+str(int(item.mtime))))
                if os.path.exists(thumburi+'.png'):
                    thumburi = thumburi+'.png'
                    item.thumburi=thumburi
                elif os.path.exists(thumburi+'.jpg'):
                    thumburi = thumburi+'.jpg'
                    item.thumburi=thumburi
            elif collection.local_filesystem:
                itemfile=collection.get_path(item)
                uri = io.get_uri(itemfile)
                item.thumburi=thumb_factory.lookup(uri,int(item.mtime))
                if not item.thumburi:
                    thumburi=thumb_factory_large.lookup(uri,int(item.mtime))
            if thumburi:
                t=time.time()
                image = Image.open(thumburi)
                image.thumbnail((128,128),pil_load_thumb_flags)
                image=image_to_pixbuf(image) #todo: not sure this works (maybe because thumbnail doesn't finalize data?)
            elif item.thumburi:
                t=time.time()
                image=gtk.gdk.pixbuf_new_from_file(item.thumburi)
                image=image.scale_simple(128,128, gtk.gdk.INTERP_BILINEAR) #todo: doesn't this distort non-square images?
    except:
        image=None
    if image is not None:
        item.thumb=image
        cache_thumb_in_memory(item)
        return True
    else:
        return False
예제 #7
0
파일: browser.py 프로젝트: spillz/picty
 def drag_get_signal(self, widget, drag_context, selection_data, info, timestamp):
     '''
     callback triggered to set the selection_data payload
     (viewer is the source of the drop)
     '''
     if self.drag_item==None:
         return
     if info == self.TARGET_TYPE_IMAGE:
         selection_data.set('image-filename', 8, self.drag_item.uid)
     if info == self.TARGET_TYPE_XDS: #drag save is currently disabled (unnecessary?)
         if self.XDS_ATOM in drag_context.targets:
             typ, fmt, dest = drag_context.source_window.property_get(self.XDS_ATOM,self.TEXT_ATOM)
             success=True
             code = self.XDS_SUCCESS if success else self.XDS_ERROR
             selection_data.set(selection_data.target, 8, code)
         self.drag_item=None
     if info == self.TARGET_TYPE_URI_LIST:
         size=None
         if settings.dragdrop_resize>0:
             size=(settings.dragdrop_max_size,settings.dragdrop_max_size)
         if not self.drag_item.selected:
             path = imagemanip.get_jpeg_or_png_image_file(self.drag_item,self.active_collection,size,settings.dragdrop_strip_metadata,settings.dragdrop_apply_edits)
             print 'path',path
             if path is None:
                 return
             uri=io.get_uri(path) #I don't know why, but nautilius expects uris enclosed in quotes
             selection_data.set_uris([uri])
         else:
             uris=[]
             i=0
             while i<len(self.active_view):
                 item=self.active_view(i)
                 if item.selected:
                     path = imagemanip.get_jpeg_or_png_image_file(item,self.active_collection,size,settings.dragdrop_strip_metadata,settings.dragdrop_apply_edits)
                     if path is None:
                         continue
                     uri=io.get_uri(path)
                     uris.append(uri)
                 i+=1
             print 'uris',uris
             selection_data.set_uris(uris)
예제 #8
0
파일: imagemanip.py 프로젝트: spillz/picty
def has_thumb(item,collection,cache=None):
    '''
    returns true if the item has a thumbnail image in the cache
    '''
    fname=collection.get_path(item)
    if item.thumburi and os.path.exists(item.thumburi):
        return True
    if cache==None:
        uri = io.get_uri(fname)
        item.thumburi=thumb_factory.lookup(uri,int(item.mtime))
        if item.thumburi:
            return True
        if thumb_factory_large.lookup(uri,int(item.mtime)):
            return True
    else:
        thumburi=os.path.join(cache,muuid(item.uid+str(int(item.mtime))))+'.png'
        if os.path.exists(thumburi):
            item.thumburi = thumburi
            return True
    return False
예제 #9
0
def has_thumb(item, collection, cache=None):
    '''
    returns true if the item has a thumbnail image in the cache
    '''
    fname = collection.get_path(item)
    if item.thumburi and os.path.exists(item.thumburi):
        return True
    if cache == None:
        uri = io.get_uri(fname)
        item.thumburi = thumb_factory.lookup(uri, int(item.mtime))
        if item.thumburi:
            return True
        if thumb_factory_large.lookup(uri, int(item.mtime)):
            return True
    else:
        thumburi = os.path.join(
            cache, muuid(item.uid + str(int(item.mtime)))) + '.png'
        if os.path.exists(thumburi):
            item.thumburi = thumburi
            return True
    return False
예제 #10
0
def make_thumb(item,
               collection,
               interrupt_fn=None,
               force=False,
               cache=None,
               use_embedded=False,
               write_to_cache=True):
    '''
    create a thumbnail from the original image using either PIL or dcraw
    interrupt_fn = callback that returns False if routine should cancel (not implemented)
    force = True if thumbnail should be recreated even if already present
    affects thumb, thumburi members of item
    '''
    itemfile = collection.get_path(item)
    thumb_pb = None
    if cache == None and thumb_factory.has_valid_failed_thumbnail(
            itemfile, int(item.mtime)):
        if not force:
            item.thumb = False
            return False
        print 'Forcing thumbnail creation'
        uri = io.get_uri(itemfile)
        thumb_uri = thumb_factory.lookup(uri, int(item.mtime))
        if write_to_cache and thumb_uri:
            os.remove(thumb_uri)
    if not force and item.thumb == False:
        return False
    delete_thumb(item)
    ##todo: could also try extracting the thumb from the image (essential for raw files)
    ## would not need to make the thumb in that case
    print 'Creating thumbnail for', item.uid, itemfile
    t = time.time()
    try:
        uri = io.get_uri(itemfile)
        mimetype = io.get_mime_type(itemfile)
        thumb_pb = None
        if mimetype.lower().startswith('video'):
            cmd = settings.video_thumbnailer % (itemfile, )
            imdata = os.popen(cmd).read()
            image = Image.open(StringIO.StringIO(imdata))
            image.thumbnail(
                (128, 128),
                Image.ANTIALIAS)  ##TODO: this is INSANELY slow -- find out why
        else:
            try:
                mime = io.get_mime_type(itemfile)
                if use_embedded and load_embedded_thumb(item, collection):
                    thumb_pb = item.thumb
                    image = None
                    print 'Used embedded thumb'
                elif not settings.is_windows and mime in gdk_mime_types:  #todo: this is completely broken on windows
                    thumb_pb = gtk.gdk.pixbuf_new_from_file_at_size(
                        itemfile, 128, 128)
                    thumb_pb = orient_pixbuf(thumb_pb, item.meta)
                    image = None
                    print 'Opened with GDK'
                else:
                    image = Image.open(itemfile)
                    image.thumbnail((128, 128), Image.ANTIALIAS)
                    print 'Opened with PIL'
            except:
                cmd = settings.dcraw_cmd % (itemfile, )
                imdata = os.popen(cmd).read()
                if not imdata or len(imdata) < 100:
                    cmd = settings.dcraw_backup_cmd % (itemfile, )
                    imdata = os.popen(cmd).read()


#                pipe = subprocess.Popen(cmd, shell=True,
#                        stdout=PIPE) ##, close_fds=True
#                print pipe
#                pipe=pipe.stdout
#                print 'pipe opened'
#                imdata=pipe.read()
#                print 'pipe read'
                p = ImageFile.Parser()
                p.feed(imdata)
                image = p.close()
                image.thumbnail((128, 128), Image.ANTIALIAS)
                image = orient_image(image, item.meta)
                print 'Opened with DCRAW'
        if image is not None:
            thumb_pb = image_to_pixbuf(image)
        if thumb_pb is None:
            raise TypeError
    except:
        item.thumb = False
        item.thumburi = None
        if write_to_cache and cache == None:
            thumb_factory.create_failed_thumbnail(itemfile, int(item.mtime))
        print 'Error creating thumbnail for', item
        import sys
        import traceback
        tb_text = traceback.format_exc(sys.exc_info()[2])
        print tb_text
        return False
    width = thumb_pb.get_width()
    height = thumb_pb.get_height()
    uri = io.get_uri(itemfile)
    #save the new thumbnail
    try:
        if write_to_cache:
            if cache == None:
                thumb_factory.save_thumbnail(thumb_pb, uri, int(item.mtime))
                item.thumburi = thumb_factory.lookup(uri, int(item.mtime))
            else:
                if not os.path.exists(cache):
                    os.makedirs(cache)
                item.thumburi = os.path.join(
                    cache, muuid(item.uid + str(int(item.mtime)))) + '.png'
                thumb_pb.save(item.thumburi, "png")
            print 'cached at', item.thumburi
    except:
        print 'Error caching thumbnail for', item
        import sys
        import traceback
        tb_text = traceback.format_exc(sys.exc_info()[2])
        print tb_text
        item.thumb = False
        item.thumburi = None
        if write_to_cache and cache == None:
            thumb_factory.create_failed_thumbnail(itemfile, int(item.mtime))
        return False
    item.thumb = thumb_pb
    cache_thumb_in_memory(item)
    return True
예제 #11
0
def load_thumb(item, collection, cache=None):
    '''
    load thumbnail from a cache location (optionally using the
    thumbnailing methods provided in gnome.ui if cache is None)
    affects thumbnail, thumburi members of item
    '''
    ##todo: rename load_thumb_from_cache
    ##note that loading thumbs embedded in image files is handled in load_thumb_from_preview_icon, load_embedded_thumb and load_metadata
    if item.thumb == False:
        return False
    image = None
    try:
        if item.thumburi:
            if settings.is_windows:
                image = Image.open(item.thumburi)
                image.thumbnail((128, 128), pil_load_thumb_flags)
                image = image_to_pixbuf(image)
            else:
                image = gtk.gdk.pixbuf_new_from_file(item.thumburi)
                s = (image.get_width(), image.get_height())
                if s[0] > 128 or s[1] > 128:
                    m = max(s)
                    w = s[0] * 128 / m
                    h = s[1] * 128 / m
                    image = image.scale_simple(
                        w, h, gtk.gdk.INTERP_BILINEAR
                    )  #todo: doesn't this distort non-square images?
        else:
            if cache != None:
                thumburi = os.path.join(cache,
                                        muuid(item.uid + str(int(item.mtime))))
                if os.path.exists(thumburi + '.png'):
                    thumburi = thumburi + '.png'
                    item.thumburi = thumburi
                elif os.path.exists(thumburi + '.jpg'):
                    thumburi = thumburi + '.jpg'
                    item.thumburi = thumburi
            elif collection.local_filesystem:
                itemfile = collection.get_path(item)
                uri = io.get_uri(itemfile)
                item.thumburi = thumb_factory.lookup(uri, int(item.mtime))
                if not item.thumburi:
                    thumburi = thumb_factory_large.lookup(uri, int(item.mtime))
            if thumburi:
                t = time.time()
                image = Image.open(thumburi)
                image.thumbnail((128, 128), pil_load_thumb_flags)
                image = image_to_pixbuf(
                    image
                )  #todo: not sure this works (maybe because thumbnail doesn't finalize data?)
            elif item.thumburi:
                t = time.time()
                image = gtk.gdk.pixbuf_new_from_file(item.thumburi)
                image = image.scale_simple(
                    128, 128, gtk.gdk.INTERP_BILINEAR
                )  #todo: doesn't this distort non-square images?
    except:
        image = None
    if image is not None:
        item.thumb = image
        cache_thumb_in_memory(item)
        return True
    else:
        return False
예제 #12
0
파일: imagemanip.py 프로젝트: spillz/picty
def make_thumb(item,collection,interrupt_fn=None,force=False,cache=None,use_embedded=False,write_to_cache=True):
    '''
    create a thumbnail from the original image using either PIL or dcraw
    interrupt_fn = callback that returns False if routine should cancel (not implemented)
    force = True if thumbnail should be recreated even if already present
    affects thumb, thumburi members of item
    '''
    itemfile=collection.get_path(item)
    thumb_pb=None
    if cache==None and thumb_factory.has_valid_failed_thumbnail(itemfile,int(item.mtime)):
        if not force:
            item.thumb=False
            return False
        print 'Forcing thumbnail creation'
        uri = io.get_uri(itemfile)
        thumb_uri=thumb_factory.lookup(uri,int(item.mtime))
        if write_to_cache and thumb_uri:
            os.remove(thumb_uri)
    if not force and item.thumb==False:
        return False
    delete_thumb(item)
    ##todo: could also try extracting the thumb from the image (essential for raw files)
    ## would not need to make the thumb in that case
    print 'Creating thumbnail for',item.uid,itemfile
    t=time.time()
    try:
        uri = io.get_uri(itemfile)
        mimetype=io.get_mime_type(itemfile)
        thumb_pb=None
        if mimetype.lower().startswith('video'):
            cmd=settings.video_thumbnailer%(itemfile,)
            imdata=os.popen(cmd).read()
            image=Image.open(StringIO.StringIO(imdata))
            image.thumbnail((128,128),Image.ANTIALIAS) ##TODO: this is INSANELY slow -- find out why
        else:
            try:
                mime=io.get_mime_type(itemfile)
                if use_embedded and load_embedded_thumb(item,collection):
                    thumb_pb = item.thumb
                    image = None
                    print 'Used embedded thumb'
                elif not settings.is_windows and mime in gdk_mime_types: #todo: this is completely broken on windows
                    thumb_pb = gtk.gdk.pixbuf_new_from_file_at_size(itemfile,128,128)
                    thumb_pb = orient_pixbuf(thumb_pb,item.meta)
                    image = None
                    print 'Opened with GDK'
                else:
                    image=Image.open(itemfile)
                    image.thumbnail((128,128),Image.ANTIALIAS)
                    print 'Opened with PIL'
            except:
                cmd=settings.dcraw_cmd%(itemfile,)
                imdata=os.popen(cmd).read()
                if not imdata or len(imdata)<100:
                    cmd=settings.dcraw_backup_cmd%(itemfile,)
                    imdata=os.popen(cmd).read()
#                pipe = subprocess.Popen(cmd, shell=True,
#                        stdout=PIPE) ##, close_fds=True
#                print pipe
#                pipe=pipe.stdout
#                print 'pipe opened'
#                imdata=pipe.read()
#                print 'pipe read'
                p = ImageFile.Parser()
                p.feed(imdata)
                image = p.close()
                image.thumbnail((128,128),Image.ANTIALIAS)
                image=orient_image(image,item.meta)
                print 'Opened with DCRAW'
        if image is not None:
            thumb_pb=image_to_pixbuf(image)
        if thumb_pb is None:
            raise TypeError
    except:
        item.thumb=False
        item.thumburi=None
        if write_to_cache and cache==None:
            thumb_factory.create_failed_thumbnail(itemfile,int(item.mtime))
        print 'Error creating thumbnail for',item
        import sys
        import traceback
        tb_text=traceback.format_exc(sys.exc_info()[2])
        print tb_text
        return False
    width=thumb_pb.get_width()
    height=thumb_pb.get_height()
    uri = io.get_uri(itemfile)
    #save the new thumbnail
    try:
        if write_to_cache:
            if cache==None:
                thumb_factory.save_thumbnail(thumb_pb,uri,int(item.mtime))
                item.thumburi=thumb_factory.lookup(uri,int(item.mtime))
            else:
                if not os.path.exists(cache):
                    os.makedirs(cache)
                item.thumburi=os.path.join(cache,muuid(item.uid+str(int(item.mtime))))+'.png'
                thumb_pb.save(item.thumburi,"png")
            print 'cached at',item.thumburi
    except:
        print 'Error caching thumbnail for',item
        import sys
        import traceback
        tb_text=traceback.format_exc(sys.exc_info()[2])
        print tb_text
        item.thumb=False
        item.thumburi=None
        if write_to_cache and cache==None:
            thumb_factory.create_failed_thumbnail(itemfile,int(item.mtime))
        return False
    item.thumb=thumb_pb
    cache_thumb_in_memory(item)
    return True