0

I made a method that saves a PImage into a file (.png, .jpg, etc...), but if the file exceeds a certain size [not size on disk, I mean the dimensions], the program will repeat over and over again.

I post the method and a lighter version of the sketch.

Sketch:

void setup(){
  noLoop();
  size(300, 300);
}

void draw(){
  background(0);
  text("Hello world!", 15, 15);
  PImage toEdit = loadImage("C:\\Users\\ingrossod\\Desktop\\toEdit.png");
  PImage toSave = change(toEdit, color(86, 177, 222), color(239, 254, 255));
  save(toSave, "C:\\Users\\ingrossod\\Desktop\\edited"); // The file extension is missing
}

Now, PImage change(PImage, color, color) takes a PImage and substitutes a color with another.

Here is void save(PImage, String):

void save(PImage toSave, String fileName){
  fileName += ".png"; // Adding the extension

  // Saving the current frame
  int oldWidth = width;
  int oldHeight = height;
  saveFrame(fileName);
  PImage oldFrame = loadImage(fileName);

  // Modifyng the current frame with the image I want to save
  size(toSave.width, toSave.height);
  background(toSave);
  saveFrame(fileName);

  // Restoring the frame with old parameters
  size(oldWidth, oldHeight);
  background(oldFrame);
}

I already tried modifying frameRate value, putting everything in one method calling it... Nothing.

The program WORKS with small images. Any idea on how to solve it?

ingroxd
  • 995
  • 1
  • 12
  • 28
  • Where is this loop you're talking about? – musical_coder Oct 12 '13 at 17:17
  • I didn't put a loop, this is why I can't get why it does it... I can easily say that it loops because instead of comments in my sketch I print something in the applet of the IDE. – ingroxd Oct 12 '13 at 17:20

2 Answers2

1

Are you sure that infinite loop is not from change function? You do not provide us with its implementation.

My advice:

  1. use image(toSave, 0, 0) instead of background()
  2. use save(fileName) instead of saveFrame()
Majlik
  • 1,082
  • 1
  • 10
  • 20
  • I noticed that all works well if I put any other instruction that modify the frame screen, such as a `text(String, int, int)`. So I am sure the problem is in `saveFrame(String)`. Anyway, with this "workaround" I made it works, thanks a lot! – ingroxd Oct 23 '13 at 14:01
0

You've put noLoop in void setup, which prevents void draw continuing after frame 1. So frameRate is going to have precisely no effect..

geotheory
  • 22,624
  • 29
  • 119
  • 196
  • What have you set to 1? – geotheory Oct 12 '13 at 17:44
  • But `noLoop` stops `draw` from looping. You can't start it again by setting `frameRate`. – geotheory Oct 12 '13 at 17:47
  • Oh Gosh... I am not trying to repeat it! I am trying to stop the loop! Please read **carefully** the question! – ingroxd Oct 12 '13 at 17:49
  • Then your use of frameRate looks incorrect, because that changes the rate of the draw loop. So if draw is not supposed to loop.. (a) why are you using frameRate and (b) why not just put everything in setup? Also the purpose of your program is not exactly clear as it has not been explained.. – geotheory Oct 12 '13 at 18:08
  • [a] I am not using frameRate, and if you **read** the **entire** post you won't see any `void frameRate(int)`! [b] I can't put everything in the setup because I have something else before saving the image; [c] if you **read CAREFULLY** the **ENTIRE** post, I explained how I want to load, modify and save an image as png. Please, before another comment, **read** the question! – ingroxd Oct 12 '13 at 21:42
  • Please stop **typing in bold** and **UPPERCASE**. I'm trying to help, and I have fully read your post but your description of your problem is confusing and vague at best, contradictory at worst. Also [your example is not reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). e.g. `change()` is not defined. If you make a smaller reproducible example of your problem, it is much much easier for people to help. – geotheory Oct 13 '13 at 09:05
  • The program works, you can also erase the line where I use `change()` method, it isn't necessary here. The issue come out only with big images. Anyway, I found that if I add any frame variation [such as `print(String, int, int)`] it works well... Now all I wonder is why the sketch present this behavior! – ingroxd Oct 13 '13 at 12:39