# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Assignment 1: Building a Better Contact Sheet
# In the lectures for this week you were shown how to make a contact sheet for digital photographers, and how you can take one image and create nine different variants based on the brightness of that image. In this assignment you are going to change the colors of the image, creating variations based on a single photo. There are many complex ways to change a photograph using variations, such as changing a black and white image to either "cool" variants, which have light purple and blues in them, or "warm" variants, which have touches of yellow and may look sepia toned. In this assignment, you'll be just changing the image one color channel at a time
#
# Your assignment is to learn how to take the stub code provided in the lecture (cleaned up below), and generate the following output image:
#
# ![](./readonly/assignment1.png "")
#
# From the image you can see there are two parameters which are being varied for each sub-image. First, the rows are changed by color channel, where the top is the red channel, the middle is the green channel, and the bottom is the blue channel. Wait, why don't the colors look more red, green, and blue, in that order? Because the change you to be making is the ratio, or intensity, or that channel, in relationship to the other channels. We're going to use three different intensities, 0.1 (reduce the channel a lot), 0.5 (reduce the channel in half), and 0.9 (reduce the channel only a little bit).
#
# For instance, a pixel represented as (200, 100, 50) is a sort of burnt orange color. So the top row of changes would create three alternative pixels, varying the first channel (red). one at (20, 100, 50), one at (100, 100, 50), and one at (180, 100, 50). The next row would vary the second channel (blue), and would create pixels of color values (200, 10, 50), (200, 50, 50) and (200, 90, 50).
#
# Note: A font is included for your usage if you would like! It's located in the file `readonly/fanwood-webfont.ttf`
#
# Need some hints? Use them sparingly, see how much you can get done on your own first! The sample code given in the class has been cleaned up below, you might want to start from that.

# ## HINT 1
#
# Check out the `PIL.ImageDraw module` for helpful functions

help(PIL.ImageDraw.Draw.text)


# +
import PIL
from PIL import Image
from PIL import ImageEnhance
from PIL import ImageDraw
from PIL import ImageFont
# read image and convert to RGB
image=Image.open("readonly/msi_recruitment.gif")
image=image.convert('RGB')

fnt = ImageFont.truetype('readonly/fanwood-webfont.ttf', 75)

enhancer=ImageEnhance.Brightness(image)
images=[]
x=1

# this list, pixelData contains 3 tuples. First tuple is for R pixel at intensity int_lst. 
#second tuple is for G pixel at intensity in int_lst. 
#third tuple is for B pixel at intensity in int_lst. 

pixelData = [(20,100,180),(10,50,90),(10,30,50)]

#these two lists for intensity and channel text draw below the images

int_lst=[0.3,0.6,0.95,0.3,0.6,0.95,0.3,0.6,0.95]
channel=[0,1,2]

# this for loop changes images intensities int_lst respectively, 
#in group of 3 images

for i in range(9):
    images.append(enhancer.enhance(int_lst[i]))
    


# this function changes the images pixels as mentioned in pixelData list. This function takes image as input 
#and new pixel value from RGB pixels
def newPix(image, pixData, pixel):
    for y in range(image.size[1]):
        for x in range(image.size[0]):
            if pixel=="R":
                pix= tuple(image.getpixel((x,y)))
                image.putpixel((x,y), (pixData, pix[1],pix[2]))
            if pixel=="G":
                pix= tuple(image.getpixel((x,y)))
                image.putpixel((x,y), (pix[0], pixData,pix[2]))
            if pixel=="B":
                pix= tuple(image.getpixel((x,y)))
                image.putpixel((x,y), (pix[0], pix[1],pixData))



# create a contact sheet from different brightnesses
first_image=images[0]
contact_sheet=PIL.Image.new(first_image.mode, (first_image.width*3,first_image.height*3+210))
x=0
y=0
n=0
x_1=0
y_1=0
d=ImageDraw.Draw(contact_sheet)

for n in range(9):
    # Lets paste the current image and drawing into the contact sheet
    y1=y+450    

#This is for R and for first 3 images with intensity 0.1, 0.5, 0.9 with R values 20 (image 1), 
 #100 (image 2), 180 (image(3).
    if n<3:
        newPix(images[n], pixelData[0][n], "R")
        contact_sheet.paste(images[n], (x, y)) 
        d.text(xy=(x,y1), text="channel " +str(channel[0])+" intensity "+ str(int_lst[n]), fill=None, font=fnt, anchor=None, spacing=0, align="left", direction=None, features=None, language=None, stroke_width=0, stroke_fill=None)

#This is for G and for first 3 images with intensity 0.1, 0.5, 0.9 with G values 30 (image 4), 
 #70 (image 5), 110 (image(6).
    if n>2 and n<6:
        newPix(images[n], pixelData[1][x_1], "G")
        contact_sheet.paste(images[n], (x, y) )
        d.text(xy=(x,y1), text="channel " +str(channel[1])+" intensity "+ str(int_lst[x_1]), fill=None, font=fnt, anchor=None, spacing=0, align="left", direction=None, features=None, language=None, stroke_width=0, stroke_fill=None)
        x_1=x_1+1 

#This is for B and for first 3 images with intensity 0.1, 0.5, 0.9 with G values 30 (image 7), 
 #50 (image 8), 70 (image(9).
    if n>5:
        newPix(images[n], pixelData[2][y_1], "B")
        contact_sheet.paste(images[n], (x, y) )
        d.text(xy=(x,y1), text="channel " +str(channel[2])+" intensity "+ str(int_lst[y_1]), fill=None, font=fnt, anchor=None, spacing=0, align="left", direction=None, features=None, language=None, stroke_width=0, stroke_fill=None)
        y_1=y_1+1
  
    n=n+1    
    

    # Now we update our X position. If it is going to be the width of the image, then we set it to 0
    # and update Y as well to point to the next "line" of the contact sheet.
    if x+first_image.width == contact_sheet.width:
        x=0
        y=y+first_image.height+70
        
    else:
        x=x+first_image.width

# resize and display the contact sheet
contact_sheet = contact_sheet.resize((int(contact_sheet.width/2),int(contact_sheet.height/2) ))
display(contact_sheet)
# -

# ## HINT 2
#
# Did you find the `text()` function of `PIL.ImageDraw`?

# ## HINT 3
#
# Have you seen the `PIL.ImageFont` module? Try loading the font with a size of 75 or so.

# ## HINT 4
# These hints aren't really enough, we should probably generate some more.



StarcoderdataJupyterScriptsDedupFiltered