Project 09: Portrait

sketch
//Anthony Pan
//Section C



//empty variable to hold/load portrait
var img;

//load portrait
function preload() {
   img = loadImage("https://i.imgur.com/OZnUWbW.jpg");

}

//set up canvas
function setup() {
    createCanvas(420, 280);

}

//create pixelation effect using mouseX position
function draw() {
    background(220);

    //diplay image
    image(img, 0, 0, width, height);

    //constrain mouseX position to canvas and change diamter of circles according to mouseX position
    var mousePosition = map(constrain(mouseX, 0, 280), 0, width, 0, 20);
    //create circles
    for(var row = 0; row < 32; row++) {
        for(var col = 0; col <= 48; col++){
            var x = col * 10;
            var y = row * 10;
            //sample pixel color
            var sampleColor = img.get(x*15, y*15);
            noStroke();
            fill(sampleColor);
            //draw circle
            circle(x + 10, y + 10, mousePosition); 
        }
    }
}

I wanted to create a portrait that would get pixelated as you moved the mouse from left to right. It was cool to see it become stylized, almost like I was applying a filter to the image.

Project 09: Computational Portrait (Custom Pixel)

project 09 sketch copy

var compportraitImg;

function preload() {
    ImageURL = "https://i.imgur.com/ZTxOcmt.jpg";
    compportraitImg = loadImage(ImageURL);
}

function setup() {
    // canvas proportional to image size
    createCanvas(400, 480);
    compportraitImg.loadPixels();
    background(220);
    frameRate(300);

}

function draw() {
    //have the correct color show at right location 
    var kx = floor(random(compportraitImg.width));
    var ky = floor(random(compportraitImg.height));
    var colorLoc = compportraitImg.get(kx, ky);
    noStroke();
    fill(colorLoc);

    //scale to size of canvas
    x = map(kx, 0, compportraitImg.width, 0, width);
    y = map(ky, 0, compportraitImg.height, 0, height);
    var j = dist(width/2, height/2, x, y);
    j = j % 20;
    bubble(x, y, j, 4);

}

function bubble(x, y, radius, npoints) {
  angle = PI / npoints;
  
  beginShape();
  for (let a = 0; a < PI; a += angle) {
      sx = x + cos(a) * radius;
      sy = y + sin(a) * radius;
      vertex(x + cos(a) * radius, y + sin(a) * radius);
  }
  endShape();
  
}

I made my “custom pixel” in the shape of half of a hexagon and made it such that they populate the canvas in a circle pattern and leave some of the background peeking through.

Project-09: Computational Portrait (Custom Pixel)

sketchDownload
/*Name:Camellia(Siyun) Wang; 
Section: C; 
Email Address: siyunw@andrew.cmu.edu;*/

https://imgur.com/qOtkTrJ
var img;
var i = 0;
var a = 0;

function preload(){
    img = loadImage("https://i.imgur.com/qOtkTrJ.jpg");
}

function setup(){
    createCanvas(380, 480);
    background(0);
    img.resize(380, 480);//resize the image to fit into canvas
    img.loadPixels();
}
//based on the example code
/*function draw(){
    var sw = map(mouseX, 0, width, 1, 5);//
    var x = random(img.width); //randomly select pixels of the image
    var y = random(img.height);
    var pointColor = img.get(x, y); //select the color of the chosen pixel
    stroke(pointColor); 
    strokeWeight(sw);
    line(x,y,x+5,y);
}*/

//based on the simple nested loop logic
/*function draw(){
    var i = random ()
    pointColor = img.get(i, a);
    noStroke();
    fill(pointColor);
    ellipse(i,a,random(1,5),random(3,7));
    i += 5;
    if(i >= 380){
        a += 5;
        i = 0;
    }
}*/

//based on spiral motion
var angle = 0;
var radius = 0;
var r = 6.2; 

function draw() {
    var center_x = width / 2;
    var center_y = height / 2;
    var x = radius *cos(radians(angle));
    var y = radius *sin(radians(angle));
    pointColor = img.get(width/ 2+x, height/2+y);
    //pick color of the chosen pixel from the center of the image 
    //picking in spiral motion which corresponds to the sipral motion of the drawn ellipses
    push();
    //translate the canvas to draw the spiral from the center outwards
    translate(center_x, center_y);
    noStroke();
    fill(pointColor);
    circle(x, y, r);
    radius += 0.1;
    angle += 5;
    r += 0.01;
    pop();
}
   

