shengpo / processing_snips

some simple and small examples of Processing for references in any time
3 stars 1 forks source link

aTileSaver.pde // question #1

Open hellopam opened 11 years ago

hellopam commented 11 years ago

hello shen

i'm trying to use your version of "aTilerSaver.pde" in my sketch but the image i'm getting at the end has the same size of the image i get if i use saveImage().

there's any way to define the size of the final image i want to have? in which place in the code can I do that?

despite my file is created with this name: 'Simple01268_4750x2850.png' the image has only 770x570 (the size of my canvas).

thanks for your time

shengpo commented 11 years ago

sorry for the late reply.

if you want to change the final image size, please modify the following piece of code in aTileSaverSimpleTest.pde

public void keyPressed() {  
    //change the last parameter to change the final image size !
    if (key=='t') tiler.init("Simple"+nf(frameCount, 5), 5);   
}  

when you run sketch aTileSaverSimpleTest, press 't' to get preview image and final image with huge resolution you can find them in data folder. for example: one is Simplexxxx_2000x1500.tga and the other one is Simplexxxx_preview.png

hope this helpful.

hellopam commented 11 years ago

thanks a lot! I got it (:

just another question... I'm trying to use the selecFolder() function in way to select the place in my computer where i want to save the created files (preview .png + .tga).

but, i'm not succeeding...

can you take a look at my code and tell me what am I doing wrong? ( http://pastebin.com/jt00jzaH )

it's really important to me to move forward in my project...

thanks for your time!

On 8 July 2013 07:32, shengpo notifications@github.com wrote:

sorry for the late reply.

if you want to change the final image size, please modify the following piece of code in aTileSaverSimpleTest.pde

public void keyPressed() { //change the last parameter to change the final image size ! if (key=='t') tiler.init("Simple"+nf(frameCount, 5), 5); }

when you run sketch aTileSaverSimpleTest, press 't' to get preview image and final image with huge resolution you can find them in data folder. for example: one is Simplexxxx_2000x1500.tga and the other one is Simplexxxx_preview.png

hope this helpful.

— Reply to this email directly or view it on GitHubhttps://github.com/shengpo/processing_snips/issues/1#issuecomment-20587831 .

shengpo commented 11 years ago

Hello hellopam,

sorry for the late reply, too busy recently.

I modified your code and upload it to here : http://pastebin.com/pKszduqm

I change your code and also add some comments please read them and hope you can get it why your code don't run in addition, I add a new key to trigger selectFolder() and add a new method in aTileSaver class to fit your need, that is init(String location, String _filename, int _num)

please check it!

hope it is helpful !

cheers,

shengpo commented 11 years ago

by the way,

I update the aTileSaverSimpleTest example code and aTileSaver class to support your need. please check it.

hellopam commented 11 years ago

thanks a lot. it worked like a charm! (:

thanks for being so helpful and for waste some of your time with this.

btw, do you have a portfolio online that i could take a look? i'm looking for a few 'creative coders/digital artists' for a possible collaboration.

cheers from portugal!

pedro.

On 15 July 2013 04:43, shengpo notifications@github.com wrote:

Hello hellopam,

sorry for the late reply, too busy recently.

I modified your code and upload it to here : http://pastebin.com/pKszduqm

I change your code and also add some comments please read them and hope you can get it why your code don't run in addition, I add a new key to trigger selectFolder() and add a new method in aTileSaver class to fit your need, that is init(String location, String _filename, int _num)

please check it!

hope it is helpful !

cheers,

— Reply to this email directly or view it on GitHubhttps://github.com/shengpo/processing_snips/issues/1#issuecomment-20950648 .

shengpo commented 11 years ago

hello hellopam,

I am happy for helping you to work out!

you can find some info about me at here : http://shengpo.github.io/ but it is a little out of date (always no time to update it) and some info written in chinese.

cheers,

shengpo#

hellopam commented 11 years ago

hey sheng

sorry for bothering you again. but for the last days i've been trying to use your example in way to print a colored object in a transparent background.

i've tried to remove the statement that gives color to the background but once i've done it, the creation of the .tga file behaves in a strange way... it outputs a tiled image and not my full canvas..

do you have any tip about what should i do?

thanks in advance

On 18 July 2013 03:07, shengpo notifications@github.com wrote:

hello hellopam,

I am happy for helping you to work out!

you can find some info about me at here : http://shengpo.github.io/ but it is a little out of date (always no time to update it) and some info written in chinese.

cheers,

shengpo#

— Reply to this email directly or view it on GitHubhttps://github.com/shengpo/processing_snips/issues/1#issuecomment-21158383 .

shengpo commented 11 years ago

Hello hellopam,

not very clear about your question. could you please show me your wrong result image and your predicted image? or source code?

that will be helpful to figure out :)

shengpo#

2013/9/2 hellopam notifications@github.com

hey sheng

sorry for bothering you again. but for the last days i've been trying to use your example in way to print a colored object in a transparent background.

i've tried to remove the statement that gives color to the background but once i've done it, the creation of the .tga file behaves in a strange way... it outputs a tiled image and not my full canvas..

do you have any tip about what should i do?

thanks in advance

On 18 July 2013 03:07, shengpo notifications@github.com wrote:

hello hellopam,

I am happy for helping you to work out!

you can find some info about me at here : http://shengpo.github.io/ but it is a little out of date (always no time to update it) and some info written in chinese.

cheers,

shengpo#

— Reply to this email directly or view it on GitHub< https://github.com/shengpo/processing_snips/issues/1#issuecomment-21158383>

.

— Reply to this email directly or view it on GitHubhttps://github.com/shengpo/processing_snips/issues/1#issuecomment-23632522 .

hellopam commented 11 years ago

basically i'm just using the source code you have here ( https://github.com/shengpo/processing_snips/tree/master/aTileSaverSimpleTest) and the only changes that i've done was in the draw function.

you can see the final result in attachment.. but what i wanted was to only have a square in the center of the canvas and a transparent background...

thanks!

########## THE CODE: #############

aTileSaver tiler;

void setup() { size(500, 500, P3D); noStroke(); tiler=new aTileSaver(this); tiler.doSavePreview = false; //not to save preview image (default will save preview image) }

public void draw() {

// Saves tiled imaged when 't' is pressed public void keyPressed() { //make the 1st parameter as empty String to save images to sketch's data directory if (key == 't') tiler.init("", "Simple"+nf(frameCount, 5), 5);

//define your location path
if (key == 's') {
    tiler.init("/home/shengpo", "Simple"+nf(frameCount, 5), 5);

//save to the place you want //tiler.init("/home/shengpo/", "Simple"+nf(frameCount, 5), 5); //last '/' in the location parameter can be added or not }

//use dialog to select the location path
if(key == 'f'){
    selectFolder("Select a folder to process:", "folderSelected");
    //known problem: tiler.doSavePreview = true; will produce wrong

preview image } }

void folderSelected(File selection) { if(selection == null){ println("Window was closed or the user hit cancel."); }else{ println("User selected " + selection.getAbsolutePath());

    String location = selection.getAbsolutePath();
    tiler.init(location, "Simple"+nf(frameCount, 5), 5);    //save to

the place you want } }

class aTileSaver { public boolean isTiling=false, done=true; public boolean doSavePreview=true;

PApplet p;
float FOV=60; // initial field of view
float cameraZ, width, height;
int tileNum=10, tileNumSq; // number of tiles
int tileImgCnt, tileX, tileY;
boolean firstFrame=false, secondFrame=false;
String tileFilename, tileFileExt=".tga";
//String tileFilename, tileFileExt=".png";
PImage tileImg;
float perc, percMilestone;

// The constructor takes a PApplet reference to your sketch.
public aTileSaver(PApplet _p) {
    p=_p;
}

// If init() is called without specifying number of tiles,

getMaxTiles() // will be called to estimate number of tiles according to free memory.

public void init(String _filename) {
    init("", _filename, getMaxTiles(p.width));
}

// Initialize using a filename to output to and number of tiles to use.

public void init(String location, String _filename, int _num) {
    tileFilename=_filename;
    tileNum=_num;
    tileNumSq=(tileNum*tileNum);

    width=p.width;
    height=p.height;
    cameraZ=(height/2.0f)/p.tan(p.PI*FOV/360.0f);
    p.println("aTileSaver: "+tileNumSq+" tiles and Resolution:

"+(p.width_tileNum)+"x"+(p.height_tileNum));

    if (!new java.io.File(tileFilename).isAbsolute()){
        if(location.equals("") || location==null){
            tileFilename=p.dataPath(tileFilename);
        }else{
            if(location.lastIndexOf("/") == location.length()-1){
                tileFilename = location + tileFilename;
            }else{
                tileFilename = location + "/" + tileFilename;
            }
        }
    }

    // remove extension from filename
    tileFilename=noExt(tileFilename);
    p.createPath(tileFilename);

    // save preview
    if (doSavePreview) p.g.save(tileFilename+"_preview.png");

    // set up off-screen buffer for saving tiled images
    tileImg=new PImage(p.width*tileNum, p.height*tileNum);

    // start tiling
    isTiling=true;
    firstFrame=true;          //for skip first frame after do tiling
    secondFrame=true;     //for skip second frame after do tiling
    tileImgCnt=0;
    tileX = 0;
    tileY = 0;

    done=false;
    perc=0;
    percMilestone=0;
}

// set filetype, default is TGA. pass a valid image extension as

parameter. public void setSaveType(String extension) { tileFileExt=extension; if (tileFileExt.indexOf(".")==-1) tileFileExt="."+tileFileExt; }

// pre() handles initialization of each frame.
// It should be called in draw() before any drawing occurs.
public void pre() {
    if (!isTiling) return;
    if (firstFrame) {
        firstFrame=false;
    } else if (secondFrame) {
        secondFrame=false;
    }
    setupCamera();
}

// post() handles tile update and image saving.
// It should be called at the very end of draw(), after any drawing.
public void post() {
    // If first or second frame, don't update or save.
    if (firstFrame || secondFrame || (!isTiling)) return;

    // Get current image from sketch and draw it into buffer
    p.loadPixels();
    tileImg.set(tileX*p.width, tileY*p.height, p.g);

    // Increment tile index
    tileImgCnt++;
    perc=100*((float)tileImgCnt/(float)tileNumSq);
    if (perc-percMilestone> 5 || perc>99) {
        p.println(p.nf(perc, 3, 2)+"% completed.

"+tileImgCnt+"/"+tileNumSq+" images saved."); percMilestone=perc; }

    if (tileImgCnt==tileNumSq) tileFinish();
    else tileInc();
}

public boolean checkStatus() {
    return isTiling;
}

// tileFinish() handles saving of the tiled image
public void tileFinish() {
    isTiling=false;

    restoreCamera();

    // save large image to TGA

tileFilename+="_"+(p.width_tileNum)+"x"+(p.height_tileNum)+tileFileExt; p.println("Save: "+tileFilename.substring(tileFilename.lastIndexOf(java.io.File.separator)+1));

    tileImg.save(tileFilename);
    p.println("Done tiling.n");

    // clear buffer for garbage collection
    tileImg=null;
    done=true;
}

// Increment tile coordinates
public void tileInc() {
    if (tileX==tileNum-1) {
        tileX=0;
        tileY=(tileY+1)%tileNum;
    }
    else {
        tileX++;
    }
}

// set up camera correctly for the current tile
//caution: camera's coordination system si different from drawing's

coordination system // so, must be very care of parameters in camera() and frustum() !! public void setupCamera() { //reset to default camera coordination with Y-UP (up-increase), and original poit is at (width/2f, height/2f, 0) p.camera(width/2.0f, height/2.0f, cameraZ, width/2.0f, height/2.0f, 0, 0, 1, 0);

    if (isTiling) {
        //set to a specific perspective (or clipping volume) with

enough near near-plane and enough far far-plane //this step is to fix the viewport on each tile float mod=1f/10f; //to make near plane is close to camera(aka eye) p.frustum((-width/2f + width_((float)tileX/(float)tileNum))mod, //left (-width/2f + width((tileX+1)/(float)tileNum))mod, //right (height/2f - height((tileY+1)/(float)tileNum))mod, //bottom (height/2f - height((float)tileY/(float)tileNum))_mod, //up cameraZ*mod, 10000); //near, far } }

// restore camera once tiling is done
public void restoreCamera() {
    //reset to default camera coordination with Y-UP (up-increase), and

original poit is at (width/2f, height/2f, 0) p.camera(width/2.0f, height/2.0f, cameraZ, width/2.0f, height/2.0f, 0, 0, 1, 0);

    //reset to default perspective (or clipping volume) with enough

near near-plane and enough far far-plane float mod=1f/10f; //to make near plane is close to camera(aka eye) p.frustum(-(width/2)_mod, (width/2)_mod, //left, right -(height/2)_mod, (height/2)_mod, //bottom, up cameraZ*mod, 10000); //near, far }

// checks free memory and gives a suggestion for maximum tile
// resolution. It should work well in most cases, I've been able
// to generate 20k x 20k pixel images with 1.5 GB RAM allocated.
public int getMaxTiles(int width) {
    // get an instance of java.lang.Runtime, force garbage collection
    java.lang.Runtime runtime=java.lang.Runtime.getRuntime();
    runtime.gc();

    // calculate free memory for ARGB (4 byte) data, giving some slack
    // to out of memory crashes.
    int

num=(int)(Math.sqrt((float)(runtime.freeMemory()/4)*0.925f))/width;

p.println(((float)runtime.freeMemory()/(1024_1024))+"/"+((float)runtime.totalMemory()/(1024_1024)));

    // warn if low memory
    if (num==1) {
        p.println("Memory is low. Consider increasing memory.");
        num=2;
    }

    return num;
}

// strip extension from filename
String noExt(String name) {
    int last=name.lastIndexOf(".");
    if (last>0)
        return name.substring(0, last);

    return name;
}

}

On 2 September 2013 03:47, shengpo notifications@github.com wrote:

Hello hellopam,

not very clear about your question. could you please show me your wrong result image and your predicted image? or source code?

that will be helpful to figure out :)

shengpo#

2013/9/2 hellopam notifications@github.com

hey sheng

sorry for bothering you again. but for the last days i've been trying to use your example in way to print a colored object in a transparent background.

i've tried to remove the statement that gives color to the background but once i've done it, the creation of the .tga file behaves in a strange way... it outputs a tiled image and not my full canvas..

do you have any tip about what should i do?

thanks in advance

On 18 July 2013 03:07, shengpo notifications@github.com wrote:

hello hellopam,

I am happy for helping you to work out!

you can find some info about me at here : http://shengpo.github.io/ but it is a little out of date (always no time to update it) and some info written in chinese.

cheers,

shengpo#

— Reply to this email directly or view it on GitHub<

https://github.com/shengpo/processing_snips/issues/1#issuecomment-21158383>

.

— Reply to this email directly or view it on GitHub< https://github.com/shengpo/processing_snips/issues/1#issuecomment-23632522>

.

— Reply to this email directly or view it on GitHubhttps://github.com/shengpo/processing_snips/issues/1#issuecomment-23638726 .

shengpo commented 11 years ago

Hello hellopam,

in Processing, the default background color is gray when you do not use background() please try this sample code:

void setup(){
    size(200, 200);
}

void draw(){
    fill(255, 0, 0);
    rect(100, 100, 50, 50);
}

if you want to get a transparent background, you have to use PGraphics just like this:

PGraphics pg;

void setup() {
  size(200, 200);
  pg = createGraphics(width, height);
}

void draw() {
  pg.beginDraw();
  //pg.background(0);    //don't use background() to make transparent background
  pg.stroke(255);
  pg.strokeWeight(20);
  pg.line(20, 20, 180, 180);
  pg.endDraw();

  image(pg, 0, 0); 

  //save PGraphics's image
  pg.save("transparent.png");    //this file will be saved in sketch folder
  exit();   //exit program
}

so, in aTileSaverSimpleTest sketch the easiest way to get transparent background is to keep using background() in draw() for example: background(255) and then, using Image Editor application, such as GIMP, to convert image's background to transparent (reference video: http://www.youtube.com/watch?v=AC5vdKuwTp0)

hope this is helpful :)

shengpo#