Python 如何获取特定文件的图标或缩略图
我正在寻找一种方法,在Linux上获取与特定文件类型关联的图标 使用shell脚本或pythonPython 如何获取特定文件的图标或缩略图,python,linux,shell,Python,Linux,Shell,我正在寻找一种方法,在Linux上获取与特定文件类型关联的图标 使用shell脚本或python 我更喜欢在所有平台上都能工作的本机python方法,但是shell脚本方法就可以了。ImageMagick非常适合从命令行进行基本操作。ImageMagick上的信息非常适合从命令行进行基本操作。信息在我不认为图标文件在不同平台上是相同的 在Mac上,图标存储在应用程序包中-例如: /Applications/Mail.app/Contents/Resources/app.icns 在Linux中
我更喜欢在所有平台上都能工作的本机python方法,但是shell脚本方法就可以了。ImageMagick非常适合从命令行进行基本操作。ImageMagick上的信息非常适合从命令行进行基本操作。信息在我不认为图标文件在不同平台上是相同的 在Mac上,图标存储在应用程序包中-例如:
/Applications/Mail.app/Contents/Resources/app.icns
在Linux中,它们似乎处于相似但不同的位置,例如:
/usr/lib/firefox/icons/mozicon16.xpm
因此,我认为,对于一个简单、跨平台的解决方案来说,您运气不好,必须编写例程来查找每个操作系统的适当位置。我不认为图标文件在不同平台上是相同的 在Mac上,图标存储在应用程序包中-例如:
/Applications/Mail.app/Contents/Resources/app.icns
在Linux中,它们似乎处于相似但不同的位置,例如:
/usr/lib/firefox/icons/mozicon16.xpm
因此,我认为,对于一个简单、跨平台的解决方案来说,您运气不好,必须编写例程来查找每个操作系统的适当位置。我找到了一个解决方案,并编写了一个函数来完成这项工作
#!/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))
谢谢大家我找到了一个解决方案,并编写了一个函数来完成这项工作
#!/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))
谢谢大家谢谢你的解决方案。我将其更改为与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))
多亏了他的解决方案。我将其更改为与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))
似乎这个软件包只为图像制作缩略图,并编辑图像,它能为mpg、ogg、mp3、zip、tar.gz生成缩略图或图标吗?似乎这个软件包只为图像制作缩略图,并编辑图像,它能为mpg、ogg、mp3、zip、tar.gz生成缩略图或图标吗?