## Jupyter at Bryn Mawr College

Public notebooks: /services/public/dblank / CS110 Intro to Computing / 2017-Spring / Notes

# 1. Image Functions¶

## 1.1 Pixel-based Drawing¶

First, let's draw an image that we create:

In [1]:
PImage img;

void setup() {
size(500, 500);
img = new PImage(200, 300, RGB);
}

void draw() {
background(128);
image(img, 0, 0);
noLoop();
}

Sketch #1:

We know that we can create shapes using the built-in Processing functions like rect(). But what are they doing at the deepest level? Could we do it ourselves?

In [2]:
PImage img;

void setup() {
size(500, 500);
img = new PImage(500, 500, RGB);
}

void draw() {
myrect(10, 20, 200, 200, color(255, 0, 0));
mycircle(300, 300, 75, color(0, 255, 0));
image(img, 0, 0);
noLoop();
}

void myrect(int x, int y, int w, int h, color c) {
// what would go here?
}

void mycircle(int x, int y, int radius, color c) {
// what would go here?
}

Sketch #2:

Yes, we could. All we need to do is change the pixels ourselves, like this:

In [3]:
PImage img;

void setup() {
size(500, 500);
img = new PImage(500, 500, RGB);
}

void draw() {
myrect(10, 20, 200, 200, color(255, 0, 0));
mycircle(300, 300, 75, color(0, 255, 0));
image(img, 0, 0);
noLoop();
}

void myrect(int x, int y, int w, int h, color c) {
// what would go here?
for (int i=x; i < x + w; i++) {
for (int j=y; j < y + h; j++) {
img.pixels[i + j * img.width] = c;
}
}
img.updatePixels();
}

void mycircle(int x, int y, int radius, color c) {
// what would go here?
img.pixels[i + j * img.width] = c;
}
}
img.updatePixels();
}

Sketch #3:

BONUS PROBLEM 1:

Write the code that draws a green circle rather than a green square.

## 1.2 Image Flipping¶

### 1.2.1 Horizontal Mirror¶

For this demonstration, I'll use this picture of a student:

%download https://athena.brynmawr.edu/jupyter/hub/dblank/public/CS110%20Intro%20to%20Computing/2015-Fall/Notes/IMG_4688.jpeg

First, let's just test it and display it:

In [21]:
/* @pjs preload="IMG_4688.jpeg"; */

PImage img;

void setup() {
img.resize(int(img.width/20), int(img.height/20));
size(img.width, img.height);
}

void draw() {
image(img, 0, 0);
noLoop();
}

Sketch #16:

Next, let's flip it so that everything on the right is on the left and everything on the left is on the right.

To do that, we just map the pixels so that the pixels in the first column (zero) will now be in the last column (img.width - 1):

In [5]:
/* @pjs preload="IMG_4688.jpeg"; */

PImage img;
PImage result;

void setup() {
img.resize(int(img.width/5), int(img.height/5));
size(img.width, img.height);
// Create a result from img:
result = new PImage(img.width, img.height);
for (int y=0; y < img.height; y++) {
for (int x=0; x < img.width; x++) {
color c = img.pixels[x + y * img.width];
result.pixels[img.width - 1 - x + (y * img.width)] = c;
}
}
result.updatePixels();
}

void draw() {
image(result, 0, 0);
noLoop();
}

Sketch #5:

BONUS PROBLEM 2:

Flip student upside down.

In [19]:
/* @pjs preload="IMG_4688.jpeg"; */

PImage img;

void setup() {
img.resize(int(img.width/5), int(img.height/5));
size(img.width, img.height);
for (int y=0; y < img.height; y++) {
for (int x=0; x < img.width; x++) {
color c = img.pixels[x + y * img.width];
float r = red(c);
float g = green(c);
float b = blue(c);
// 201, 59, 98
if (190 < r && r < 250 &&
55 < g && g < 90 &&
90 < b && b < 150) {
img.pixels[x + (y * img.width)] = color(0, 255, 0);
}
}
}
img.updatePixels();
}

void mousePressed() {
color c = img.get(mouseX, mouseY);
float r = red(c);
float g = green(c);
float b = blue(c);
println("RGB: " + r + ", " + g + ", " + b);
}

void draw() {
image(img, 0, 0);
noLoop();
}

Sketch #14:

RGB: 165, 48, 74