|
Author |
Topic: save a large image as multiples files sliceClass (Read 676 times) |
|
mKoser
|
save a large image as multiples files sliceClass
« on: Mar 12th, 2004, 1:42am » |
|
hey, since some people (including myself) are using processing to generate printed graphics i wrote a small class that lets you save LARGE images (say a 5000x5000 pixels) into a number of smaller files: suddenly (when starting to write this post) i realized that the reason why i was writing this code, was that i some time ago made a project where i could not use the save-function... or it didn't work outside the screen... or something - damn, not actually sure why this code should be usefull!!! hmm! anyway, now it's made, i hope someone finds a use for it. + mikkel Code: // Image slicing class for saving larger // than the screen sized images into // smaller images. // // March 12th 2004 // Mikkel Crone Koser // www.beyondthree.com // // currently this only saves as .tiff files, // but maybe an implementation of Martin // and Konie's JPEG, PNG... etc. saving // method could be added. // create a variable to hold the SegmentSaver object SegmentSaver imgSegSaver; // change this to affect the size of // the canvas and the size of the simple // graphics being drawn int dim = 500; void setup(){ size(dim*2, dim*2); // when initiating the SegmentSaver object // you tell it how many BImage slices you // want your canvas sliced into ... this // example is 4 by 4 imgSegSaver = new SegmentSaver(4, 4); } void loop(){ } void mousePressed(){ // draw something on the canves fill(255, 0, 0); rect(0, 0, dim, dim); line(0, 0, dim, dim); line(0, dim, dim, 0); fill(0, 255, 0); rect(dim, 0, dim, dim); line(dim, 0, dim*2, dim); line(dim, dim, dim*2, 0); fill(0, 0, 255); rect(0, dim, dim, dim); line(0, dim, dim, dim*2); line(0, dim*2, dim, dim); fill(255, 255, 0); rect(dim, dim, dim, dim); line(dim, dim, dim*2, dim*2); line(dim, dim*2, dim*2, dim); } void keyPressed(){ // make the SegmentSaver object do all the saving stuff imgSegSaver.saveSegments(); } class SegmentSaver{ int xNum, yNum, h, w; BImage[] segments; SegmentSaver(int _xNum, int _yNum){ xNum = Math.max(_xNum, 1); yNum = Math.max(_yNum, 1); h = height/yNum; w = width/xNum; initSegments(); } void initSegments(){ segments = new BImage[xNum * yNum]; println("BImage segments created ("+xNum+"x"+yNum+"): "+segments.length); for(int y=0; y<yNum; y++){ for(int x=0; x<xNum; x++){ segments[(y*xNum)+x] = new BImage(w, h); } } } void saveSegments(){ for(int y=0; y<yNum; y++){ for(int x=0; x<xNum; x++){ segments[(y*xNum)+x].replicate(copy(),x*w,y*h,(x*w)+w,(y*h)+h,0,0,w,h); // change this string if you want to save // the file using a different name, or in // a different folder. String filename = "segment_" + ((y*xNum)+x) + ".tif"; segments[(y*xNum)+x].save(filename); println(filename + " is saved!"); } } } } |
|
|
« Last Edit: Mar 12th, 2004, 1:42am by mKoser » |
|
mikkel crone koser | www.beyondthree.com | http://processing.beyondthree.com
|
|
|
|