【问题标题】:How can I get the thumbnail that nautilus uses for a given file?如何获取 nautilus 用于给定文件的缩略图?
【发布时间】:2012-12-16 06:17:58
【问题描述】:

Nautilus 向我显示文件的缩略图,如果是图像,它将向我显示预览,如果是视频,它将显示视频中的帧,如果是文档,它将向我显示应用程序图标。

如何访问图像?

我看到它们被缓存在 ~/.thumbnail/ 但是它们都被赋予了唯一的名称。

【问题讨论】:

    标签: c linux thumbnails gnome


    【解决方案1】:

    缩略图文件名是文件名的 md5。但是文件名 是图像的绝对 URI(没有换行符)。

    所以你需要这样做:

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

    如果有空格,则需要将它们转换为 '%20',例如 "foo bar.jpg"

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

    发现于Ubuntu forums。另见Thumbnail Managing Standard 文档,链接自freedesktop.org wiki

    【讨论】:

    • 缩略图不存在时,用什么机制强制生成?
    • 不仅需要编码空格! [] 需要变为 %5B%5D。见标准RFC3986。您可能想为此使用库/API/框架。搜索“uri 编码”。 (有 this built-in 用于 JavaScript)。
    【解决方案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')
    

    【讨论】:

      【解决方案3】:

      我猜您需要以编程方式访问缩略图。你想使用Gio library

      我无法找到检查缩略图的方法,如果不存在,请查看应用程序图标,因此您需要分两步完成。这里有一个示例(抱歉,Python。我不会流利使用 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()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-03
        • 2015-01-22
        • 2013-10-31
        • 1970-01-01
        • 2020-10-27
        • 2023-03-09
        相关资源
        最近更新 更多