#!/usr/bin/env python

# writeRGB.py: show two ways of saving out an 8-bit RGB PNG

import Image, numpy

# First example, in which output data is created as an array
# of a particular size
out_data =  numpy.zeros((32,256,3),dtype=numpy.uint8)

# Example of filling in values with linear ramp
# In Project 1, colors in the output colormapped image will
# come from your function that gives the RGB color, from a
# colormap, given an input data value
for i in range(256):
  out_data[:,i,0] = i
  out_data[:,i,1] = 256-i
  out_data[:,i,2] = i

out_image = Image.fromarray(out_data,mode='RGB')
out_image.save("rgb1.png")

############################################

# Second example, creating the output numpy array based on shape
# of given image (as you'll have to do for Project 1)
# http://people.cs.uchicago.edu/~glk/class/scivis/proj1/six.png
in_image = Image.open("six.png")
in_data = numpy.asarray(in_image)
(sy, sx) = (in_data.shape[0], in_data.shape[1])
out_data = numpy.zeros((sy, sx, 3),dtype=numpy.uint8)

# Again, your program must determine output colors based on
# the colormap you create
for j in range(sy):
  for i in range(sx):
    out_data[j,i,0] = in_data[j,i]/256
    out_data[j,i,1] = 255 - in_data[j,i]/256
    out_data[j,i,2] = in_data[j,i]/256

out_image = Image.fromarray(out_data, "RGB")
out_image.save("rgb2.png")
