We are going to transform a 100 x 100-pixel image to a greyscale.
This means that there are therefore
pixels to
modify. You can access the image used in this example at the following
address:
http://xlogo.tuxfamily.org/pics/transfo.png
This is how we are going to proceed: first, we will refer to the
top left corner of the image as [0 0]. Then the turtle will examine
the first 100 pixels of the first line, followed by the first 100
of the second line, and so on. Each time, the colour of the pixel
will be retrieved with findcolor, and the colour will then
be changed to the average of the three [r g b] values. Here is
the relevant code:
(Don't forget to change the filepath in the transform procedure!)
to pixel :list # return the average of three numbers [r g b] make "r first :list make "list bf :list make "g first :list make "list bf :list make "b first :list make "b round (:r+:g+:b)/3 output se list :b :b :b end to greyscale :c if :y=-100 [stop] if :c=100 [make "c 0 make "y :y-1] # We assign the "average" colour of the following pixel to the pen setpc pixel fc liste :c :y # We turn the dot to greyscale dot list :c :y greyscale :c+1 end to transform # You must change the path to the image transfo.png # Eg: setdir "c:\\my_images loadimage "transfo.png] cs ht setdir "/home/loic loadimage "transfo.png make "y 0 greyscale 0 end