I started this project with duplicating the example code, then I used the nested loop logic to do the second trial. However, the way this portrait was drawn did not seem interesting enough, since there’s no change in motion and each drawn square. Therefore, I changed the drawing logic to the spiral motion. In this way, the portrait is drawn from the center in gradually amplifying ellipses, so that the final piece has a clearer shape of face in the center and blurry periphery.

First Trial-Duplicated Version
Second Trial-Nested Loop
Final Piece-Spiral Version

project-09

sketch
function preload() {
    var myImage="https://i.imgur.com/0PVv59G.jpeg";
    loadedImage=loadImage(myImage);    
}

function setup() {
  createCanvas(400,400);
  background(0);
  loadedImage.resize(400,400);   
  loadedImage.loadPixels();
  frameRate(100000000000000000000000000000000000000000000000000000000000);
}

function draw() {
  var x=random(width);
  var y=random(height);
  var pixelx=constrain(floor(x),0,width);
  var pixely=constrain(floor(y),0,height);
  var pixelcolor=loadedImage.get(pixelx,pixely);    
  noStroke();
  fill(pixelcolor);    

  square(x,y,random(0,5));    


}

Project 9: Portrait

wpf-portraits.js
//Patrick Fisher, Section B, wpf@andrew.cmu.edu Assignment -09-project
let img;
let drawState = 0; //variable to change the type of drawing that is happeing
let x2 = 5; //startin x variable for the bouncing drawing
let y2 = 5; //startin y variable for the bouncing drawing
let r2 = 5; //startin radius variable for the bouncing drawing
let dx2; //delta for x for bouncing drawing
let dy2; //delta for y for bouncing drawing
var ly1; //first end of the line for the line drawing
var ly2; //second end of the line for line drawing
var lx; //mid x point of the line for line drawing

function preload() {
    img = loadImage('https://i.imgur.com/HFzVcQF.jpeg');
}

function setup() {
    createCanvas(480, 480);
    img.resize(480,480); //fits image to canvas
    imageMode(CENTER);
    noStroke();
    background(255);
    img.loadPixels();
    dx2 = floor(random(1,10)); //sets delta x
    dy2 = floor(random(1,10)); //sets delta y
    while(dy2 == dx2){ //makes sure the deltas are not the same
        dy2 = floor(random(1,10));
    }
    ly1 = random(0,height); //sets the random line locations
    ly2 = random(0,height);
    lx = random(50,width-50); 
}
function draw() {
    if(drawState == 0){ //line drawings
        push();
        strokeWeight(4);
        let pix = img.get(lx,(ly1+ly2)/2);
        stroke(pix,128);
        line(lx-50,ly1,lx+50,ly2);
        ly1 = random(0,height);
        ly2 = random(0,height);
        lx = random(50,width-50);
        pop();
    }
    if(drawState == 1){ //bouncing drawing
        let pix = img.get(x2,y2);
        fill(pix,128);
        circle(x2,y2,2*r2);
        x2 += dx2;
        y2 += dy2;

        if(x2 >= width){
            dx2 = -dx2;
        }
        if(y2 >= height){
            dy2 = -dy2;
        }

        if(x2 <= 0){
            dx2 = -dx2;
        }
        if(y2 <= 0){
            dy2 = -dy2;
        }

    }
    if(drawState == 2){ //  square loop drawing
        var xy3 = mouseX/10;
        var rcnum = height/(xy3+1); //variable for the width of the square based on how many squares there are
        background(255);
        for(var col = 0; col <=xy3; col++){
            for(var row = 0; row <= xy3; row++){
                let pix = img.get((row*(rcnum))+(rcnum)/2,(col*(rcnum))+(rcnum)/2);
                fill(pix,128);
                square(row*(rcnum),col*(rcnum),(rcnum));
            }
        }
    }

}

function mousePressed (){ //changes the drawing with a press of the mouse
    if(drawState == 0) {
        background(255);
        drawState = 1;
    }
    else if(drawState == 1){
        background(255);
        drawState = 2;
    }
    else if(drawState == 2){
        background(255);
        drawState = 0;
    }
}

This project was a lot of fun. I had fun coming up with different ideas of how the photo could be revealed and while not simple, the coding process was rewardingly not strenous. I did have a lot of difficulty with imgur and p5.js trusting my image at first, including a few people commenting mean things, which was annoying.

Project 9: Computational Portrait (Custom Pixel)

