Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sudo apt-get install libgsf-bin python-imaging
- [Thumbnailer Entry]
- TryExec=/usr/bin/gsf-office-thumbnailer
- Exec=/usr/bin/gsf-office-thumbnailer -i %i -o %o -s %s
- MimeType=application/vnd.oasis.opendocument.graphics;application/vnd.oasis.opendocument.graphics-template;application/vnd.oasis.opendocument.formula;application/vnd.oasis.opendocument.text-master;application/vnd.oasis.opendocument.presentation;application/vnd.oasis.opendocument.presentation-template;application/vnd.oasis.opendocument.spreadsheet;application/vnd.oasis.opendocument.spreadsheet-template;application/vnd.oasis.opendocument.text;application/vnd.oasis.opendocument.text-template;application/vnd.openxmlformats-officedocument.presentationml.presentation;application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;application/vnd.openxmlformats-officedocument.wordprocessingml.document
- #!/usr/bin/env python
- import gnomevfs
- import gtk
- import os
- import sys
- import zipfile
- from PIL import Image, ImageEnhance
- # Alter these varibles to change thumbnail look
- ICON_USE_CURRENT_THEME = False
- # If not using current theme then use this path
- ICON_PATH_BASE = "/usr/share/icons/hicolor/48x48/apps/" # Change this path to alter icons
- ICON_PREFIX = "ooo-" #Ubuntu icons
- #ICON_PREFIX="openofficeorg-20-" #OOo2 stock icons
- #ICON_PREFIX="openofficeorg3-" # OOo3 icons
- ICON_OPACITY = 0.8 #Opacity of the icon (between 0.0 and 1.0)
- THUMBNAIL_BACKGROUND_COLOR = "white" # Color of the background
- # Prefix for using mime types
- ICON_MIME_PREFIX = "gnome-mime-application-"
- in_file_path = gnomevfs.get_local_path_from_uri(sys.argv[1])
- out_file_path = sys.argv[2]
- path_without_thumbs = os.getenv("HOME")+"/Templates"
- def get_icon(thumbnail_size):
- icon_names={"formula":"math","graphics":"draw","presentation":"impress","spreadsheet":"calc","text":"writer"}
- #Get file mimetype
- file_mime_type=gnomevfs.get_mime_type(in_file_path)
- #print file_mime_type
- #Get last part of mimetype name.
- application_name=file_mime_type.split(".")[-1]
- try:
- #For OOo2 files we have to find icon name in icon_names
- icon_name=icon_names[application_name]
- except:
- #But for OOo1 files it is equal to icon name (without prefix).
- icon_name=application_name
- #Load icon
- if ICON_USE_CURRENT_THEME:
- icon_name = ICON_MIME_PREFIX+ file_mime_type.split("/")[1]
- icon = get_current_theme_icon( icon_name )
- else:
- icon = Image.open(ICON_PATH_BASE+ICON_PREFIX+icon_name +".png").convert("RGBA")
- #Set it's opacity
- icon = set_icon_opacity(icon,ICON_OPACITY)
- #And set it's position in thumbnail
- icon_posx=thumbnail_size[0]-icon.size[0]
- icon_posy=thumbnail_size[1]-icon.size[1]
- icon_width=thumbnail_size[0]
- icon_height=thumbnail_size[1]
- return {"image":icon,"position":(icon_posx,icon_posy,icon_width,icon_height)}
- def get_basic_thumbnail():
- #Find out if the file is not in Templates directory
- if in_file_path.find(path_without_thumbs)!=0:
- try:
- #Extract thumbnail from OOo file and save it
- zip=zipfile.ZipFile(in_file_path,mode="r")
- picture=zip.read("Thumbnails/thumbnail.png")
- zip.close()
- thumbnail=open(out_file_path,"w")
- thumbnail.write(picture)
- thumbnail.write("/n")
- thumbnail.close()
- #Open saved thumbnail
- image=Image.open(out_file_path).convert("RGBA")
- return {"suceeded":True,"image":image,"size":(image.size[0],image.size[1])}
- except:
- return {"suceeded":False}
- else:
- return {"suceeded":False}
- # Nicked from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/362879
- def set_icon_opacity(icon,opacity):
- #Returns an image with reduced opacity.
- assert opacity >= 0 and opacity <= 1
- if icon.mode != 'RGBA':
- icon = icon.convert('RGBA')
- else:
- icon = icon.copy()
- alpha = icon.split()[3]
- alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
- icon.putalpha(alpha)
- return icon
- # Convert GTK pixbuf to PIL Image
- def convert_pixbuf_to_image(pb):
- assert(pb.get_colorspace() == gtk.gdk.COLORSPACE_RGB)
- dimensions = pb.get_width(), pb.get_height()
- stride = pb.get_rowstride()
- pixels = pb.get_pixels()
- mode = pb.get_has_alpha() and "RGBA" or "RGB"
- return Image.frombuffer(mode, dimensions, pixels,"raw", mode, stride, 1)
- # Get the icon from the current GTK theme
- def get_current_theme_icon( icon ):
- theme = gtk.icon_theme_get_default()
- #print theme.list_icons()
- pixbuf_icon = theme.load_icon(icon, 46, 0)
- return convert_pixbuf_to_image(pixbuf_icon)
- thumbnail=get_basic_thumbnail()
- if thumbnail["suceeded"]:
- background=Image.new("RGB", thumbnail["size"], THUMBNAIL_BACKGROUND_COLOR)
- icon=get_icon(thumbnail["size"])
- thumbnail=thumbnail["image"]
- # Add thumbnail
- background.paste(thumbnail, None, thumbnail)
- # Add icon
- background.paste(icon["image"],icon["position"],icon["image"])
- # Save thumbnail
- background.save(out_file_path,"PNG")
- <gconfschemafile>
- <schemalist>
- <schema>
- <key>/schemas/desktop/gnome/thumbnailers/application@vnd.oasis.opendocument.text/enable</key>
- <applyto>/desktop/gnome/thumbnailers/application@vnd.oasis.opendocument.text/enable</applyto>
- <owner>opendocument-thumb</owner>
- <type>bool</type>
- <default>true</default>
- <locale name="C">
- <short></short>
- <long></long>
- </locale>
- </schema>
- <schema>
- <key>/schemas/desktop/gnome/thumbnailers/application@vnd.oasis.opendocument.text/command</key>
- <applyto>/desktop/gnome/thumbnailers/application@vnd.oasis.opendocument.text/command</applyto>
- <owner>opendocument-thumb</owner>
- <type>string</type>
- <default>/usr/bin/opendocument-thumbnailer %u %o</default>
- <locale name="C">
- <short></short>
- <long></long>
- </locale>
- </schema>
- <schema>
- <key>/schemas/desktop/gnome/thumbnailers/application@vnd.oasis.opendocument.spreadsheet/enable</key>
- <applyto>/desktop/gnome/thumbnailers/application@vnd.oasis.opendocument.spreadsheet/enable</applyto>
- <owner>opendocument-thumb</owner>
- <type>bool</type>
- <default>true</default>
- <locale name="C">
- <short></short>
- <long></long>
- </locale>
- </schema>
- <schema>
- <key>/schemas/desktop/gnome/thumbnailers/application@vnd.oasis.opendocument.spreadsheet/command</key>
- <applyto>/desktop/gnome/thumbnailers/application@vnd.oasis.opendocument.spreadsheet/command</applyto>
- <owner>opendocument-thumb</owner>
- <type>string</type>
- <default>/usr/bin/opendocument-thumbnailer %u %o</default>
- <locale name="C">
- <short></short>
- <long></long>
- </locale>
- </schema>
- <schema>
- <key>/schemas/desktop/gnome/thumbnailers/application@vnd.oasis.opendocument.graphics/enable</key>
- <applyto>/desktop/gnome/thumbnailers/application@vnd.oasis.opendocument.graphics/enable</applyto>
- <owner>opendocument-thumb</owner>
- <type>bool</type>
- <default>true</default>
- <locale name="C">
- <short></short>
- <long></long>
- </locale>
- </schema>
- <schema>
- <key>/schemas/desktop/gnome/thumbnailers/application@vnd.oasis.opendocument.graphics/command</key>
- <applyto>/desktop/gnome/thumbnailers/application@vnd.oasis.opendocument.graphics/command</applyto>
- <owner>opendocument-thumb</owner>
- <type>string</type>
- <default>/usr/bin/opendocument-thumbnailer %u %o</default>
- <locale name="C">
- <short></short>
- <long></long>
- </locale>
- </schema>
- <schema>
- <key>/schemas/desktop/gnome/thumbnailers/application@vnd.oasis.opendocument.formula/enable</key>
- <applyto>/desktop/gnome/thumbnailers/application@vnd.oasis.opendocument.formula/enable</applyto>
- <owner>opendocument-thumb</owner>
- <type>bool</type>
- <default>true</default>
- <locale name="C">
- <short></short>
- <long></long>
- </locale>
- </schema>
- <schema>
- <key>/schemas/desktop/gnome/thumbnailers/application@vnd.oasis.opendocument.formula/command</key>
- <applyto>/desktop/gnome/thumbnailers/application@vnd.oasis.opendocument.formula/command</applyto>
- <owner>opendocument-thumb</owner>
- <type>string</type>
- <default>/usr/bin/opendocument-thumbnailer %u %o</default>
- <locale name="C">
- <short></short>
- <long></long>
- </locale>
- </schema>
- <schema>
- <key>/schemas/desktop/gnome/thumbnailers/application@vnd.oasis.opendocument.presentation/enable</key>
- <applyto>/desktop/gnome/thumbnailers/application@vnd.oasis.opendocument.presentation/enable</applyto>
- <owner>opendocument-thumb</owner>
- <type>bool</type>
- <default>true</default>
- <locale name="C">
- <short></short>
- <long></long>
- </locale>
- </schema>
- <schema>
- <key>/schemas/desktop/gnome/thumbnailers/application@vnd.oasis.opendocument.presentation/command</key>
- <applyto>/desktop/gnome/thumbnailers/application@vnd.oasis.opendocument.presentation/command</applyto>
- <owner>opendocument-thumb</owner>
- <type>string</type>
- <default>/usr/bin/opendocument-thumbnailer %u %o</default>
- <locale name="C">
- <short></short>
- <long></long>
- </locale>
- </schema>
- <schema>
- <key>/schemas/desktop/gnome/thumbnailers/application@vnd.sun.xml.writer/enable</key>
- <applyto>/desktop/gnome/thumbnailers/application@vnd.sun.xml.writer/enable</applyto>
- <owner>opendocument-thumb</owner>
- <type>bool</type>
- <default>true</default>
- <locale name="C">
- <short></short>
- <long></long>
- </locale>
- </schema>
- <schema>
- <key>/schemas/desktop/gnome/thumbnailers/application@vnd.sun.xml.writer/command</key>
- <applyto>/desktop/gnome/thumbnailers/application@vnd.sun.xml.writer/command</applyto>
- <owner>opendocument-thumb</owner>
- <type>string</type>
- <default>/usr/bin/opendocument-thumbnailer %u %o</default>
- <locale name="C">
- <short></short>
- <long></long>
- </locale>
- </schema>
- <schema>
- <key>/schemas/desktop/gnome/thumbnailers/application@vnd.sun.xml.calc/enable</key>
- <applyto>/desktop/gnome/thumbnailers/application@vnd.sun.xml.calc/enable</applyto>
- <owner>opendocument-thumb</owner>
- <type>bool</type>
- <default>true</default>
- <locale name="C">
- <short></short>
- <long></long>
- </locale>
- </schema>
- <schema>
- <key>/schemas/desktop/gnome/thumbnailers/application@vnd.sun.xml.calc/command</key>
- <applyto>/desktop/gnome/thumbnailers/application@vnd.sun.xml.calc/command</applyto>
- <owner>opendocument-thumb</owner>
- <type>string</type>
- <default>/usr/bin/opendocument-thumbnailer %u %o</default>
- <locale name="C">
- <short></short>
- <long></long>
- </locale>
- </schema>
- <schema>
- <key>/schemas/desktop/gnome/thumbnailers/application@vnd.sun.xml.draw/enable</key>
- <applyto>/desktop/gnome/thumbnailers/application@vnd.sun.xml.draw/enable</applyto>
- <owner>opendocument-thumb</owner>
- <type>bool</type>
- <default>true</default>
- <locale name="C">
- <short></short>
- <long></long>
- </locale>
- </schema>
- <schema>
- <key>/schemas/desktop/gnome/thumbnailers/application@vnd.sun.xml.draw/command</key>
- <applyto>/desktop/gnome/thumbnailers/application@vnd.sun.xml.draw/command</applyto>
- <owner>opendocument-thumb</owner>
- <type>string</type>
- <default>/usr/bin/opendocument-thumbnailer %u %o</default>
- <locale name="C">
- <short></short>
- <long></long>
- </locale>
- </schema>
- <schema>
- <key>/schemas/desktop/gnome/thumbnailers/application@vnd.sun.xml.math/enable</key>
- <applyto>/desktop/gnome/thumbnailers/application@vnd.sun.xml.math/enable</applyto>
- <owner>opendocument-thumb</owner>
- <type>bool</type>
- <default>true</default>
- <locale name="C">
- <short></short>
- <long></long>
- </locale>
- </schema>
- <schema>
- <key>/schemas/desktop/gnome/thumbnailers/application@vnd.sun.xml.math/command</key>
- <applyto>/desktop/gnome/thumbnailers/application@vnd.sun.xml.math/command</applyto>
- <owner>opendocument-thumb</owner>
- <type>string</type>
- <default>/usr/bin/opendocument-thumbnailer %u %o</default>
- <locale name="C">
- <short></short>
- <long></long>
- </locale>
- </schema>
- <schema>
- <key>/schemas/desktop/gnome/thumbnailers/application@vnd.sun.xml.impress/enable</key>
- <applyto>/desktop/gnome/thumbnailers/application@vnd.sun.xml.impress/enable</applyto>
- <owner>opendocument-thumb</owner>
- <type>bool</type>
- <default>true</default>
- <locale name="C">
- <short></short>
- <long></long>
- </locale>
- </schema>
- <schema>
- <key>/schemas/desktop/gnome/thumbnailers/application@vnd.sun.xml.impress/command</key>
- <applyto>/desktop/gnome/thumbnailers/application@vnd.sun.xml.impress/command</applyto>
- <owner>opendocument-thumb</owner>
- <type>string</type>
- <default>/usr/bin/opendocument-thumbnailer %u %o</default>
- <locale name="C">
- <short></short>
- <long></long>
- </locale>
- </schema>
- </schemalist>
- </gconfschemafile>
Add Comment
Please, Sign In to add comment