Nice one minio 
Heres another one, grabs the icon from the current gnome theme instead...
Not really sure what the deal is with the mime types I've got
- gnome-mime-application-vnd.oasis.opendocument.text
- ooo-writer
- gnome-mime-application-vnd.stardivision.writer
- openofficeorg-20-writer
- gnome-mime-application-vnd.sun.xml.writer
- ximian-openoffice-writer
- ooo_writer
Any idea which one is the 'correct' one? My monies on the oasis one.
Code:
#!/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 = True
# 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_OPACITY = 0.6 #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")