Page 2 of 11 FirstFirst 1234 ... LastLast
Results 11 to 20 of 106

Thread: HOWTO: Thumbnails for OpenOffice.org 2 files

  1. #11
    Join Date
    Nov 2004
    Location
    Prague, Czech Republic
    Beans
    68

    Re: HOWTO: Thumbnails for OpenOffice.org 2 files

    Well this should solve the problem with templates, delete the old one /usr/bin/ooo2-thumbnailer
    Code:
    sudo rm /usr/bin/ooo2-thumbnailer
    then crate new one
    Code:
    sudo gedit /usr/bin/ooo2-thumbnailer
    and put in this code instead of the one in the first post
    Code:
    #!/usr/bin/python
    # released into the public domain http://creativecommons.org/licenses/publicdomain
    
    import zipfile
    import sys
    import os
    import gnomevfs
    
    inURL=gnomevfs.get_local_path_from_uri(sys.argv[1])
    outURL=sys.argv[2]
    nothumbURL=os.getenv("HOME")+"/Templates"
    nothumbList=nothumbURL.split(os.sep)
    inList=inURL.split(os.sep)
    
    if inList[1]==nothumbList[1] and inList[2]==nothumbList[2] and inList[3]!=nothumbList[3]:
    	zip=zipfile.ZipFile(inURL,mode="r")
    	picture=zip.read("Thumbnails/thumbnail.png")
    	zip.close()
    	thumbnail=open(outURL,"w")
    	thumbnail.write(picture)
    	thumbnail.write("/n")
    	thumbnail.close()
    and save it
    Then delete thumbnails of those files in Teplates directory (thumbnails are in ~/thumbnails you will have to find them by eye or delete entire ~/thumbnails directory)
    restart nautilus
    Code:
    killall -9 nautilus
    It doesn't work 100% because if you put in Templates directory some file which was already thumbnailed it will keep its thumbnail...

    I am also trying to figure out how to add overlay on thumbnails, and turn them to white background insteaad of transparent one. Once I figure it out I will update the first post
    Last edited by minio; March 25th, 2009 at 09:37 PM.

  2. #12

    Re: HOWTO: Thumbnails for OpenOffice.org 2 files

    This version put in a white background as well as overlay an Ooo2 icon

    Screen Shot
    http://img492.imageshack.us/img492/2...eenshot5dl.png

    Code:
    #!/usr/bin/python
    
    import zipfile
    import sys
    import os
    import gnomevfs
    from PIL import Image, ImageEnhance
    
    # Alter these varibles to change thumbnail look
    BaseIconPath = "/usr/share/icons/hicolor/48x48/apps/" # Change this path to alter icons
    
    # Opacity of the icon
    IconOpacity = 0.8
    # Color of the background
    BackgroundColor = "white"
    
    # Two types of icons Ubuntu and Ooo2
    #iconPreFix = "openofficeorg-20-" # Ooo2 Stock
    iconPreFix = "ooo-" # Ubuntu
    
    """
    Open Document Types
    ================
    	Text 				.odt 	
    	Spreadsheet 		.ods 
    	Presentation 		.odp 
    	Drawing 			.odg 
    	Chart 			.odc
    	Formula 			.odf 
    	Database 			.odb 
    	Image 			.odi 
    	Master Document	.odm
    """
    
    # Nicked from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/362879
    def reduce_opacity(im, opacity):
        """Returns an image with reduced opacity."""
        assert opacity >= 0 and opacity <= 1
        if im.mode != 'RGBA':
            im = im.convert('RGBA')
        else:
            im = im.copy()
        alpha = im.split()[3]
        alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
        im.putalpha(alpha)
        return im
    
    inURL=gnomevfs.get_local_path_from_uri(sys.argv[1])
    outURL=sys.argv[2]
    nothumbURL=os.getenv("HOME")+"/Templates"
    nothumbList=nothumbURL.split(os.sep)
    inList=inURL.split(os.sep)
    
    if inList[1]==nothumbList[1] and inList[2]==nothumbList[2] and inList[3]!=nothumbList[3]:
    	zip=zipfile.ZipFile(inURL,mode="r")
    	picture=zip.read("Thumbnails/thumbnail.png")
    	zip.close()
    	thumbnail=open(outURL,"w")
    	thumbnail.write(picture)
    	thumbnail.write("/n")
    	thumbnail.close()
    
    	# Get File Extension    
    	fileExtension = inURL[ len(inURL)-3 :].lower()
    
    	# Select Document Type
    	iconFile = "writer"
    	if fileExtension == "ods":
    		iconFile = "calc"
    	elif fileExtension == "odp":
    		iconFile = "impress"
    	elif fileExtension == "odg":
    		iconFile = "draw"
    	elif fileExtension == "odp":
    		iconFile = "calc"
    	elif fileExtension == "odf":
    		iconFile = "math"
    	elif fileExtension == "odi":
    		iconFile = "draw"
    	elif fileExtension == "odi":
    		iconFile = "template"
    
    	thumbnail = Image.open(outURL).convert("RGBA")
    
    	# Load icon
    	icon = Image.open(BaseIconPath + iconPreFix+ iconFile +".png").convert("RGBA")
    	icon = reduce_opacity(icon, IconOpacity)
    	# Create new img with white bg
    	final = Image.new("RGB", (thumbnail.size[0],thumbnail.size[1]), BackgroundColor)
    	# Add thumbnail
    	final.paste(thumbnail, None, thumbnail)
    	# Add icon
    	x = final.size[0] - icon.size[0]
    	y = final.size[1]-icon.size[1]
    	w = x + icon.size[0]
    	h = y + icon.size[1]
    	final.paste(icon, (x,y,w,h), icon)
    	# Save thumbnail
    	final.save(outURL, "PNG")
    Do sudo gedit /usr/bin/ooo2-thumbnailer and paste in the above code

    Edited
    Added abiltiy to add fade to icon
    Last edited by KillerKiwi; November 6th, 2005 at 02:38 AM.

  3. #13
    Join Date
    Jul 2005
    Location
    Grimstad, Norway
    Beans
    80
    Distro
    Ubuntu 6.10 Edgy

    Re: HOWTO: Thumbnails for OpenOffice.org 2 files

    Very nice... I have not tested it, but the screenshot does look very good.

  4. #14
    Join Date
    Nov 2004
    Location
    Purdue University, Indiana
    Beans
    816
    Distro
    Dapper Drake Testing/

    Re: HOWTO: Thumbnails for OpenOffice.org 2 files

    I see that the new script uses the hicolor folder...ew, hicolor icons...

    Couldn't you modify it to pull the user's current theme directory? And then, if that doesn't have OOO icons, fall back to hicolor? I know this is easy in C using gtk icon theme functions, but I don't know python...

  5. #15
    Join Date
    Nov 2004
    Location
    Prague, Czech Republic
    Beans
    68

    Re: HOWTO: Thumbnails for OpenOffice.org 2 files

    Quote Originally Posted by KillerKiwi
    This version put in a white background as well as overlay an Ooo2 icon
    Works really good Thanks But could you make the icon somewhat transparent? (I think aplha around 0.2 woud be the best) I would do it myself if i know how to use PIL module but i have almost no experience with it (i think that this module hates me ). Good work anyway

  6. #16
    Join Date
    Nov 2004
    Location
    Prague, Czech Republic
    Beans
    68

    Re: HOWTO: Thumbnails for OpenOffice.org 2 files

    Quote Originally Posted by varunus
    I see that the new script uses the hicolor folder...ew, hicolor icons...

    Couldn't you modify it to pull the user's current theme directory? And then, if that doesn't have OOO icons, fall back to hicolor? I know this is easy in C using gtk icon theme functions, but I don't know python...
    This is something what I can do but i don't understand why. If you want change used icons then change the path to them. Thumbnails, once generated, are virtually forever so if i would make them follow current theme then, if you change icon theme, you will have thumbnails with old icons for old files and thumbnails with new icons for new ones and it would be a mess (believe me i've tried it).

  7. #17

    Re: HOWTO: Thumbnails for OpenOffice.org 2 files

    Edited my post to include ability to add fade to icons

  8. #18
    Join Date
    May 2005
    Location
    Marlborough, UK
    Beans
    469
    Distro
    Ubuntu Karmic Koala (testing)

    Re: HOWTO: Thumbnails for OpenOffice.org 2 files

    Nice - I like it alot. The icon in the corner helps alot - this should definatly be in by default - i cant think of any way the original icon is better.
    Regards,
    Haegin
    If all else fails curl up in bed with a good book.
    "<mc44> rob: every time you don’t read the topic, god kills a lolcat"

  9. #19
    Join Date
    Nov 2004
    Location
    Prague, Czech Republic
    Beans
    68

    Re: HOWTO: Thumbnails for OpenOffice.org 2 files

    Ok i rewrote the code once again. It should work (at least it seems to work for me...)
    I've splitted the code into few functions and added KillerKiwi code. I've also changed icon name resolution, detection of ~/Templates directory and made it to do nothing if there is no thumbnail in file. Any comments (also about coding style) and requests are welcome especially from KillerKiwi, since half of the work is his.
    Code:
    #!/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_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
    
    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)
    	#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
    	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
    
    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")
    Last edited by minio; November 10th, 2005 at 03:28 PM.

  10. #20
    Join Date
    Apr 2005
    Location
    IRC or Forums
    Beans
    539
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: HOWTO: Thumbnails for OpenOffice.org 2 files

    Well done, it does work very clean in Dapper Drake

    Excellent!
    Best regards,
    WhatuTag - Q's & A's

Page 2 of 11 FirstFirst 1234 ... LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •