计算机专外Week2-Exercises

Week 2 > Image For-Loop Exercises > Vertical2


Q1: Add code inside the loop to modify flowers.jpg like this: set each pixel to have green of 0, leaving the red and blue values unchanged. The result should be that the flowers look red, since the yellow was made of red+green light.

image = new SimpleImage("flowers.jpg");

for (pixel: image) {
  // Your code here

  pixel.setGreen(0);

}

print(image);

Q2: Add code inside the loop to modify flowers.jpg like this: set each pixel to have red 255, green 0, blue 0.

image = new SimpleImage("flowers.jpg");

for (pixel: image) {
  // Your code here
  pixel.setRed(255);
  pixel.setGreen(0);
  pixel.setBlue(0);
}

print(image);

Week 2 > Image Expressions Exercises > Problems


Q1:Write code to fix the 51020-poppy.png image which should show an orange California Poppy in the foreground (the California state flower!). The background is mostly green with some in the upper-right.

image = new SimpleImage("51020-poppy.png");

for (pixel: image) {
  // Your code here
  pixel.setRed(pixel.getRed()*10);
  pixel.setGreen(pixel.getGreen()*5);
  pixel.setBlue(pixel.getBlue()*20);
}

print(image);

Q2:Write code to fix the 51020-stop-sky.png image which should show a red stop sign front with a background of a light blue sky and green tree leaves.

image = new SimpleImage("51020-stop-sky.png");

for (pixel: image) {
  // Your code here
  pixel.setRed(pixel.getRed()*20);
  pixel.setGreen(pixel.getGreen()*10);
  pixel.setBlue(pixel.getBlue()*5);
}

print(image);

Q3: Write code to fix the 51020-oranges.png image which should show a box of oranges. The box itself is dull gray. The sign on the box is black, with “organic” written in light orange, and the rest of the letters in white.

image = new SimpleImage("51020-oranges.png");

for (pixel: image) {
  // Your code here
  pixel.setRed(pixel.getRed()*20);
  pixel.setGreen(pixel.getGreen()*5);
  pixel.setBlue(pixel.getBlue()*10);
}

print(image);

Week 2 > Image Puzzle Exercises > Problems


Q1 Iron Puzzle: Write code to fix the puzzle-iron.png image. The red and green values in the image are random noise, so they should be set to 0. The real image is in the blue values, which have been divide by 10. The “solution” image will look blue, since it is exclusively in the blue values, so don’t worry about that. We’ll see a way to fix that blueness in a later section.

image = new SimpleImage("puzzle-iron.png");

for (pixel: image) {
  // Your code here
  pixel.setRed(pixel.getRed()*0);
  pixel.setGreen(pixel.getGreen()*0);
  pixel.setBlue(pixel.getBlue()*10);
}

print(image);

Q2 Copper Puzzle: Write code to fix the puzzle-copper.png image. The red values in the image are random noise, so they should be set to 0. The real image is in the blue and green values, which have been divide by 10.

image = new SimpleImage("puzzle-copper.png");

for (pixel: image) {
  // Your code here
    pixel.setRed(pixel.getRed()*0);
    pixel.setGreen(pixel.getGreen()*10);
  pixel.setBlue(pixel.getBlue()*10);
}

print(image);

Week 2 > Grayscale Image Exercises > Problems


Q1: The image below, golden-gate-red.jpg, shows the golden gate bridge, but all of the data is in the red values.
Write code that for each pixel copies the red value over to be the green and blue value. The result will be to change the image to grayscale which will look better.

image = new SimpleImage("golden-gate-red.jpg");

for (pixel: image) {
  // Your code here
  pixel.setGreen(pixel.getRed());
  pixel.setBlue(pixel.getRed());
}

print(image);

Q2: Here is the banana.jpg image:
Write code to change the banana.jpg image to be grayscale. Reminder: here is the line used in the loop to compute the average of the red/green/blue values and store that value in a variable named “avg”.

avg = (pixel.getRed() + pixel.getGreen() + pixel.getBlue())/3;

image = new SimpleImage("banana.jpg");

for (pixel: image) {
  // Your code here
  avg = (pixel.getRed() + pixel.getGreen() + pixel.getBlue())/3;
   pixel.setRed(avg);
    pixel.setGreen(avg);
  pixel.setBlue(avg);
}

print(image);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值