Game of Life on the photo.

Creative coding works with a Game of Life.

With the image from
Two Tabby Cats
http://www.photos-public-domain.com/2016/11/15/two-tabby-cats/


Creative coding works with a Game of Life.

With the image from
Orange And White Cat Closeup
http://www.photos-public-domain.com/2011/01/07/orange-and-white-cat-closeup/ 

 

Description of this image manipulation code.

This is an image manipulation type creative coding work made with the 'Processing'.

It's yet another way to apply Game of Life to photo images. Add some rule on Conway's Game of Life to make a shape like a fungus colony.

You can use your own photo with this code. You can hard coding the path of your photo image file or you can give command line parameter like this.

/your_processing_installed_path/processing-java --force --sketch=/your_sketch_path/ --run /your_photo_file_path

 







The 'Processing' code example.

Please feel free to use this example code under the terms of the GPL. To see other works based on my code is my pleasure. And my honor.

// Funpuss.
// @author @deconbatch
// @version 0.2
// Processing 3.2.1
// 0.1 2018.04.15
// 0.2 2018.12.23

PImage imgInit;
float[] imgBrightArray;

void setup() {
  size(1080, 1080);
  colorMode(HSB, 360, 100, 100, 100);
  noStroke();
  smooth();
  noLoop();

  float baseWH = 980;

  if (args == null) {
    // you can use your photo in ./data/your_image.jpg
    imgInit = loadImage("your_image.jpg");
  } else {
    // args[0] must be image path
    imgInit = loadImage(args[0]);
  }

  // fit to canvas size
  float rateSize = baseWH / max(imgInit.width, imgInit.height);
  float canvasW  = imgInit.width * rateSize;
  float canvasH  = imgInit.height * rateSize;
  imgInit.resize(int(canvasW), int(canvasH));
  imgInit.loadPixels();

  // need for Life Game calculation
  // NO GOOD -> int i = 0; i < imgInit.pixels.length; ++i
  imgBrightArray = new float[imgInit.pixels.length];
  for (int idxW = 1; idxW < imgInit.width - 1; ++idxW) {  
    for (int idxH = 1; idxH < imgInit.height - 1; ++idxH) {
      int idxPoint = idxH * imgInit.width + idxW;
      imgBrightArray[idxPoint] = brightness(imgInit.pixels[idxPoint]);
    }
  }
  
  background(0.0, 0.0, 90.0, 100.0);

}

void draw() {

  translate((width - imgInit.width) / 2, (height - imgInit.height) / 2);

  blendMode(BLEND);
  drawImagePixels(14,  10, getBorderBrightness(10));
  drawImagePixels(22,  20, getBorderBrightness(50));
  drawImagePixels(28,  40, getBorderBrightness(25));

  blendMode(SCREEN);
  drawImagePixels( 8,   5, getBorderBrightness(10));
  drawImagePixels( 8,   5, getBorderBrightness(50));
  drawImagePixels( 8,   5, getBorderBrightness(25));

  saveFrame("frames/####.png");
  exit();
  
}

/* ---------------------------------------------------------------------- */
void drawImagePixels(
                     float pBriPowMax,    // amplify brightness 
                     int   pCalcCicle,    // calculate Life Game pCalcCicle times
                     int   pBorderBri     // die or birth border brightness
                     ) {

  int drawCntMax = 4;

  // keep drawCntMax times calculation results
  // pCalcCicle * drawCntMax = Life Game calculation total times
  float[][] calcArrayWork = new float[drawCntMax][];
  calcArrayWork[0] = lifeGame(
                              pCalcCicle,
                              pBorderBri,
                              imgBrightArray.clone()
                              );
  for (int drawCnt = 1; drawCnt < drawCntMax; ++drawCnt) {
    calcArrayWork[drawCnt] = lifeGame(
                                      pCalcCicle,
                                      pBorderBri,
                                      calcArrayWork[drawCnt - 1]
                                      );
  }
      

  // draw wide area first, narrow area last
  for (int drawCnt = drawCntMax - 1; drawCnt >= 0 ; --drawCnt) {

    float briPow = map(drawCnt, drawCntMax - 1, 0, pBriPowMax, pBriPowMax / 4.0); 
    float satPow = map(drawCnt, drawCntMax - 1, 0, 10.0, 1.0); 
      
    PImage imgWork = imgInit.copy();

    for (int idxW = 1; idxW < imgWork.width - 1; ++idxW) {  
      for (int idxH = 1; idxH < imgWork.height - 1; ++idxH) {

        int idxPoint = idxH * imgWork.width + idxW;
        color cPoint = imgWork.pixels[idxPoint];

        float valHue = hue(cPoint);
        float valSat = saturation(cPoint);
        float valBri = brightness(cPoint);
        float valAlp = 100.0;

        if (calcArrayWork[drawCnt][idxPoint] == 0.0) {
          valBri = 0.0;
          valAlp = 0.0;
        } else if (calcArrayWork[drawCnt][idxPoint] == 200.0) {
          valBri = withinRange((1.0 + valBri / 10.0) * briPow);
          valSat = withinRange((1.0 + valSat /  4.0) * satPow);
        } else {
          valBri = withinRange((0.5 + valBri / 20.0) * briPow);
          valSat = withinRange((2.0 + valSat /  2.0) * satPow);
        }

        imgWork.pixels[idxPoint] = color(valHue, valSat, valBri, valAlp);

      }
    }

    imgWork.updatePixels();
    image(imgWork, 0.0, 0.0);

  }

}
 
