## Jupyter at Bryn Mawr College

Public notebooks: /services/public/dblank / CS110 Intro to Computing / 2017-Fall / Notebooks

# Image Functions 2¶

To work with images in Jupyter Processing, you need to do three things:

2. List the images in a @pjs comment (see below)
In [1]:
/* @pjs preload="pic3.jpg,tornadovolcano.jpg"; */

PImage img;
PImage bimg;

void setup() {
int guess = 13;
img.resize(600, 400);
bimg.resize(600, 400);
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);
if (
(((201 - guess) < r && r < (201 + guess)) &&
((59 - guess) < g  && g < (59 + guess)) &&
((98 - guess) < b && b < (98 + guess)))
||
(((180 - guess) < r && r < (211 + guess)) &&
((141 - guess) < g  && g < (141 + guess)) &&
((151 - guess) < b && b < (151 + guess)))
|| // 162, 113, 116
(((162 - guess) < r && r < (162 + guess)) &&
((113 - guess) < g  && g < (113 + guess)) &&
((120 - guess) < b && b < (116 + guess)))
||
(((123 - guess) < r && r < (123 + guess)) &&
((77 - guess) < g  && g < (77 + guess)) &&
((80 - guess) < b && b < (80 + guess)))) {
img.pixels[x + (y * img.width)] = bimg.pixels[x + (y * bimg.width)];
}
}
}
img.updatePixels();
}

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

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

Sketch #1:

In [2]:
/* @pjs preload="pic3.jpg"; */
// (34, 89)
// (135, 186)

PImage img;

void setup() {
head = createImage(135 - 34, 186 - 89, RGB);
img.resize(600, 400);
size(img.width, img.height);
for (int x=34; x < 135; x++) {
for (int y=89; y < 186; y++) {
head.pixels[x - 34 + (y - 89) * head.width] = img.pixels[x + y * img.width];
}
}
for (int i =0; i < 100; i++) {
}

}

float hx, hy, vx, vy;

this.hx = hx;
this.hy = hy;
this.vx = random(5) - 2.5;
this.vy = random(5) - 2.5;
}

void move() {
hx += vx;
hy += vy;
if (hx > width)
vx = -vx;
else if (hx < 0)
vx = -vx;
if (hy > height)
vy = -vy;
else if (hy < 0)
vy = -vy;
}

void draw() {
}
}

void draw() {
background(255);
for (int i =0; i < 100; i++) {