sketchDownload
var babyImg;
var adj;

function preload() {
  babyImg = loadImage("https://i.imgur.com/9bQND1O.jpg"); 
  adj = ["cute", "baby", "adorable", "sweet", "lovely", "endearing", "tiny", "cutesy", "kawaii"];
  print(adj); //adjectives that describe the baby
}

function setup() {
  createCanvas(480, 480);
  babyImg.resize(480, 480); //resize the scale of the image to fit the canvas
  background(0);
  imageMode(CENTER); //positioning the image as center
  babyImg.loadPixels(); //loading the adjectives of the baby image
}

function draw() {
  var px = floor(random(babyImg.width)); //randomly select pixels of the image
  var py = floor(random(babyImg.height));
  var pc = babyImg.get(px, py); //bring the color of the chosen pixel
  fill(pc, 255); 
  textSize(12);
  textFont('Georgia');
  text(adj[Math.floor((Math.random()*adj.length))], px, py);
}

While exploring through Imgur in search of images, I found an adorable photo of a smiling baby and I was able to come up with many different adjectives (e.g. cute, adorable, sweet, lovely, etc.) when I first saw this baby. I decided to apply this thought to the project by using the words to render and display this image. It was absolutely delightful watching the rendering process of this cute baby image.

Project 09: Computational Portrait

First reading the instructions for this project, I was reminded of mosaic effect on images.

I proceeded to work with square pixels, but I realized that using a rounded shape like an ellipse could create a more organic jittery effect on my animated portrait.

sketch
var p = 1000;
var particles = [];

function preload() {
    img = loadImage("https://i.imgur.com/30d3jx2.jpeg"); 
}

function setup() {
  createCanvas(480, 480);
  pixelDensity(5);
 
  for (var i = 0; i < p; i++) {
    particles[i] = new squareParticle(240, 240); //appending new square paricles to the array that will draw the shapes
  }
}

function draw() {
 
  for (var i = 0; i < particles.length; i++) {
    particles[i].update();
    particles[i].show();
  }
}


function squareParticle(x, y) { 
  this.x = x;
  this.y = y;
  this.r = random(1, 5); //range of particle size

  this.update = function() { 
    this.x += random(-3, 3);
    this.y += random(-3, 3);

    this.x = constrain(this.x, 0, width);
    this.y = constrain(this.y, 0, height);
  };

  this.show = function() {
    stroke(250);
    strokeWeight(0.1);
    var c = img.get(this.x, this.y);
    fill(c); 
    ellipse(this.x, this.y, this.r, this.r+3);
  

  }
}

Project 9

sketch

var img;

function preload() {
    img = loadImage("https://i.imgur.com/mbg1U1D.jpg");
}

function setup() {
    img.resize(img.width/7, img.height/7);
    createCanvas(img.width, img.height);
    smallPoint = 4;
    largePoint = 10;
    //imageMode(CENTER);
    noStroke();
    img.loadPixels();
    textAlign(CENTER);
    text("CLICK TO CHANGE COLOR",width / 2,height / 2)

}

function draw() {
    //fill with heart
    let pointillize = map(mouseX, 0, width, smallPoint, largePoint);
    let x = floor(random(img.width));
    let y = floor(random(img.height));
    let pix = img.get(x, y);
    fill(pix, 10);
    heart(x, y, pointillize, pointillize);
    var cols = img.get(mouseX, mouseY);

    //fun text fills image according to mouse
    fill(cols);
    textStyle(BOLD);
    textSize(20);
    text("YA", mouseX, mouseY);
}

function heart(x, y, size) {
  //custom shape
  beginShape();
  vertex(x, y);
  bezierVertex(x - size / 2, y - size / 2, x - size, y + size / 3, x, y + size);
  bezierVertex(x + size, y + size / 3, x + size / 2, y - size / 2, x, y);
  endShape(CLOSE);
}

function mousePressed() {
    //background color changes
    background(color(random(255), random(255), random(255)));
}

Once I figured out how to make this project more creative, it was more fun to create. The text that follows the mouse to fill in the image was fun to play around with.

Project 09: Portrait

I started off by playing with different shapes and ways to depict the image. Here are some of the variations I tried.

cross hatching
large circles
star shapes made of circles, controlled by mouse location
squares made of circles, with black shadows

The variation I chose drew the image with randomly bouncing particles. When pressing a key, the color of the particles would be rainbow. When pressing the mouse, larger, black particles moved around the page to act as “erasers”.

