Randomly splicing together parts of two images
in
Programming Questions
•
7 months ago
Hi- This is my first time posting to this forum for help. I am working on this sketch as part of a larger interactive project; this is, I guess, phase one. I am trying to create a randomized grid that allows me to dynamically re-arrange photographs. Right now, I am loading one half of the sketch with pieces of one image, and one half with pieces from the other.
But, I want the integration of the two images to be more randomized, and I'm not sure how to do that. I would also like to be able to control what percentage of the mix comes from one image (right now it is essentially a 50/50 mix). Any suggestions?
Thanks.
- PImage img1;
- PImage img2;
- int yunits = 50;
- int xunits = 33;
- int ypixelunits;
- int xpixelunits;
- void setup() {
- size(500, 334);
- ypixelunits = width/yunits;
- xpixelunits = height/xunits;
- img2 = loadImage("img_04.jpg");
- img1 = loadImage("img_03.jpg");
- //frameRate(5);
- noLoop();
- }
- void draw() {
- int rows = int(random(2, 15));
- int[] rowPositions = new int[rows];
- //calculate the heights, not the y-coordinates
- int lastHeight = 0;
- int ycount = 0;
- int totalHeight = 0;
- for (int y = 0; y < height; y += lastHeight) {
- if (ycount == rows-1) {
- rowPositions[ycount] = height - totalHeight;
- lastHeight = rowPositions[ycount];
- }
- else {
- int yelement = int(random(1, ((yunits-rows-ycount)/6)));
- rowPositions[ycount] = yelement * ypixelunits;
- lastHeight = rowPositions[ycount];
- totalHeight += lastHeight;
- }
- ycount++;
- }
- int ypos = 0;
- int xpos = 0;
- for (int i = 0; i < rowPositions.length; i++) {
- int columns = 2;
- int[] columnPositions = new int[columns];
- int lastWidth = 0;
- int xcount = 0;
- int totalWidth = 0;
- for (int x = 0; x < width; x += lastWidth) {
- if (xcount == columns-1) {
- columnPositions[xcount] = width - totalWidth;
- lastWidth = columnPositions[xcount];
- }
- else {
- int xelement = int(random(1, (xunits-columns-xcount)));
- columnPositions[xcount] = xelement * xpixelunits;
- lastWidth = columnPositions[xcount];
- totalWidth += lastWidth;
- }
- xcount++;
- }
- int xCrop = 0;
- for (int j = 0; j < columnPositions.length; j++) {
- PImage crop1;
- PImage crop2;
- int thisX = width-columnPositions[j];
- if (j == columnPositions.length-1) {
- thisX = 0;
- }
- if ((j%2) == 0) {
- //even
- crop1 = img1.get(xCrop, ypos, columnPositions[j], rowPositions[i]);
- image(crop1, thisX, ypos, columnPositions[j], rowPositions[i]);
- g.removeCache(crop1);
- }
- else {
- //not even
- crop2 = img2.get(xCrop, ypos, columnPositions[j], rowPositions[i]);
- image(crop2, thisX, ypos, columnPositions[j], rowPositions[i]);
- g.removeCache(crop2);
- }
- xCrop += columnPositions[j];
- }
- ypos += rowPositions[i];
- }
- }
- void mousePressed() {
- redraw();
- }
1