ImageDemo.py

From IPRE Wiki
Jump to: navigation, search
from myro import *

init()

image = takePicture('gray')

show(image)
askQuestion("Click when ready to see the negative.", "?")

#calculate and display the image negative
negImage= makePicture(getWidth(image), getHeight(image))
for row in range(getHeight(image)):
    for col in range(getWidth(image)):
        c = getPixel(image, col, row)
        nc = makeColor(255 - getRed(c), 255 - getGreen(c), 255 - getBlue(c))
        setPixel(negImage, col, row, nc)

show(negImage, "negative")

#again, wait for the user to finish being impressed
askQuestion("Click when ready to see edge image", "?")

# now calculate and display an edge-enhanced image
edgeImg = makePicture(getWidth(image), getHeight(image))

edgeKernel = [[ -1, -1, -1 ],
              [ -1,  9, -1 ],
              [ -1, -1, -1 ]]

for y in range(1,getHeight(image)-1):
    for x in range(1,getWidth(image)-1):

        sum = 0 #Kernel sum for this pixel
        for ky in range (-1, 2, 1):
            for kx in range(-1, 2, 1):
                # Calculate the adjacent pixel for this kernel point
                #int pos = (y + ky)*img.width + (x + kx)
                # Image is grayscale, red/green/blue are identical
                val = getGray(getPixel(image, x+kx, y+ky))
                # Multiply adjacent pixels based on the kernel values
                sum += edgeKernel[ky+1][kx+1] * val

        # For this pixel in the new image, set the gray value
        # based on the sum from the kernel
        #edgeImg.pixels[y*img.width + x] = color(sum)
        sum = min( max( sum, 0 ), 255 )
        setGray(getPixel(edgeImg, x, y), round(sum))
  

show(edgeImg, "Edge Image")

#The user will tell us when s/he is ready to proceed
askQuestion( "Click when ready to see the blur image.", "?")

#calculate and display a blurred image
blurImg = makePicture(getWidth(image), getHeight(image))

v = 1.0 / 9.0
blurKernel = [[v, v, v ], [v, v, v], [v, v, v]]

for y in range(1,getHeight(image)-1):
    for x in range(1,getWidth(image)-1):
        
        sum = 0 # Kernel sum for this pixel
        for ky in range (-1, 2, 1):
            for kx in range(-1, 2, 1):
                # Calculate the adjacent pixel for this kernel point
                # int pos = (y + ky)*img.width + (x + kx)
                # Image is grayscale, red/green/blue are identical
                val = getGray(getPixel(image, x+kx, y+ky))
                # Multiply adjacent pixels based on the kernel values
                sum += blurKernel[ky+1][kx+1] * val
                
        # For this pixel in the new image, set the gray value
        # based on the sum from the kernel
        sum = min( max( sum, 0 ), 255 )
        sum = min( max( sum, 0 ), 255 )
        setGray(getPixel(blurImg, x, y), round(sum))
 
show(blurImg, "Blur Image")

# Let the user ponder this image
askQuestion( "Click when ready to see an X in the negative image.", "?" )

# Create an X in the negative image and display it

for y in range(getHeight(image)):
    setPixel(negImage, y, y, red)
    setPixel(negImage, getHeight(negImage)-y, y, red)

show(negImage, "X")

# Wait for the user to be ready to exit
askQuestion( "Click when ready to exit.", "?")