random colors when key is pressed

sketch

//Alana Wu
//ID: alanawu
//Project 09

var img;
var balls = [];
var ballNum = 15;
function preload()
{
    img = loadImage ("https://i.imgur.com/2U02mf4.jpg");
}

function makeBall (ax, ay, adx, ady)
{
    var a = {
        x: ax,
        y: ay,
        dx: adx,
        dy: ady,
        dirX: 1,
        dirY: -1,
        stepFunction: moveBall,
        drawFunction: drawBall
    }
    return a;
}

function moveBall ()
{
    //bounce off of walls
    if (this.x >= 338 || this.x <= 0)
    {
        this.dirX *= -1;
    }
    if (this.y >= 400 || this.y <=0)
    {
        this.dirY *= -1;
    }
    this.x += this.dx*this.dirX + random(-3, 3);
    this.y += this.dy*this.dirY + random(-3, 3);
}

function drawBall ()
{
//color of ball = pixel from image in that location
    var col = img.get(this.x, this.y);
    fill (col);
//when key is pressed, taste the rainbow :) 
    if (keyIsPressed) 
    {
        fill (random(255), random(255), random(255));
    }
//draws balls
    circle (this.x, this.y, 5, 5);

//when mouse is pressed, black jittery particles that act as erasers
    if (mouseIsPressed)
    {
        fill (0);
        circle (this.x + random(40), this.y + random(40), 20);
    }
}

function setup()
{
    createCanvas(338, 400);
    background(0);
    noStroke();
//fits image to canvas size
    img.resize(width, height);
//makes objects for each ball
    for (var i = 0; i < ballNum; i++)
    {
        a = makeBall (0,0, random(5), random(5));
        balls.push(a);
    }
}

function draw()
{
    for (var i = 0; i < balls.length; i++)
    {
        balls[i].drawFunction();
        balls[i].stepFunction();    
    }
}










//other shapes and ideas I played with, but didn't use


function ripple2 (size)
{
    for (var x = 0; x < size; x+=5)
    {
        for (var y = 0; y < size; y+=5)
        {
            fill (0);
            circle (mouseX + x + 8, mouseY + y + 8, 5);
            circle(mouseX - x + 8, mouseY - y + 8, 5);
        }
    }

    for (var x = 0; x < size; x+=5)
    {
        for (var y = 0; y < size; y+=5)
        {
            var col = img.get(mouseX + x,mouseY +y);
            fill(col);
            circle (mouseX + x, mouseY + y, 5);
            var col2 = img.get(mouseX - x, mouseY - y);
            fill (col2);
            circle(mouseX - x, mouseY - y, 5);
        }
    }
}


function shape1 (x, y, dx, dy) //diagonal lines
{
    for (var y = 0; y < height; y += dy)
    {
        for (var x = 0; x < width; x += dx)
        {
            var col = img.get(x, y);
            strokeWeight (mouseX/100);
            stroke (col);
            line (x, y, x + dx, y + dy);
        }        
    }
}

function shape2 (x, y, size) //circles, animated if w/ random
{
    for (var y = 0; y < height; y += size)
    {
        for (var x = 0; x < width; x += size)
        {
            var col = img.get(x, y);
            fill(col);
            circle (x, y, size);
        }        
    }
}

function shape3 (x, y, w, h) //moving ellipse in a ripple effect
{
    for (var y = 0; y < height; y += h)
    {
        for (var x = 0; x < width; x += 15)
        {
            var col = img.get(x, y);
            fill(col);
            ellipse (x, y, random(35), h);
        }        
    }

}

function shape4 (x, y, size) //triangles that slowly get bigger along the diagonal
{
    for (var y = 0; y < height; y += size/3)
    {
        for (var x = 0; x < width; x += size)
        {
            var col = img.get(x, y);
            fill(col);
            triangle (x, y, x + size, y + size, x - size, y + size);
        } 
        size += size/10;              
    }

}

function ripple ()
{
    var x = 0;
    var y = 0;
    var r = 5;
    push();
    for (var j = 0; j < count; j++)
    {
        for (var i = 0; i < count; i++)
        {
            x = r*cos(radians(i*200));
            y = r*sin(radians(i*200));
            var col = img.get(mouseX + x, mouseY + y);
            fill (col); 
            circle (mouseX + x, mouseY + y, 5, 5);
        } 
        r += 5; 
    }
    pop();

}

