2

My goal is to double the picture size then change the left half to grayscale, then change the green value of the top right half and the blue value of the bottom right half. I have values that I found in my textbook for the grayscale but im not sure if thats what I actually use. And I also am unsure if I program each of these different values using for loops or just something different

So far my code is:

 def crazyPic(newGreen,newBlue,pic,file):
      show(pic)
      newPic = makeEmptyPicture(getWidth(pic)*2,getHeight((pic)*2
           for x in range(width):
              for y in range(height):
                  for px in getPixel(pic,0,100):
                  nRed = getRed(px) * 0.299
                  nGreen = getGreen(px) * 0.587
                  nBlue = getBlue(px) * 0.114
                  luminance = nRed + nGreen + nBlue
                  setColor(px,makeColor(luminance,luminance,luminance)
RacerNerd
  • 1,579
  • 1
  • 13
  • 31
user2220660
  • 21
  • 1
  • 4

1 Answers1

0

I shouldn't give full answers as JES is an application designed for students, but I think that three months later one can give a full working sample which can be used as reference for the others...

This should be close to what you attempted to do:

Note : your approach of a simple double loop over x and y was the right one.

def crazyPic(pic, newRed, newGreen, newBlue):

    w = getWidth(pic)
    h = getHeight(pic)
    new_w = w * 2
    new_h = h * 2
    newPic = makeEmptyPicture(w * 2, h * 2)

    for x in range(new_w):
      for y in range(new_h):
          new_px = getPixel(newPic, x, y)

          # Top-left: B&W
          if (x < w) and (y < h):
            px = getPixel(pic, x, y)
            nRed = getRed(px) * newRed #0.299
            nGreen = getGreen(px) * newGreen #0.587
            nBlue = getBlue(px) * newBlue #0.114
            luminance = nRed + nGreen + nBlue
            new_col = makeColor(luminance, luminance, luminance)

          # Top-right
          elif (y < h):
            px = getPixel(pic, x - w, y)
            nRed = getRed(px) * newRed
            new_col = makeColor(nRed, getGreen(px), getBlue(px))

          # Bottom-left
          elif (x < w):
            px = getPixel(pic, x, y - h)
            nGreen = getGreen(px) * newGreen
            new_col = makeColor(getGreen(px), nGreen, getBlue(px))

          # Bottom-right
          else:
            px = getPixel(pic, x - w, y - h)
            nBlue = getBlue(px) * newBlue
            new_col = makeColor(getGreen(px), getBlue(px), nBlue)

          setColor(new_px, new_col)

    return newPic

file = pickAFile()
picture = makePicture(file)
#picture = crazyPic(picture, 0.299, 0.587, 0.114)
# Here, with my favorite r, g, b weights
picture = crazyPic(picture, 0.21, 0.71, 0.07)

writePictureTo(picture, "/home/quartered.jpg")

show(picture)


Output (Painting by Antoni Tapies):


enter image description here......From......enter image description here


Here is a more detailed thread about greyscale.


Community
  • 1
  • 1
Gauthier Boaglio
  • 10,054
  • 5
  • 48
  • 85