# Match image to data, sequentially

**URL:** https://discourse.processing.org/t/match-image-to-data-sequentially/6158
**Category:** Coding Questions
**Created:** [December 2, 2018, 1:42am UTC](https://discourse.processing.org/t/match-image-to-data-sequentially/6158 "2018-12-02T01:42:26Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![karinalopez87](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/karinalopez87/32/2603_2.png) [@karinalopez87](https://discourse.processing.org/u/karinalopez87)
#### Post date: [December 2, 2018, 1:42am UTC](https://discourse.processing.org/t/match-image-to-data-sequentially/6158/1 "2018-12-02T01:42:26Z")

</div>

Hello everyone,

I have an array of numbers from my txt file,  
e.g. line[0] = img[0], line[1] = img[1], …

it seems to read all of the images but it only saves one. the code doesn’t give me an error yet it’s not saving multiple images even though when i type println(currentImage) it shows me all of the numbers corresponding to the new array of images. Can someone help me figure out what I’m doing wrong??

p.s. the code below is only addressing where the issue falls, the rest is the functions that are being called.

```auto
import java.util.Date;

String[] lines;
int index = 0;

PImage img;
int currentImage = 0;

int mode = 0;

int loops = 1;

int blackValue;
int brigthnessValue;
int whiteValue;

int row = 0;
int column = 0;

void setup() {
  size(200, 200);
  //frameRate(30);

  lines = loadStrings("test.txt");
  String path = sketchPath() + "/draft";

  //println("Listing all filenames in a directory: ");
  String[] filenames = listFileNames(path);
  //printArray(filenames);

  StringList newfilenames = new StringList();
  for (int i = 0; i < filenames.length; i++) {
    if (filenames[i].contains(".png")) {
      newfilenames.push(filenames[i]);
    }
  }

  for (int i = 0; i < lines.length; i++) {

    // Load the new image
    //printArray(path + "/" + newfilenames.get(currentImage));
    img = loadImage(path + "/" + newfilenames.get(currentImage));

      float fftBands = float(lines[i]);

      float imgAlpha = map(fftBands, 10, 2000, 500000, -10000000); //frequency w/minim lib
      int blackAlpha = floor(imgAlpha);
      image(img, 0,0);

      if (mode == 0) {
        blackValue = blackAlpha;
      }
    

    row = 0;
    column = 0;

    while (column < width-1) {
      img.loadPixels(); 
      sortColumn();
      column++;
      img.updatePixels();
    }

    while (row < height-1) {
      img.loadPixels(); 
      sortRow();
      row++;
      img.updatePixels();
    }
    // Save the image
    img.save("test-pngs/draft000-####.png");
    currentImage++;
  }
}

```

---

<div class="post-metadata">

### Author: ![Whahahahaha](https://avatars.discourse-cdn.com/v4/letter/w/3be4f8/32.png) [@Whahahahaha](https://discourse.processing.org/u/Whahahahaha)
#### Post date: [December 3, 2018, 9:54am UTC](https://discourse.processing.org/t/match-image-to-data-sequentially/6158/2 "2018-12-03T09:54:25Z")

</div>

@karinalopez87 I don’t have much experience with saving images like this, but i would assume

> img.save(“test-pngs/draft000-####.png”);

would just overwrite the image all the time since it’s just one name.  
you could use “i” as an identifier to save it as seperate images like:

```auto
img.save("test-pngs/draft000-####" + i + ".png");

```

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [December 6, 2018, 11:57pm UTC](https://discourse.processing.org/t/match-image-to-data-sequentially/6158/3 "2018-12-06T23:57:26Z")

</div>

It seems like a key issue here is the hash-for-numbers scheme that is supported for `saveFrame()`…

> You can specify the name of the sequence with the **filename** parameter, including hash marks (####), which will be replaced by the current **frameCount** value. (The number of hash marks is used to determine how many digits to include in the file names.) [saveFrame() / Reference / Processing.org](https://processing.org/reference/saveFrame_.html)

…and whether that is also supported by PImage.save()

> **[save() / Reference](https://processing.org/reference/PImage_save_.html)**
>
> Saves the image into a file. Append a file extension to the name of the file, to indicate the file format to be used: either TIFF (.tif), TARGA (.tga), JPEG (.jpg), or PNG (.png). If no extension is i…

To program the same behavior yourself you could combine `frameCount` with `nf()`.

> **[nf() / Reference](https://processing.org/reference/nf_.html)**
>
> Utility function for formatting numbers into strings. There are two versions: one for formatting floats, and one for formatting ints. The values for the digits and right parameters shoul…

…so

```auto
String frameNumber = nf(frameCount, 4); // same as ####
img.save("test-pngs/draft000-" + frameNumber + ".png");

```

---

<div class="post-metadata">

### Author: ![karinalopez87](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/karinalopez87/32/2603_2.png) [@karinalopez87](https://discourse.processing.org/u/karinalopez87)
#### Post date: [December 8, 2018, 5:36pm UTC](https://discourse.processing.org/t/match-image-to-data-sequentially/6158/4 "2018-12-08T17:36:47Z")

</div>

Perfect, yes, this seemed to work. Thank you!!
