On Thu, Oct 9, 2008 at 9:52 AM, Kevin Cozens <[EMAIL PROTECTED]> wrote:
> Hedley Finger wrote:
>> Can anybody point me to a Gimp plug-in or external utility that can
>> convert *.ase swatch files from Adobe Kuler to Gimp *.gpl files please?
> If you haven't found anything using Google and there is no plug-in already
> available in the registry, someone will have to create a plug-in, or script
> file, to do the conversion. That someone will need to have access to .ase
> files and to the details of the format of those files.

OK - I took a shot at a plug-in.  I doubt that it works properly under
Windows - I have not had time to test it (yet).  Also, once the
palette is loaded (if it works :), you need to manually refresh the
palette list - the new palette will appear toward the top.  This
plug-in does not handle the full ASE spec (notably RBG only)- I doubt
ASE files from sources other than Kuler will work.  If you are not
using GIMP from current svn, you need to change the 'gimp_folder'
variable at the top of the file to match your .gimp folder.

Oh yeah - it's under 'Filters->Import ASE palette...' - I need to move
it somewhere more useful.

There are a ton of FIXME items that I still need to attend to, but if
anyone is feeling brave, I'd love feedback on it...


PS - (unofficial) ASE file spec is here:
#!/usr/bin/env python
# Author: Chris Mohler
# Copyright 2008 Chris Mohler
# License: GPL v3
# GIMP plugin to convert ASE palettes to GPL palettes

#FIXME - is there a way to autogenerate this?
gimp_folder = ".gimp-2.7" # for the target directory - change to gimp-2.6, but not gimp-2.6.1, for example

from gimpfu import *
import sys,  os,  re
from struct import unpack_from,  unpack

home_dir = os.environ['HOME'] #FIXME - does this work on windows?
pal_dir = os.path.join(home_dir,  gimp_folder,  "palettes") 
#print ("Palette Dir: " + pal_dir)

# Get file name from command line, if exists - UNTESTED
if (sys.argv[1] != "-gimp"):
    this_file = sys.argv[1]
    print "ASE file: " + this_file

def ase_converter(this_file):
    #print "ASE file: " + this_file
    # Strip of the "file:///" prefix - if called by nautilus, etc
    if this_file.startswith('file:///'):
        this_file = this_file.replace('file:///', '/')
        pal_gpl = "GIMP Palette\nName: " # start GPL (GIMP Palette) file
        NUL = chr(0) # NULL byte
        SoH = chr(1) # Start of ASE separator byte
        f = open(this_file) #open ASE file
        ase_header = f.read(4) # first 4 bytes
        #print("ASE Header: " + ase_header)
        if ase_header == "ASEF": # first 4 bytes should be "ASEF"
            ase_version = f.read(4) # next 4 bytes are version - we don't need this.
            ase_swatches_data = f.read() # rest of the file should be swatch data
            #print ("ASE Swatches data: " + ase_swatches_data)
            ase_swatches = ase_swatches_data.split(SoH + NUL + NUL + NUL) # split by seperator
            pal_len = ase_swatches.pop(0) # This seems to be the file length - I don't think we need it
            pal_title_data = ase_swatches.pop(0).lstrip() #This should be the title of the palette
            pal_title = ""
            # check for strange characters in the palette name
            for c in pal_title_data:
                char = unpack('>s',  c) # Little endian string
                if re.match("[A-Za-z0-9_\-]",  char[0]):
                    pal_title += char[0] # only add if A-Z or 0-9
            #print ("Palette title: " + pal_title)
            pal_gpl += pal_title + "\nColumns: 1\n#\n" #Finish GPL header info
            j = 0
            for swatch in ase_swatches:
                j = j+1
                swatch = swatch.lstrip().split(" ",  1) # split on the first space
                swatch[0] = swatch[0].replace("\x16\x00\x01\x00\x00", "") # strip "funk" from swatch color mode
                #print swatch[0]
                if swatch[0] == "RGB": # only operate on RGB swatches
                    red = unpack_from('>f',  swatch[1]) #read 4 bytes, unpack to float - little endian
                    green = unpack_from('>f',  swatch[1],  4) #next 4 bytes
                    blue = unpack_from('>f',  swatch[1],  8) #next 4 bytes
                    red = (red[0] * 255) # multiply float by 255
                    red = int(round(red,  0)) # round to int
                    green = (green[0] * 255)
                    green = int(round(green,  0))
                    blue = (blue[0] * 255)
                    blue = int(round(blue,  0))
                    #print red,  green ,  blue
                    # Add each swatch's RGB values to the GPL
                    pal_gpl += str(red) + "\t" + str(green) + "\t" + str(blue) + "\t" + "Color_" + str(j) + "\n"
            #print pal_gpl
            pal_file = os.path.join(pal_dir,  (pal_title + ".gpl"))
            #print pal_file
            pf = open(pal_file,  'w') #open palette file
            pf.write(pal_gpl) # write GPL data
            pf.close # close file handle
    except Exception, e:
        # Oops! Cowardly bailing out!
        print '%s: %s' % (e.__class__.__name__, e)
        if isinstance(e, SystemExit): 
            raise # take the exit
            #FIXME - show a GIMP error here!
        print 'Nonstandard Exception %r: %r' % __import__('sys').exc_info()[:2]
        #FIXME - show a GIMP error here!
    # if called from CLI, quit GIMP
    if (sys.argv[1] != "-gimp"):

	"Convert ASE palette to GIMP palette",
	"Convert ASE palette to GIMP palette",
	"Chris Mohler",
	"Chris Mohler",
	"<Toolbox>/Xtns/Import ASE palette...", #FIXME - this is not the correct spot to attach!
		(PF_FILE, "this_file", "ASE File", ""),

Gimp-user mailing list

Reply via email to