Loading...
Logo
Processing Forum

Image puzzling

in Programming Questions  •  2 years ago  
Hello,
I'm looknig for cutting an image into regularpieces. I figured I could have 2 loops going through the image and extract regular size defined pieces, then save them...

here's the challenge
a 500x500px picture cut in 10x10 pieces


size(500, 500);
background(255);
noStroke();
fill(0);

String ch = "fold";
createOutput(ch+"dummy");

PImage img = loadImage("decoup2.gif");
PImage crop;

 
for(int i = 0; i < img.width; i += 50) {

  for(int j = 0; j < img.height; j += 50) {
    crop = img.get( j, j, j+50, i+50); //location in the first round is top left corner, 50 pic large square
    crop.save(ch+"/"+"-"+i+"-"+j+".gif");

   
  }

}

cannot figure what's wrong... maybe there's some other and smarter way, with pixel arrays...?

Replies(5)

Re: Image puzzling

2 years ago
look at your arguments to "get" -- you have i and j swapped and j repeated and you need to specify the width and height, not offset+width or offset+height

Copy code
  1. crop = img.get( i, j, 50, 50);
will be better -- but also beware that unless the image evenly divides into chunks of 50 pixels you may be in for a surprise

Re: Image puzzling

2 years ago
of course i was replacing j (I just had corrected the code before posting, as my program was a little different)
...still the offset was the key

thank you

Re: Image puzzling

2 years ago
now I see that the pieces are saved...directed in the sketch folder
...it would be much better if I stored them in the data folder but

Copy code
  1.       crop.save("data/"+ch+"/"+iboucle+".gif");
returns an error

java.io.FileNotFoundException: /Users/guidamry/Documents/Processing/_8910_juill/data/IMG_5749.JPG/0.gif (Not a directory)

it is strange because if I dont try to store them in /data

and do simply
Copy code
  1. crop.save("ch+"/"+iboucle+".gif");
the storage in the sketchfolder works perfectly....

My problem consists in the following :

I try to test wether the previous files exist

Copy code
  1.     String ch;
        ch = LesmasquesM[indexmasqueM]+"/0.gif";   
        boolean exists = (new File(ch)).exists();
        if (exists) {
            // File or directory exists
            println("t bon car "+ch);
        } else {
            // File or directory does not exist
            println("tu vas te planter car "+ ch);
        }
but my files are not recognized

Uh! one big trouble! because my directory is created in the sketchfolder from a file named 001.jpg that exist in the data foler!!!
why is not seen at all?

Re: Image puzzling

2 years ago
" it is strange because if I dont try to store them in /data"
Yes you are, I can see "data/" in the line you give. But I see no connection between that line and the error message you show. It should be more like /Users/guidamry/Documents/Processing/_8910_juill/data/fold/0.gif instead, which might fail if 'fold' doesn't exist beforehand. Unless you again altered manually some information but not all. Try to be consistent if you want us to understand something...

crop.save("ch+"/"+iboucle+".gif");
This won't even compile...
BTW, avoid Gif format, it is outdated, just stick to PNG.

I don't understand the last part.

Re: Image puzzling

2 years ago
thanks for your attention, I'm sorry for not being clear enough, I'll try to sum up and explain better...

For the storage question : the main point is that if I had "data/" in the saving path, it causes an error (whereas if if I dont all files are saved and directories created directly in the sketchfolder)... so I guess I must create my folder (whatever the name is) in the first place

the second problem concerns the access : at some point I did create directoriesand stored files in them, but if I want to access them, (in the sketchfolder) I just cant . Furthermore, I did something symptomatic and convenient, by creating a directory and naming as the image thats been puzzled.

Now, I think I must build an index for the puzzled files, and store the sudir, under data... omg maybe the failure lies in naming the directory as the existing files, of course i wont be able to store any gif (or png) in a .jpeg