We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello, this is my second post here, so sorry if it's not relevant in a way or another...
So here is my first code, I hope it's not too big. The purpose is to let user choose an image, do a small visual change of the image and then save the result as a .jpg file.
So I have first a question regarding the general way it should work, as for now it shows some kind of shimmering image, which is normal because the modification of the image take part in the draw() part of the code. So my question is how could I do the same in order to have this modification applied only once and not as fast as possible as it's the case now?
Here's my code:
PImage loadedImgage;
String selectedPath = "";
void setup() {
size(600, 600);
selectInput("Select a file to process:", "fileSelected");
}
void fileSelected(File selection) {
selectedPath = selection.getAbsolutePath();
}
String getFileName(String completeFileName) {
String[] fileNameParts = split(completeFileName, ".");
fileNameParts = shorten(fileNameParts);
return join(fileNameParts, ".");
}
float randomizeVal(float value, float minVal, float maxVal, float delta) {
if(value < minVal | value > maxVal) {
return 0;
}
float returnVal = value;
//Add or substract delta (random boolean simulation)
if(random(1) < 0.5) {
returnVal = returnVal + random(delta);
if(returnVal > maxVal) {
returnVal = maxVal;
}
}
else {
returnVal = returnVal - random(delta);
if(returnVal < minVal) {
returnVal = minVal;
}
}
return returnVal;
}
color changeColor(color originalColor) {
float delta = 30;
float redVal = randomizeVal(red(originalColor), 0, 255, delta);
float greenVal = randomizeVal(green(originalColor), 0, 255, delta);
float blueVal = randomizeVal(blue(originalColor), 0, 255, delta);
return color(redVal, greenVal, blueVal);
}
void draw() {
//While no path has been selected, don't do anything
if(selectedPath != ""){
loadedImgage = loadImage(selectedPath);
image(loadedImgage, 0, 0);
for (int i = 0; i < loadedImgage.width - 10; i = i+10) {
for (int j = 0; j < loadedImgage.height - 10; j = j+10) {
color imageColor = get(i, j);
//Applying image modification
color imageColorModified = changeColor(imageColor);
noStroke();
fill(imageColorModified);
rect(i, j, 5, 5);
}
}
//Save the image ?
if(keyPressed) {
if(key == 's' || key == 'S') {
save(getFileName(selectedPath) + "__.jpg");
exit();
}
}//if(keyPressed)
}//(selectedPath != "")
}//draw()
Answers
edit post, highlight code, press ctrl-o to format.
if you want something done once then setup is generally the place.
but often i put it in draw() and use noLoop() to stop it repeating.
Hello koogs, thanx again, but your answer is too short, I tried but where exactly would you call noLoop()?
I can't do that in a way where everything works like before. If I call noLoop() at line 70 or after 77 (no difference) this still won't allow me to save the image...
you can call noLoop() in setup or draw - it sets an internal flag and stops draw running again (until loop or redraw is called and resets the flag)
but you have stuff in your code that only works if the draw is looping so i'm not sure that'll work.
you are calling image on your selected image, which will copy it to the screen. you can do this, modify it, and save it and if you then call background(0) it'll fill the screen with black meaning that you don't see the image.
you can also set selectedPath = "" after line 77 to stop the draw loop processing.
actually, that won't work.
you are only checking the save key if you have a selected image so unselecting the image at the bottom of the loop disables the checking for the save key.
i'm tempted to move everything to a keyPressed() method. check if s is pressed there, then check if there's an image selected, modify it, save it out.
Hello koogs, thanx for your assistance but in fact it simply doesn't work as it should : first I have nothing except a 600x600 "blank" square displayed, until I press the "s" key. The expected behavior should be : the display of the modified image, then when I press "s" key the image should be saved.
still not exactly sure what you are trying to do tbh
i've moved the saving to keyPressed and added noloop stuff to bottom of draw, within the condition. image draws once. can save at anytime.
Hi koogs, and thanx again! This time everything works, this exactly how it was supposed to work, with the exception of line 20 (selectedPath = "";) which is leading to a wrong path and file name for the saved image file. So I just deleted this line and everything works fine. Thanx a lot for your help!