Meowky Way

I tried to make Milky Way from cats photo with this Processing code.

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


I tried to make Milky Way from cats photo with this Processing code.

Image manipulation example 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 creative coding.

It's an image manipulation type creative coding work made with Processing.
I tried to make something with yet another way to apply Conway's Game of Life to photo images.
When I applied Conway's Game of Life to some photo images, I felt it seems like a starry sky.
So I tried to make Milky Way from cats photo.

blend(BLEND)
Apply Conway's Game of Life many cycles.
blend(SCREEN)
Apply Conway's Game of Life a few cycles.
Add some gas.

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

Processing example code.

Please feel free to use this code, if you like it.
To see another works based on my code is my pleasure. And my honor.


// Meowky Way.
// @author @deconbatch
// @version 0.2
// Processing 3.2.1
// 0.1 2018.01.30
// 0.2 2018.12.23

import java.util.Random;

PImage  imgInit;
float[] calcArray;
float   canvasW, canvasH;
int     borderBrightness;

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

  float baseWH      = 980;
  int   borderRatio = 25; // percentage

  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);
  canvasW = imgInit.width * rateSize;
  canvasH = imgInit.height * rateSize;
  imgInit.resize(int(canvasW), int(canvasH));
  imgInit.loadPixels();

  calcArray = new float[imgInit.pixels.length]; // for life game calculation
  int[] briArray = new int[101]; // pixel count per brightness 0-100
  for (int idxW = 1; idxW < imgInit.width - 1; ++idxW) {  
    for (int idxH = 1; idxH < imgInit.height - 1; ++idxH) {
      int idxPoint = idxH * imgInit.width + idxW;
      calcArray[idxPoint] = brightness(imgInit.pixels[idxPoint]);
      ++briArray[int(brightness(imgInit.pixels[idxPoint]))];
    }
  }

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

  background(0.0, 0.0, 0.0, 100.0);

}

