Calculate colors and build a ColorThemes in Keynote

Hi.All I hope every is still happy and well.

I could really need some help to make this AppleScript to become something beautiful.

This Script use python function to calculate the colors value and pick whose to build color themes.

Reference: Reference: https://lokeshdhakar.com/projects/color-thief/

PS: TO BE ABLE TO RUN THIS APPLESCRIPT YOU NEED TO:
Code: https://github.com/fengsp/color-thief-py

Or

pip install colorthief

Keynote Template and Document
https://filebin.net/ub4ucnprm10wny0h

The AppleScript is executed on specific template in Keynote.

set pathToDesktop to POSIX path of (path to desktop as text)

set scriptPath to pathToDesktop & "getColorDominate.py"
set imageFile to pathToDesktop & "puppy1.jpg"

set imageData to do shell script "echo " & " | " & quoted form of scriptPath & space & "-i " & imageFile

set dColor to get words 1 thru 3 of imageData

set color1 to get words 4 thru 6 of imageData
set color2 to get words 7 thru 9 of imageData
set color3 to get words 10 thru 12 of imageData
set color4 to get words 13 thru 15 of imageData

--~~~~~~~~~

tell application id "com.apple.iWork.Keynote"
	tell current slide of front document
		tell table "DominantColor"
			tell cell 1
				set background color to {my c8To16(item 1 of dColor), my c8To16(item 2 of dColor), my c8To16(item 3 of dColor)}
			end tell
		end tell
		
		--~~~~~~~~~~~
		
		tell table "ColorThemes"
			tell cell 1
				set background color to {my c8To16(item 1 of color1), my c8To16(item 2 of color1), my c8To16(item 3 of color1)}
			end tell
			tell cell 2
				set background color to {my c8To16(item 1 of color2), my c8To16(item 2 of color2), my c8To16(item 3 of color2)}
			end tell
			tell cell 3
				set background color to {my c8To16(item 1 of color3), my c8To16(item 2 of color3), my c8To16(item 3 of color3)}
			end tell
			tell cell 4
				set background color to {my c8To16(item 1 of color4), my c8To16(item 2 of color4), my c8To16(item 3 of color4)}
			end tell
		end tell
	end tell
end tell

on c8To16(c)
	return (c * 257)
end c8To16

=====>

If you like to improve this AppleScript or Python Script I will be more and happy.

Regards.

Fredrik

Here is XMLRPC version of the same Python module colorthief.
In Keynote we make table like this.


There cell 1 to 16 will be merged to display the target image.

This is XMLRPC method to call, methodName is colorPalette and take 2 parameters.
The URL of the image and colorCount. In this example I only use 4 colors as you could
see on the image.

Ps. The script do not update the image in merged cells it have been done manually.

def xmlrpc_colorPalette(self, *args):
    """
    """
    from colorthief import ColorThief

    color_thief = ColorThief(args[0])
    dominant_color = color_thief.get_color(quality=1)
    palette = color_thief.get_palette(color_count=args[1])
    return palette

The AppleScript will update cell 17 - 20 with the dominate color.

property colorCount : 4

set choosePicture to POSIX path of (choose file)

tell methodCall
	set colorList to its methodName:¬
		"colorPalette" params:{choosePicture, colorCount - 1}
end tell

set dominateColorList to {}
repeat with anItem in colorList
	set colorItem to {(item 1 of (contents of anItem)) * 255, ¬
		(item 2 of (contents of anItem)) * 255, (item 3 of (contents of anItem)) * 255}
	set the end of dominateColorList to colorItem
end repeat

tell application "Keynote"
	tell current slide of front document
		tell table "Table 1"
			(*
			* The cell 1 to 16 is merged to show image fill of the target image.
			*)
			tell cell 17 to set background color to item 1 of dominateColorList
			tell cell 18 to set background color to item 2 of dominateColorList
			tell cell 19 to set background color to item 3 of dominateColorList
			tell cell 20 to set background color to item 4 of dominateColorList
		end tell
	end tell
end tell

script methodCall
	on methodName:methodName params:params
		tell application "http://locaLhost:7080"
			call xmlrpc {method name:methodName, parameters:params}
		end tell
	end methodName:params:
end script