/* ---------------------------------------------------------------------- */
float[] lifeGame(
                 int     pCalcCicle,  // returns after pCalcCicle times calculation
                 int     pBorder,     // die or birth border brightness
                 float[] pCalcArray   // calculation object
                 ) {
  
  float briDead = 0.0;
  float[] checkArray = pCalcArray.clone();
  float[] updateArray = pCalcArray.clone();
  int imgW = int(imgInit.width);
  int imgH = int(imgInit.height);
  
  for (int cicleNo = 0; cicleNo < pCalcCicle; ++cicleNo) {

    for (int idxW = 1; idxW < imgW - 1; ++idxW) {  
      for (int idxH = 1; idxH < imgH - 1; ++idxH) {

        int aliveNum = 0;
        int idxPoint = idxH * imgW + idxW;
        aliveNum += deadOrAlive(checkArray[idxPoint - 1], pBorder);
        aliveNum += deadOrAlive(checkArray[idxPoint + 1], pBorder);
        aliveNum += deadOrAlive(checkArray[idxPoint - imgW], pBorder);
        aliveNum += deadOrAlive(checkArray[idxPoint - imgW - 1], pBorder);
        aliveNum += deadOrAlive(checkArray[idxPoint - imgW + 1], pBorder);
        aliveNum += deadOrAlive(checkArray[idxPoint + imgW], pBorder);
        aliveNum += deadOrAlive(checkArray[idxPoint + imgW - 1], pBorder);
        aliveNum += deadOrAlive(checkArray[idxPoint + imgW + 1], pBorder);

        // birth
        if (aliveNum == 3) {
          updateArray[idxPoint] = 200.0;
        }
        // die
        if (aliveNum <= 1 || aliveNum >= 4) {
          updateArray[idxPoint] = 0.0;
        }
        // zonbie
        if (aliveNum == 2) {
          updateArray[idxPoint] = pBorder + map(random(1), 0, 1, -5, 5);
        }

      }
    }

    checkArray = updateArray.clone();
    
  }
  
  return updateArray;
  
}

/* ---------------------------------------------------------------------- */
int deadOrAlive(float pVal, float pBorder) {
  if (pVal > pBorder) {
    return 1;
  }
  return 0;
}

/* ---------------------------------------------------------------------- */
float withinRange(float val) {
  return max(0.0, min(100.0, val));
}

/* ---------------------------------------------------------------------- */
int getBorderBrightness(int borderRatio) {

  int[] briArray = new int[101]; // pixel count per brightness 0-100
  for (int i = 0; i < imgInit.pixels.length; ++i) {  
    ++briArray[int(brightness(imgInit.pixels[i]))];
  }

  // borderBrightness : life game dead or alive border brightness value
  int countPixels = 0;
  int noPixels = int(imgInit.width * imgInit.height);
  int borderBrightness = 0;
  for (borderBrightness = 100; borderBrightness > 0; --borderBrightness) {
    countPixels += briArray[borderBrightness];
    if (100 * countPixels / noPixels >= borderRatio) {
      break;
    }      
  }

  return borderBrightness;

}

/*
Copyright (C) 2018- deconbatch

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>
*/


 


Next Post Previous Post
1 Comments
  • Rashed Shaon
    Rashed Shaon Sunday, December 26, 2021

    The Photo Editing is a professional clipping path service company based in India, operated by dedicated and highly experienced professionals.

Add Comment
comment url