~pyshareteam/pyshare/0.5b

« back to all changes in this revision

Viewing changes to helpers/gtkHelper.py

  • Committer: Sebastian Kacprzak
  • Date: 2010-03-13 09:41:49 UTC
  • Revision ID: naicik@gmail.com-20100313094149-a5gkv5hsyojevqkj
fix thumbnail creating for unsuported image types like xfc,scale gnome icons, fix saving thumbnail when there is no extension

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
    else:
55
55
        return False
56
56
 
 
57
def __getPixbufFromGnomeIcons(mimeType,size):
 
58
    #TODO: swtich to using gio when hardy get depraceted
 
59
    iconsPath = "/usr/share/icons/gnome/32x32/mimetypes/"
 
60
    defaultIcon = iconsPath + "empty.png"
 
61
 
 
62
    iconName = "gnome-mime-"+mimeType.split('/')[0].lower()+"-"+mimeType.split('/')[-1].lower()+".png"
 
63
    scalableIconPath = "/usr/share/icons/gnome/scalable/mimetypes/" + iconName
 
64
    if os.path.exists(scalableIconPath):
 
65
        pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(scalableIconPath,size,size)
 
66
    elif os.path.exists(iconsPath+iconName):
 
67
        pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(iconsPath+iconName,size,size)
 
68
    else:
 
69
        pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(defaultIcon,size,size)
 
70
    return pixbuf
 
71
 
57
72
def createThumbnail(file):
58
73
    """takes file url as parameter, returns thumbnail without changing image ratio"""
59
 
    image = gtk.Image()
60
 
    #if sendToImageshack.correctExtension(file): #TODO: more generic check, and or thumbnail creation needed
61
 
    mime = getMimeTypes(file)
62
 
    if not isImage(mime):
63
 
        iconsPath = "/usr/share/icons/gnome/32x32/mimetypes/"
64
 
        defaultIcon = iconsPath + "empty.png"
65
 
 
66
 
        iconName = "gnome-mime-"+mime.split('/')[0].lower()+"-"+mime.split('/')[-1].lower()+".png"
67
 
        if os.path.exists(iconsPath+iconName):
68
 
            pixbuf = gtk.gdk.pixbuf_new_from_file(iconsPath+iconName)
 
74
    #image = gtk.Image()
 
75
    #get pixbuf from file or from system icon
 
76
    thumbnailSize = set.getThumbnailSize()
 
77
    try:
 
78
        originalPixbuf = gtk.gdk.pixbuf_new_from_file(file)
 
79
        #scale pixbuf
 
80
        ratio = float(originalPixbuf.get_width()) / originalPixbuf.get_height()
 
81
        thumbnailSizeY = thumbnailSizeX = thumbnailSize
 
82
        if ratio > 1:
 
83
            thumbnailSizeY /= ratio
69
84
        else:
70
 
            pixbuf = gtk.gdk.pixbuf_new_from_file(defaultIcon)
71
 
        #image.set_from_pixbuf(pixbuf)
72
 
    else:
73
 
        try:
74
 
            originalPixbuf = gtk.gdk.pixbuf_new_from_file(file)
75
 
            ratio = float(originalPixbuf.get_width()) / originalPixbuf.get_height()
76
 
            thumbnailSizeY = thumbnailSizeX = set.getThumbnailSize()
77
 
            if ratio > 1:
78
 
                thumbnailSizeY /= ratio
79
 
            else:
80
 
                thumbnailSizeX *= ratio
81
 
            pixbuf = originalPixbuf.scale_simple(int(thumbnailSizeX), int(thumbnailSizeY), gtk.gdk.INTERP_BILINEAR)
82
 
            #image.set_from_pixbuf(pixbufZoomed)
83
 
        #except GError:
84
 
            #print "create thumbnail from mime type"
85
 
        except:
86
 
            from sys import exc_info
87
 
            excInfo = exc_info()
88
 
            print excInfo
 
85
            thumbnailSizeX *= ratio
 
86
        pixbuf = originalPixbuf.scale_simple(int(thumbnailSizeX), int(thumbnailSizeY), gtk.gdk.INTERP_BILINEAR)
 
87
        #image.set_from_pixbuf(pixbufZoomed)
 
88
    #except GError:
 
89
        #print "create thumbnail from mime type"
 
90
    except: #GError cannot be imported in some system, fe: Ubuntu Hardy Haron
 
91
        pixbuf = __getPixbufFromGnomeIcons(getMimeTypes(file),thumbnailSize)
 
92
    
89
93
    return pixbuf
90
94
 
91
95
def saveThumbnail(pixbuf, file, link):
92
96
    cachePath = getPyShareHomeDirectory() + 'cache/'
93
97
    filename = cachePath + md5(link).hexdigest()
94
 
    ext = file.split('.')[-1].lower()
95
 
    if(ext=="jpg" or ext=="gif"):
 
98
    splittedFileName = file.split('.')
 
99
    ext = None
 
100
    if len(splittedFileName) > 1:
 
101
        ext = splittedFileName[-1].lower()
 
102
    if(ext=="jpg" or ext=="gif" or (not ext)):
96
103
        pixbuf.save(filename, "jpeg")
97
104
    else:
98
105
        pixbuf.save(filename, ext)