Python in Numworks: Duplicating and Grayscale
All three scripts presented today use the math, random, and the Numworks specific
Stage 1: Generating Boxes of Random Colors
The script generates 15 boxes of random colors of 30 pixels each. This the list of available colors:
red: (255, 0, 0)
orange: (255, 127,0)
lime green: (0, 255, 0)
teal: (0, 128, 128)
blue: (0, 0, 255)
yellow: (255, 255, 0)
dark green: (0, 128, 0)
purple: (128, 0, 128)
The kandinsky command fill_rect draws a filled rectangle and has the following syntax:
fill_rect( x, y, width, height, rbg tuple )
(x,y) is the upper left hand corner of the rectangle in pixels (size 320 x 220).
from math import *
from kandinsky import *
from random import *
'''
draw boxes of random colors
Edward Shore
6/7/2026
'''
# set up eight colors
# red,orange,lime,teal,
# blue,yellow,dark green,purple
colors=[(255,0,0),(255,127,0),(0,255,0),(0,128,128),(0,128,0),(128,0,128)]
# draw 15 squares of random colors
for i in range(3):
for j in range(5):
col=choice(colors)
fill_rect(30+30*i,30+30*j,30,30,col)
Stage 2: Duplicating the Boxes
This script generates 15 boxes and then copies them to another area. I wanted the script to determine the pixel color’s because in more complex programs, we may not always know the color tuple of a pixel in advance.
The command get_pixel returns a color’s tuple:
get_pixel(x, y) → (red, green, blue)
Although this script does not use the command, Kandinsky also has a set_pixel command which turns a pixel a certain color.
set_pixel(x, y, (red, green, blue))
from math import *
from kandinsky import *
from random import *
'''
draw boxes of random colors
and dupicate them
Edward Shore
6/7/2026
'''
# set up eight colors
# red,orange,lime,teal,
# blue,yellow,dark green,purple
colors=[(255,0,0),(255,127,0),(0,255,0),(0,128,128),(0,128,0),(128,0,128)]
# draw 15 squares of random colors
for i in range(3):
for j in range(5):
col=choice(colors)
fill_rect(30+30*i,30+30*j,30,30,col)
# get the color
gcol=get_pixel(30+30*i,30+30*j)
fill_rect(150+30*i,30+30*j,30,30,gcol)
Stage 3: Grayscale
The third script converts the duplicated pixel to grayscale using this formula:
grayscale = red * 0.299 + green * 0.587 + blue * 0.114
and the resulting color tuple is (grayscale, grayscale, grayscle)
The weights are selected to accommodate the human eye’s sensitivity to red, green, and blue.
from math import *
from kandinsky import *
from random import *
'''
draw boxes of random colors
and dupicate them in grayscale
Edward Shore
6/7/2026
'''
colors=[(255,0,0),(255,127,0),(0,255,0),(0,128,128),(0,128,0),(128,0,128)]
# draw 15 squares of random colors
for i in range(3):
for j in range(5):
col=choice(colors)
fill_rect(30+30*i,30+30*j,30,30,col)
# get the color
w=get_pixel(30+30*i,30+30*j)
# convert to grayscale
w=w[0]*0.299+w[1]*0.587+w[2]*0.114
# create a tuple
gcol=(w,w,w)
fill_rect(150+30*i,30+30*j,30,30,gcol)
Source
“Color Space Conversion & Binarization for Image Processing” Dynamsoft. May 24, 2019. https://www.dynamsoft.com/blog/insights/image-processing/image-processing-101-color-space-conversion/ Retrieved June 6, 2026.
Eddie
All original content copyright, © 2011-2026. Edward Shore. Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited. This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.




