multi page PDF. / getting blank page every 5 pages

Wrote this little program that takes a master image (local), splits it into pages (because it's big), and reproduces it with tiles (taken from Flikr) based on averaged colors. On the back of each sheet is a page number, so we know ho to put them back order. All works great! very pleased! But for some reason, i get a blank page every 5 pages... why? Master image size is currently 4771 x 3181px

Also, this code probably needs some optimization, but that's another topic.

/**
 * This code is copyright (c) Mathias Markl 2016 (the imageLoader part)
 *
 * 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 2 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/>.
 */

import at.mukprojects.imageloader.*;
import at.mukprojects.imageloader.flickr.*;
import at.mukprojects.imageloader.image.*;

import processing.pdf.*;


PGraphicsPDF pdf;


ImageLoader loader;
ImageList list;
Image img;

String apiKey = "xxxx";
String apiSecret = "xxxx";

PImage master = new PImage();
PImage cover = new PImage();

int maxImages = 50;
int detail = 50;
int loaderOn = 1;
int extra;

PImage[] myImages = new PImage[50];

StringList imageSorter = new StringList(0);
int c=0;
int i=0;
int j =0;
int k =0;
int n =0;
int o=0;

//// Image Thingies

PImage baseImage;
int baseIndex=5;
int maxHeight=900;
int maxWidth=900;

int rounderY;
int rounderX;

IntList rTile = new IntList(0);
IntList gTile = new IntList(0);
IntList bTile = new IntList(0);
int rBase;
int gBase;
int bBase;
IntList cRVar = new IntList(0);
IntList cGVar = new IntList(0);
IntList cBVar = new IntList(0);
IntList cVar = new IntList(0);
IntList targetArray = new IntList(0);

int target;
int page=1;
PFont noPage;

void setup() {
  size(1000, 1000);

  loader = new FlickrLoader(this, apiKey, apiSecret);
  list = loader.start("sunset", false, 60*1000);

  master=loadImage("master.jpg");

  pdf = (PGraphicsPDF)beginRecord(PDF, "Lines.pdf");

  frameRate (60);
}

void draw() {


  if (loaderOn==1) {
    println("retrieved "+list.size()+" photos");
    //println(list.getIds());


    // Remove Duplicates

    if (list.size()==maxImages) {
      for (int b=0; b<maxImages; b++) {
        if (imageSorter.size()==0) {
          img = list.getImage(k);
          imageSorter.append(img.getImgUrl());
          k++;
        } else if (imageSorter.size()>0) {
          img = list.getImage(k);
          if (imageSorter.get(imageSorter.size()-1)==img.getImgUrl()) {
            k++;
          } else {
            imageSorter.append(img.getImgUrl());
            k++;
          }
        }
      }


      // Resizing all the funzies!

      println("LOADING IMAGES INTO PIMAGE ARRAY AND RESIZING");

      for (int i=0; i<imageSorter.size(); i++) {
        myImages[i]=loadImage(imageSorter.get(i), "jpg");
        myImages[i].resize(detail, detail);
      }


      // Tile Pixel Averaging Baby Boy

      println("LOADING AND AVERAGING TILE PIXEL COLORS");

      for (int i=0; i<imageSorter.size(); i++) {
        myImages[i].loadPixels();
        color r = 0, g = 0, b = 0;

        for (color c : myImages[i].pixels) {
          r += c >> 020 & 0xFF;
          g += c >> 010 & 0xFF;
          b += c        & 0xFF;
        }

        r /= myImages[i].pixels.length;
        g /= myImages[i].pixels.length;
        b /= myImages[i].pixels.length;

        rTile.append(r);
        gTile.append(g);
        bTile.append(b);
      }
      println("DONE");
      loader.stop();
      list=null;
      loaderOn=0;
    }
  }



  // Fonctions répetées à chaques page //


  if (loaderOn==0) {

    // Découpage du master en pages (imageBase)

    for (int v=0; v<master.height; v+=height) {
      for (int w=0; w<master.width; w+=width) {


        baseImage=master.get(v, w, width, height);


        // Découpage de la page en détails




        for (i=0; i<baseImage.width; i+=detail) {
          for (j=0; j<baseImage.height; j+=detail) {

            println("LOADING AND AVERAGING DETAIL COLORS"+" "+"DETAIL NO"+" "+(i+j));

            PImage newImg = baseImage.get(i, j, detail, detail);

            // Analyze de la couleur de chaque détail


            newImg.loadPixels();

            color rBase = 0, gBase = 0, bBase = 0;

            for (color c : newImg.pixels) {
              rBase += c >> 020 & 0xFF;
              gBase += c >> 010 & 0xFF;
              bBase += c        & 0xFF;
            }

            rBase /= newImg.pixels.length;
            gBase /= newImg.pixels.length;
            bBase /= newImg.pixels.length;

            if ((rBase+gBase+bBase)==0) {
              fill(255);
              noStroke();
              rect(i, j, detail, detail);
            } else {

              // Calcule de détail le plus près de l'image de base

              println("ANALYSING TILE COLOR PROXIMITY");


              for (k=0; k<maxImages; k++) {
                cRVar.append(abs(rBase-rTile.get(k)));

                cGVar.append(abs(gBase-gTile.get(k)));

                cBVar.append(abs(bBase-bTile.get(k)));

                cVar.append(cRVar.get(k)+cGVar.get(k)+cBVar.get(k));

                println("APPROX TILE NO"+k );
              }

              target = cVar.index(cVar.min());
              println("MATHCHING TILE="+target);
              image(myImages[target], i, j);   
              println("CLEARING ALL VARIABLES");

              c=o;

              k=0;
              n=o;
              o=0;

              cRVar.clear();
              cGVar.clear();
              cBVar.clear();
              cVar.clear();

              println("PRINTING TILE "+(i+j));
            }
          }
        }
        i=0;
        j=0;
        //baseImage.resize(0, 250);
        //image(baseImage, width-baseImage.width, height-baseImage.height);

        pdf.nextPage();

        PFont noPage = createFont("Helvetica", 100, true);
        textFont(noPage, 500);
        fill(225);
        textAlign(CENTER, CENTER);
        text(page, width/2, height/2);

        pdf.nextPage();
        page++;
      }
    }
    cover=master;

    if (cover.width>cover.height) {
      cover.resize(width-200, 0);
      rounderY = (cover.height%20);
      cover.resize(width-200, cover.height-rounderY);
    } else if (cover.width<cover.height) {
      cover.resize(0, height-200);
      rounderX = (cover.width%20);
      cover.resize(cover.width-rounderX, height-200);
    }
    PFont noPage = createFont("Helvetica", 100, true);
    textAlign(CENTER, CENTER);
    textFont(noPage, 20);
    text("Mosaïc Poster Maker", width/2, ((height-cover.height)/2)-100);
    text("Jonathan Mélançon", width/2, ((height+cover.height)/2)+100);
    imageMode(CENTER);
    image(cover, width/2, height/2);

    endRecord();
    println("PDF DONE!");
    exit();
  }
}

Answers

  • general tip:

    don't post your api keys on public forums.

  • oups...! will change it!

  • i've done it.

    it might be an idea to get rid of all the work in your program except the bit that writes the image, that way more people can reproduce what you've done without all the faff.

    in fact, if you use createImage(4771, 3181, RGB); then they don't need to even create an enormo image to use.

  • Yeah your are probably right. I left all the code there because i thought maybe the error was lying somewhere unexpected. I export a multi page .pdf rather than a big image because I want to be able to print it using a normal printer.

  • I don't know if the following post applies to you: https://forum.processing.org/two/discussion/18880/beginrecord-not-capturing-all-frames-in-display/p1

    I didn't go through you code. I would suggest either try to insert an empty page at the beginning or shift your images/page creating so they start in a second page. Does your skip page moves to the 6th page or stays in the fifth?

    If you PM your credentials I could try to run it and see if I could reproduce the error otherwise. What OS are you using? I am on Win 10.

    Kf

  • This might be worth testing here:

    for (i=0; i<baseImage.width; i+=detail) {
      for (j=0; j<baseImage.height; j+=detail) {
        PImage newImg = baseImage.get(i, j, detail, detail);
    

    You are hanging an arbitrarily wide sample area to the right and down from your sample point. What happens if detail isn't a factor of baseImage.width and you get to the end of each row?

  • @jeremydouglass. I get a page with only the last portion of the picture, as intended. But then, instead of getting the next full page, I get the blank page. will try to resize.

  • ok, i resized the master image so it is a duplicate of my page size. now I get twice as many blank pages... it seems to add one everytime it reaches the end of a row or a column...

Sign In or Register to comment.