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

后端 未结 4 1871
北恋
北恋 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:40

    I don't think that icon files are the same across platforms...

    On a Mac, the icons are stored in the application bundle - EG:

    /Applications/Mail.app/Contents/Resources/app.icns
    

    In Linux they seem to be in a similar, but different place EG:

     /usr/lib/firefox/icons/mozicon16.xpm
    

    So, I think you are out of luck for an easy, cross-platform solution and will have to code the routine to look into the appropriate place for each OS

    0 讨论(0)
  • 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))
    
    0 讨论(0)
  • 2021-01-25 06:44

    ImageMagick is pretty good for basic manipulation from the command line. Info at http://www.imagemagick.org/script/index.php

    0 讨论(0)
  • 2021-01-25 06:49

    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))
    

    thanks all

    0 讨论(0)
提交回复
热议问题