function drawIt () //uncovers image w/ mouse location
{
    var col = img.get(mouseX, mouseY);
    fill (col);
    circle (mouseX, mouseY, 15, 15);
}

Computational Portrait

At first I really did not know how to start off with my portrait. I looked and reread the timeline for a while until I decided I wanted to do some sort of recreation of Chuck Close’s portraits. Instead of using colors to create skin tone, I ended up sticking with the pixel colors that I got using .get for my images. It took me the longest to set up my photo than to actually get my random walk to work.

Figuring Out pixel size
Playing around with accuracy in pixel size

So to get my work to be more like Close’s I made an array of two different size circles inside my pixels. I thought this looked really cool but I was hoping to get more definition in my features by doing this.

Circles and Squares

So I ended up using my Ipad to make a grid of areas in the face that I wanted to be more or less pixelated. I ended up counting and working from this top image to make sure I was using the right constrains in my for loops. I probably could have shortened my code for this project, but I was happy with how it was so I did not bother.

Figuring out proportions

So, for every new and smaller square I was building I was also making my two circles on the top, unless the pixels got too small. I played around with the numbers a lot here. I felt that my image was very stagnant and decided to use noise to get my circles to vibrate in certain parameters. I thought doing this was quite funny because it reminded my a bit of the saying that the Mona Lisas eyes follow you across the room and, I felt that my entire face was doing the same.

file>

var noiseParam = 0;
var noiseStep = 0.1;
var img;
function preload(){
   img = loadImage("https://i.imgur.com/5n4h1dT.png");
}


function setup() {
    img.resize(img.width/3, img.height/3);//pixel image info
    createCanvas(img.width, img.height * .9);
}
function main(dx,dy){
    for(var col = 0; col < img.width; col += 8){ //main
        for(var row = 0; row < img.height; row += 8){
            var c = img.get(col,row);
            fill(color(c));
            noStroke();
            rect(col,row,8,8);
            ellipse((col + 4) + dx, (row - 4) + dy, 6, 6);
            ellipse((col + 4) + dx, (row - 12) + dy, 3, 3);
        }
    }
}

function face(){
    for(var col = 88; col < 248; col += 6){ //whole face
        for(var row = 120; row < 320 ; row += 6){
            var c = img.get(col, row);
            fill(color(c));
            noStroke();
            rect((col + 1.5), (row + 1.5), 5, 5);
            ellipse((col + 5.5) + dx, (row - 4) + dy, 5, 5);
            ellipse((col + 5.5) + dx, (row - 10) + dy, 2.5, 2.5);
        }
    }
}

function mainFeature(){
    for(var col = 104; col < 230; col += 4){ //clarity for face features == more pixels
        for(var row = 160; row < 216 ; row += 4){
            var c = img.get(col,row);
            fill(color(c));
            noStroke();
            rect((col + 4.5), (row + 4.5), 3, 3);
            ellipse((col + 8.5) + dx , (row - 4) + dy, 2, 2);
            ellipse((col + 8.5) + dx , (row - 6) + dy, 1.5, 1.5);
        } 
    }

    for(var col = 128; col < 186; col += 4){ //cont.
        for(var row = 216; row < 296 ; row += 4){
            var c = img.get(col,row);
            fill(color(c));
            noStroke();
            rect((col + 4.5), (row + 4.5), 3, 3);
            ellipse((col + 8.5) + dx ,(row - 4) + dy, 2, 2);
            ellipse((col + 8.5) + dx , (row - 6) + dy, 1.5, 1.5);

        }
    }
}

function eyes(){
    for(var col = 104; col < 136; col += 2){ //eye area 1: no circle, too small
        for(var row = 184; row < 216 ; row += 2){
            var c = img.get(col,row);
            fill(color(c));
            noStroke();
            rect((col + 6.5), (row + 6.5), 1, 1);
        }
    }
    for(var col = 176; col < 232; col += 2){ //eye area 2: no circle, too small
        for(var row = 184; row < 216 ; row += 2){
            var c = img.get(col, row);
            fill(color(c));
            noStroke();
            rect((col + 6.5), (row + 6.5), 1, 1);
        }
    }
}

function draw() {
    background(0);
    var offset = noise(noiseParam);
    offset = map(offset, 0, 1, -8, 8);
    dx = offset;
    dy = offset;
    main(dx, dy);
    face(dx, dy);
    mainFeature(dx, dy);
    eyes(dx, dy);
    noiseParam += noiseStep;
}