#
# Copyright 2023, Ali Arya
# Anyone Can Code: Algorithmic Thinking
# Example 4.4p
#

# graphics.py
# http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/graphics.html 
# http://www.pas.rochester.edu/~rsarkis/csc161/python/pip-graphics.html

# graphic library
from graphics import *
import random

#simple image manipulation
def img():
    # create graphocs window
    window = GraphWin('Image', 600, 600) # give title and dimensions

    #get the image
    imgX = 200
    imgY = 200
    img = Image(Point(imgX, imgY), "prize.png")

    #image dimensions and new color
    w = img.getWidth()
    h = img.getHeight()
    c = color_rgb(0,0,0) #make a color type for graphics.py

    #loop
    for i in range(w):
        for j in range(h):
            img.setPixel(i,j,c)
    img.draw(window)     #draw
    window.getKey()      #wait
    
    #try again with random color
    img.undraw()        #graphics.py needs to undraw if we are changing the image
    
    #loop
    for i in range(w):
        for j in range(h):
            #read the current value. In this case, we know it is 0,0,0. But this is how we read any image.
            rgb = img.getPixel(i,j)
            #manipulate the image data. Make sure the result is between 0 and 255
            rgb[0] = rgb[0] + random.randint(0,255)
            rgb[1] = rgb[1] + random.randint(0,255)
            rgb[2] = rgb[2] + random.randint(0,255)
            #make a new color type
            c = color_rgb(rgb[0],rgb[1],rgb[2])
            #write ot the image
            img.setPixel(i,j,c)

    img.draw(window)     
    window.getKey()

#starting point of the program
img()