2013-01-01 30 views
7

鸚鵡螺向我展示了一個文件的縮略圖,如果它的圖像會顯示一個預覽,如果它的視頻顯示了一個來自視頻的幀,如果它是一個文檔,它會向我顯示應用程序圖標。如何獲得鸚鵡螺對給定文件使用的縮略圖?

如何訪問圖像?

我看到它們被緩存在~/.thumbnail/中,但它們都被賦予了獨特的名稱。

回答

8

縮略圖文件名是文件名的md5。然而,文件名 是圖像的絕對URI(沒有換行符)。

所以,你需要做的:

echo -n 'file:///home/yuzem/pics/foo.jpg' | md5sum

而且如果有空格,則需要將它們轉換爲 '%20',前爲 「富bar.jpg」

echo -n 'file:///home/yuzem/pics/foo%20bar.jpg' | md5sum

發現在Ubuntu forums。另見Thumbnail Managing Standard文件,鏈接從freedesktop.org wiki

+0

當縮略圖不存在時,什麼是強制生成的機制? –

0

我想你需要以編程方式訪問縮略圖。您想使用Gio library

我一直無法找到檢查縮略圖的方法,如果它不存在,請轉到應用程序圖標,因此您需要分兩步執行。這裏有一個樣本(對不起,巨蟒我不流利的溫度。):

import gio 
import gtk 

window = gtk.Window(gtk.WINDOW_TOPLEVEL) 
window.show() 
hbox = gtk.HBox() 
hbox.show() 
window.add(hbox) 

f = gio.File(path='/home/whatever/you/want.jpg') 
info = f.query_info('*') 

# We check if there's a thumbnail for our file 
preview = info.get_attribute_byte_string ("thumbnail::path") 

image = None 
if preview: 
    image = gtk.image_new_from_file (preview) 
else: 
    # If there's no thumbnail, we check get_icon, who checks the 
    # file's mimetype, and returns the correct stock icon. 
    icon = info.get_icon() 
    image = gtk.image_new_from_gicon (icon, gtk.ICON_SIZE_MENU) 

hbox.add (image) 

window.show_all() 
gtk.main() 
2

簡單的Python工具來計算的縮略圖路徑。作者爲Raja,分享爲ActiveState code recipe。但是,請注意,此代碼不會使用空格或特殊字符轉義文件名;這意味着此代碼不適用於所有文件名。

"""Get the thumbnail stored on the system. 
Should work on any linux system following the desktop standards""" 

import hashlib 
import os 

def get_thumbnailfile(filename): 
    """Given the filename for an image, return the path to the thumbnail file. 
    Returns None if there is no thumbnail file. 
    """ 
    # Generate the md5 hash of the file uri 
    file_hash = hashlib.md5('file://'+filename).hexdigest() 

    # the thumbnail file is stored in the ~/.thumbnails/normal folder 
    # it is a png file and name is the md5 hash calculated earlier 
    tb_filename = os.path.join(os.path.expanduser('~/.thumbnails/normal'), 
           file_hash) + '.png' 

    if os.path.exists(tb_filename): 
     return tb_filename 
    else: 
     return None 

if __name__ == '__main__': 
    import sys 
    if len(sys.argv) < 2: 
     print('Usage: get_thumbnail.py filename') 
     sys.exit(0) 

    filename = sys.argv[1] 
    tb_filename = get_thumbnailfile(filename) 

    if tb_filename: 
     print('Thumbnail for file %s is located at %s' %(filename, tb_filename)) 
    else: 
     print('No thumbnail found') 
相關問題