# I would like to use the badwitch effect on the images in the "date" folder and export it to the "output" folder

**URL:** <https://discourse.processing.org/t/i-would-like-to-use-the-badwitch-effect-on-the-images-in-the-date-folder-and-export-it-to-the-output-folder/23698>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [September 5, 2020, 6:20pm UTC](https://discourse.processing.org/t/i-would-like-to-use-the-badwitch-effect-on-the-images-in-the-date-folder-and-export-it-to-the-output-folder/23698 "2020-09-05T18:20:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gatz\_gaz](https://avatars.discourse-cdn.com/v4/letter/g/bb73d2/32.png) [@gatz\_gaz](https://discourse.processing.org/u/gatz_gaz)\
**Post date:** [September 5, 2020, 6:20pm UTC](https://discourse.processing.org/t/i-would-like-to-use-the-badwitch-effect-on-the-images-in-the-date-folder-and-export-it-to-the-output-folder/23698/1 "2020-09-05T18:20:06Z")

</div>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

PImage foto;

int maxImages = 4; // Img in totale  
int imageIndex = 0; // Img iniziale

PImage images = new PImage[maxImages]; // Serie di Img  
// images = new PImage{image1, image2, image3};

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

foto=loadImage(“Data/nin1.jpg”);  
badwitch(foto.pixels);

for (int i = 0; i \< images.length; i ++ ) {  
images[i] = loadImage( “nin” + i + “.jpg” );

}  
frameRate(1);

}

void draw() {

image(images[imageIndex], 0, 0);  
imageIndex = (imageIndex + 1);  
saveFrame(“output/file\_####.png”);

}

void badwitch(int pixels) {  
for (int i=0; i\<pixels.length; i++) {  
int c=pixels[i];  
float b=(c&0xff)/255f;  
pixels[i]=color(255\*badwitch(b));  
}  
}

float badwitch(float u) {  
float invert=0.5;  
float r;  
if (u\<invert)  
r=map(u, 0, invert, 1, 0);  
else  
r=map(u, invert, 1, 0, 1);  
return r;  
}

---

<div class="post-metadata">

**Author:** ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)\
**Post date:** [September 6, 2020, 5:13am UTC](https://discourse.processing.org/t/i-would-like-to-use-the-badwitch-effect-on-the-images-in-the-date-folder-and-export-it-to-the-output-folder/23698/2 "2020-09-06T05:13:54Z")

</div>

Next time please format your code. It makes it so much easier to read and it improves chances to be answered.

Your quite close with your code and it needs a few replacement of code lines and a few new lines of code.

```auto
PImage foto;

int maxImages = 4; // Img in totale
int imageIndex = 0; // Img iniziale

PImage[] images = new PImage[maxImages]; // Serie di Img
//[] images = new PImage[]{image1, image2, image3};

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

  foto=loadImage("Data/nin1.jpg");

  for (int i = 0; i < images.length; i ++ ) {
    images[i] = loadImage( "nin" + i + ".jpg" );
  }
  //frameRate(1); //not needed unless you really want to see each image separately for a second
}

void draw() {
  for(PImage photo: images) {
    photo.loadPixels(); //To get access to pixel array 
    badwitch(photo.pixels);
    photo.updatePixels(); //stores changes made to pixel array
    image(photo, 0, 0);
    imageIndex = (imageIndex + 1);
    saveFrame("output/file_" + imageIndex + ".png");
  }
}

void badwitch(int[] pixels) {
  for (int i=0; i<pixels.length; i++) {
    int c=pixels[i];
    float b=(c&0xff)/255f;
    pixels[i]=color(255*badwitch(b));
  }
}

float badwitch(float u) {
  float invert=0.5;
  float r;
  if (u<invert)
    r=map(u, 0, invert, 1, 0);
  else
    r=map(u, invert, 1, 0, 1);
  return r;
}

```

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [September 6, 2020, 6:08am UTC](https://discourse.processing.org/t/i-would-like-to-use-the-badwitch-effect-on-the-images-in-the-date-folder-and-export-it-to-the-output-folder/23698/3 "2020-09-06T06:08:51Z")

</div>

> [@gatz\_gaz](#):
>
> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

I always wonder why so many users have this on the top of their post and then continue with an unformatted code.

---

<div class="post-metadata">

**Author:** ![gatz\_gaz](https://avatars.discourse-cdn.com/v4/letter/g/bb73d2/32.png) [@gatz\_gaz](https://discourse.processing.org/u/gatz_gaz)\
**Post date:** [September 6, 2020, 11:17am UTC](https://discourse.processing.org/t/i-would-like-to-use-the-badwitch-effect-on-the-images-in-the-date-folder-and-export-it-to-the-output-folder/23698/4 "2020-09-06T11:17:07Z")

</div>

Thank you, but, how can I stop at function at the fourth image?

---

<div class="post-metadata">

**Author:** ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)\
**Post date:** [September 6, 2020, 12:14pm UTC](https://discourse.processing.org/t/i-would-like-to-use-the-badwitch-effect-on-the-images-in-the-date-folder-and-export-it-to-the-output-folder/23698/5 "2020-09-06T12:14:30Z")

</div>

Well, it stops at last (fourth) image. If you want to end the program you can add `exit()` at the end of `draw()`.

```auto
for(PImage photo: images) {

```

is same thing as

```auto
for(int i = 0; i < images.length, i++) {
  PImage photo = images[i];

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [September 6, 2020, 5:14pm UTC](https://discourse.processing.org/t/i-would-like-to-use-the-badwitch-effect-on-the-images-in-the-date-folder-and-export-it-to-the-output-folder/23698/6 "2020-09-06T17:14:32Z")

</div>

wondered what badwitch effect is…

> **[Bad Witch](https://en.wikipedia.org/wiki/Bad_Witch)**
>
> Bad Witch is the ninth studio album by American industrial rock band Nine Inch Nails, released by The Null Corporation and Capitol Records on June 22, 2018. It is the last of a trilogy of releases, following their two previous EPs Not The Actual Events (2016) and Add Violence (2017). As with the previous releases in the trilogy, it was produced by frontman Trent Reznor and Atticus Ross, making it the band's first studio album since 2007's Year Zero to not be co-produced by the long-time collabo...

NIN

---

<div class="post-metadata">

**Author:** ![raron](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/raron/32/13651_2.png) [@raron](https://discourse.processing.org/u/raron)\
**Post date:** [September 6, 2020, 5:25pm UTC](https://discourse.processing.org/t/i-would-like-to-use-the-badwitch-effect-on-the-images-in-the-date-folder-and-export-it-to-the-output-folder/23698/7 "2020-09-06T17:25:23Z")

</div>

> [@gatz\_gaz](#):
>
> Thank you, but, how can I stop at function at the fourth image?

For code you don’t want repeated over and over again, (usually) better to put it in the setup() section. You may want to stopit with exit() or noLoop(). draw() section loops all the time.

EDIT: Partly what @SomeOne also said… (I should read pror posts better)
