15-112 Introduction to
Programming
and
Computer Science

To use ImageWriter, you would need to install OpenCV.
You can do that by going to the terminal on a MAC or a command prompt on windows and type:

pip3 install opencv-python

To check if you opencv installation works, do the following in IDLE:

import cv2

the import should be successful and you should not see any error messages.

Image Library (Download)

loadPicture(filename)
This function loads the image name passed in. The function will pass back a reference to the picture which needs to be stored for any future processing. This function will not show the image

showPicture(pic)
This function shows the image passed in as parameter.

updatePicture(pic)
If any changes are made to the picture, the changes are not displayed on the image being shown until this function is called.

savePicture(pic,filename):
This function will save the picture passed in as a file using the name passed in the variable filename. Make sure that the string filename has the proper file extension for an image. File names can have extensions “gif”, “jpg”, “bmp”.

getWidth(pic):
This function returns the width of the picture as an integer.

getHeight(pic):
This function returns the height of the picture as an integer.

getColor(pic,x,y):
This function returns the color at location x, y of the picture. The color is returned as a list of three values representing the red, green, and blue component of the color.

setColor(pic,x,y,color):
Sets the color of the location x, y to color passed in. The color is a list of three elements representing the red, green, and blue component of the color

Example usage:

import ImageWriter

#load a picture and save the reference in variable called mypic
mypic = ImageWriter.loadPicture("apple.gif")

# show the picture
ImageWriter.showPicture(mypic)

# loop over complete width of the picture
w = ImageWriter.getWidth(mypic)
for i in range(0,w):
    # set the color of row 40 to color with red = 50
    # green = 5, and blue = 200.
    ImageWriter.setColor(mypic,i,40,[50,5,200])

# update the picture so you can see the changes
ImageWriter.updatePicture(mypic)

#save the picture as sav.jpg
ImageWriter.savePicture(mypic,"sav.jpg")