How can I get the thumbnail that nautilus uses for a given file?

别说谁变了你拦得住时间么 提交于 2020-01-01 09:33:17

问题


Nautilus shows me a thumbnail of a file, if its an image it will show me a preview, if its a video it will show a frame from the video, if its a document it will show me the application icon.

How can I access the image?

I see they are cached in ~/.thumbnail/ however they are all given unique names.


回答1:


the thumbnail filename is an md5 of the filename. However the filename is the absolute URI to the image (without a newline).

So you need to do:

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

And if it has spaces, you need to convert them to '%20', ex for "foo bar.jpg"

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

Found at Ubuntu forums. See also the Thumbnail Managing Standard document, linked from the freedesktop.org wiki.




回答2:


Simple Python tool to calculate the thumbnail path. Written by Raja, shared as an ActiveState code recipe. Note, however, that this code does not escape filenames with spaces or special characters; which means this code does not work for all filenames.

"""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')



回答3:


I guess that you need to access the thumbnail programatically. You want to use the Gio library.

I haven't been able to find a way to check for the thumbnail and, if it doesn't exist, go for the application icon, so you need to do it in two steps. Here you have a sample (sorry, Python. I'm not fluent in C):

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()


来源:https://stackoverflow.com/questions/14112602/how-can-i-get-the-thumbnail-that-nautilus-uses-for-a-given-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!