ubuntuusers.de

Anhang: opendocument-thumbnailer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/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")
Anhang herunterladen

Diese Revision wurde am 29. Juni 2018 16:25 von ubuntuusers erstellt.