How Can I Get an Icon or thumbnail for a Specific file

后端 未结 4 1878
北恋
北恋 2021-01-25 06:01

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

Either using a shell script or python.

I prefer a native python method whi

4条回答
  •  暖寄归人
    2021-01-25 06:42

    Thanks to @Ali_AlNoaimi for his solution . I changed it to work with python3 and 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))
    

提交回复
热议问题