summaryrefslogtreecommitdiff
blob: 48eb89b8cd9a1d06a11dc88bdf576d8d40879469 (plain)
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
#!/usr/bin/env python

import gnomevfs
import os
import sys
import zipfile
from PIL import Image, ImageEnhance

# Alter these varibles to change thumbnail look
ICON_PATH = "/usr/share/icons/hicolor/32x32/apps/xmind.png" # Change this path to alter icons
ICON_OPACITY = 0.6 #Opacity of the icon (between 0.0 and 1.0)
THUMBNAIL_BACKGROUND_COLOR = "white" # Color of the background

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):
	#Load icon
	icon = Image.open(ICON_PATH).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 Xmind file and save it
			zip=zipfile.ZipFile(in_file_path,mode="r")
			picture=zip.read("Thumbnails/thumbnail.jpg")
			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")
			if image.size[0]>200:
				image = image.resize((200,image.size[1]*200/image.size[0]))
			if image.size[1]>200:
				image = image.resize((image.size[0]*200/image.size[1],200))
			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

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