我正在寻找一种方法来获取与Linux上的某种文件types相关联的图标。
我更喜欢一种适用于所有平台的本地python方法,但shell脚本方法可以。
我找到了一个解决方案,我写了一个功能来完成这项工作
#!/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)
感谢所有
我不认为这个图标文件在平台上是一样的
在Mac上,图标存储在应用程序包中 – EG:
/Applications/Mail.app/Contents/Resources/app.icns
在Linux中,他们似乎处于一个类似但不同的地方EG:
/usr/lib/firefox/icons/mozicon16.xpm
所以,我认为你是一个简单的,跨平台的解决方案,运气不好,将不得不编写例程来查看每个操作系统的适当位置
感谢@Ali_AlNoaimi的解决方案。 我改变了它与python3和PyGI的工作:
import os , gi gi.require_version('Gtk', '3.0') from gi.repository import Gio , Gtk def get_thumbnail(filename,size): final_filename = "" if os.path.exists(filename): file = Gio.File.new_for_path(filename) info = file.query_info('standard::icon' , 0 , Gio.Cancellable()) icon = info.get_icon().get_names()[0] icon_theme = Gtk.IconTheme.get_default() icon_file = icon_theme.lookup_icon(icon , size , 0) if icon_file != None: final_filename = icon_file.get_filename() return final_filename print(get_thumbnail("/path/to/file",32))
ImageMagick非常适合从命令行进行基本操作。 信息在http://www.imagemagick.org/script/index.php