如何获取特定文件的图标或缩略图 [英] How Can I Get an Icon or thumbnail for a Specific file

查看:385
本文介绍了如何获取特定文件的图标或缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来获取与Linux上特定文件类型相关联的图标.

I am searching for a way to get the icon associated with a certain file type on Linux.

使用Shell脚本或python.

Either using a shell script or python.

我更喜欢在所有平台上都可以使用的本机python方法,但是可以使用shell脚本方法.

I prefer a native python method which works on all the platforms, but a shell script method would be ok.

推荐答案

我找到了解决方案,并编写了一个函数来完成这项工作

I found a solution, and I wrote a function to do the job

#!/usr/bin/env python
import gio, gtk, os

    def get_icon_filename(filename,size):
        #final_filename = "default_icon.png"
        final_filename = ""
        if os.path.isfile(filename):
            # Get the icon name
            file = gio.File(filename)
            file_info = file.query_info('standard::icon')
            file_icon = file_info.get_icon().get_names()[0]
            # Get the icon file path
            icon_theme = gtk.icon_theme_get_default()
            icon_filename = icon_theme.lookup_icon(file_icon, size, 0)
            if icon_filename != None:
                final_filename = icon_filename.get_filename()

        return final_filename


    print get_icon_filename("/home/el7r/Music/test.mp3",64)

谢谢所有

这篇关于如何获取特定文件的图标或缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