void draw() {

  translate((width - canvasW) / 2, (height - canvasH) / 2);

  blendMode(BLEND);
  for (int briCnt = 1; briCnt <= 2; ++briCnt) {
    for (int cicleCnt = 1; cicleCnt <= 2; ++cicleCnt) {
      drawImagePixels(10, cicleCnt, ceil(borderBrightness / briCnt), 60.0, 20.0, 0.5, 3.0);
    }
  }

  blendMode(SCREEN);
  for (int briCnt = 1; briCnt <= 2; ++briCnt) {
    for (int cicleCnt = 1; cicleCnt <= 2; ++cicleCnt) {
      drawImagePixels(8, cicleCnt, ceil(borderBrightness / briCnt), 30.0, 10.0, 2.0, 1.0);
    }
  }

  addGas(0.002);
  addGas(0.005);
  addGas(0.008);

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

/* ---------------------------------------------------------------------- */
void addGas(float pNoiseDiv) {
  
  PImage imgWork     = imgInit.copy();
  float  noiseW      = random(100.0);
  float  noiseHStart = random(100.0);
  float  valBriMin   = borderBrightness * 3.0 / 4.0;
  float  valBriMax   = borderBrightness * 4.0 / 3.0;

  for (int idxW = 1; idxW < imgWork.width - 1; ++idxW) {
    float noiseH = noiseHStart;
    for (int idxH = 1; idxH < imgWork.height - 1; ++idxH) {
      int idxPoint = idxH * imgWork.width + idxW;
      color cPoint = imgWork.pixels[idxPoint];
      float valHue = (hue(cPoint) + 360 + gaussdist(0.0, 60.0, 30.0)) % 360.0;
      float valSat = saturation(cPoint);
      float valBri = brightness(cPoint);
      float valAlp = 100.0;

      if (valBri < valBriMax && valBri > valBriMin) {
        float ratioBri = abs(valBriMin + valBriMax - 2 * valBri) / (valBriMax - valBriMin);
        valBri = map(ratioBri, 0.0, 1.0, 100.0, 0.0) * noise(noiseW, noiseH) * abs(gaussdist(0.0, 1.0, 0.1));
        valSat = map(valSat, 0.0, 100.0, 0.0, 100.0) * noise(noiseH, noiseW);
        valAlp = 100.0;
      } else {
        valBri = 0.0;
        valAlp = 0.0;
      }
      imgWork.pixels[idxPoint] = color(valHue, valSat, valBri, valAlp);
      noiseH += pNoiseDiv;
    }
    noiseW += pNoiseDiv;
  }

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

}

/* ---------------------------------------------------------------------- */
void drawImagePixels(int pDrawCntMax, int pCicleCnt, int pBorderBrightness, float pBriAliveMax, float pBriAliveMin, float pSatAliveMax, float pSatAliveMin) {

  float[][] calcArrayWork = new float[pDrawCntMax][];
  calcArrayWork[0] = conwaysLifeGame(imgInit.width, imgInit.height, calcArray, ceil(pCicleCnt / 2), pBorderBrightness);
  calcArray = calcArrayWork[0].clone();

  for (int drawCnt = 1; drawCnt < pDrawCntMax; ++drawCnt) {
    calcArrayWork[drawCnt] = conwaysLifeGame(imgInit.width, imgInit.height, calcArrayWork[drawCnt - 1], ceil(pCicleCnt * pow(3 + drawCnt * 2, 2) / 1), pBorderBrightness);
  }

  for (int drawCnt = pDrawCntMax - 1; drawCnt >= 0 ; --drawCnt) {

    PImage imgWork = imgInit.copy();

    float briAlive = map(drawCnt, 0, pDrawCntMax - 1, pBriAliveMax, pBriAliveMin);
    float satBase = map(drawCnt, 0, pDrawCntMax - 1, pSatAliveMax, pSatAliveMin);

    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) + 360 + gaussdist(0.0, 30.0, 15.0)) % 360.0;
        float valSat = saturation(cPoint);
        float valBri = brightness(cPoint);
        float valAlp = 100.0;

        if (calcArrayWork[drawCnt][idxPoint] == 200.0) {
          valBri = map(valBri, 0.0, 100.0, briAlive / 5.0, briAlive);
          valSat *= satBase;
          valSat = withinRange(valSat);
          valAlp = 100.0;
        } else {
          valBri = 0.0;
          valAlp = 0.0;
        }

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

      }
    }

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

  }

}

/* ---------------------------------------------------------------------- */
float[] conwaysLifeGame(float pImgWidth, float pImgHeight, float[] pCalcArray, int pCicleNoMax, int pBorder) {

  float briDead = 0.0;
  float[] checkArray  = pCalcArray.clone();
  float[] updateArray = pCalcArray.clone();
  int imgW = int(pImgWidth);
  int imgH = int(pImgHeight);
  
  for (int cicleNo = 0; cicleNo < pCicleNoMax; ++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;
        }

      }
    }

    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));
}

/* ---------------------------------------------------------------------- */
float gaussdist(float pmean, float plimit, float pdevi) {
  /**
     Gaussian distribution
     1.parameters.
     pmean  : mean value
     plimit : max value of abs(deviation)
     ex. plimit >= 0
     pmean = 0.5, plimit = 0.5 -> return value = from 0.0 to 1.0
     pdevi  : standard deviation value
     ex. good value? -> pdevi = plimit / 2
     2.return.
     gaussian distribution
  **/

  if (plimit == 0) {
    return pmean;
  }

  Random obj_random = new Random();
  float gauss = (float) obj_random.nextGaussian() * pdevi;
  // not good idea
  if (abs(gauss) > plimit) {
    gauss = pow(plimit, 2) / gauss;
  }

  return pmean + gauss;
    
}

/*
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 
*/




Next Post Previous Post
No Comment
Add Comment
comment url