# Help with some loops and image processing

**URL:** <https://discourse.processing.org/t/help-with-some-loops-and-image-processing/7541>\
**Category:** Coding Questions\
**Created:** [January 15, 2019, 3:15pm UTC](https://discourse.processing.org/t/help-with-some-loops-and-image-processing/7541 "2019-01-15T15:15:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![annaschimpf](https://avatars.discourse-cdn.com/v4/letter/a/a698b9/32.png) [@annaschimpf](https://discourse.processing.org/u/annaschimpf)\
**Post date:** [January 15, 2019, 3:15pm UTC](https://discourse.processing.org/t/help-with-some-loops-and-image-processing/7541/1 "2019-01-15T15:15:26Z")

</div>

Hello there,  
I am pretty new to processing and what I am going to ask is probably a little stupid but I just can´t get on without help.  
I want to write a program to do the following:

- Take a lot of pictures
- “cut out” a stripe of pixels in the middle of every picture (the width of the stripe is the width of the later picture divided by the nomber of images)
- “pace” this stripe to another picture one after another starting at x,y=0 until the right border (wich should coincidate with the total width of all the stripes put together because of how i calculated the width of every stripe)
- show me that resulting picture.

I wrote the following code and get a ArrayOutofBounds error which turns into an Failed to initilize error in the line I put between **…** :

```auto
PImage[] originals;
PImage trans; //the result image
int stripewidth; //with of the stripes to cut out of every image
   int transx=0;//x location for translation image
        int transy=0;//y loction for translation image

void setup(){
size (1754,1240);
originals = new PImage[121];
trans =createImage(width,height,RGB);
stripewidth=width/originals.length;

for (int i=0;i<originals.length;i++){
  originals[i]=loadImage("tfm ("+(i+1)+").png");
}

}

void draw(){
  
loadPixels();
trans.loadPixels();

for (int i=0;i<originals.length;i++){ //repeat for all the images
  originals[i].loadPixels(); //initialize/load
  //get access to locations
 for (int x=860;x<860+stripewidth;x++){ //"cut out" a stripe of width stripewidth at x=860
   for (int y=0;y<height;y++){
     int loc =x+y*width;
     int transloc =transx+transy*width;
     println(transloc);
     **trans.pixels[transloc]=originals[i].pixels[loc];** //get pixel from original and put it into trans picture
      transy++;
   }
   transx++;
 }
}

updatePixels();
trans.updatePixels();

image(trans,0,0);

}

```

I guess there are more issues with this code, but I can´t work with it if I dont get any image and I cant get any image until I resolve the error that makes my program break down.  
The output I get is this:  
Could not run the sketch (Target VM failed to initialize).  
But right when it breaks down I can see that it sais ArrayIndexOutOfBoundException: 1539118

Would really appreciate some help, I am sure the problem is very stupid.  
Thanks a lot!

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 15, 2019, 4:43pm UTC](https://discourse.processing.org/t/help-with-some-loops-and-image-processing/7541/2 "2019-01-15T16:43:41Z")

</div>

i think that is all not that easy as you calc on the pixel level.

```auto
 for (int x=860;x<860+stripewidth;x++){ //"cut out" a stripe of width stripewidth at x=860
   for (int y=0;y<height;y++){
     int loc =x+y*width;

```

this double loop i think has big problems:  
-a- height and width can not be used here!  
as you must refer to the height and width of the loaded picture like  
originals[i].height  
originals[i].width

-b- after that is correct the inner loop reads one vertical line?  
starting from where? 860?  
first must check if the vertical stripe like 860 + 100 for x fits in the originals[i].width  
then the inner loop reads ( part of lines ) 860 … 960  
and the outer loop must read the next line  
from  
`originals[i].width*N + 860 ... originals[i].width*N + 960`

your loop fails as soon the combined loop hits the

> originals[i].length

( that is end of picture )

p.s.  
i think there is a way to copy a area out of a image  
so not need that pixel copy.

```auto
  int start = 200, stripe=50;
  image(img, 0, 0, stripe,height,start,0,start+stripe,img.height);

```

---

<div class="post-metadata">

**Author:** ![annaschimpf](https://avatars.discourse-cdn.com/v4/letter/a/a698b9/32.png) [@annaschimpf](https://discourse.processing.org/u/annaschimpf)\
**Post date:** [January 17, 2019, 7:14pm UTC](https://discourse.processing.org/t/help-with-some-loops-and-image-processing/7541/3 "2019-01-17T19:14:04Z")

</div>

Thank you so much for your answer!!  
Especially your tip in PS really helped me.  
figured it out in like 2 minutes, like this: 🙂

```auto
for (int i=0;i<originals.length;i++){ //repeat for all the images
  originals[i].loadPixels();
  trans.copy(originals[i],620,0,stripewidth,originals[i].height,transx,0,stripewidth,trans.height);
  transx+=stripewidth;
 }

```

Thanks!  
Anna
