Log in

View Full Version : for cycle


fenomeno83
26th January 2011, 20:46
Hello. I need to write in a script a code like this

//I want load, process and save 1.jpg, 2.jpg, 3.jpg



for (i = 1; i <= 3; i++)

{

string1 = String(i)
string2 = string1 + ".jpg"
ImageSource(string2)
.....
.....
ImageWriter (string2,0,0,"jpg")
i = i + 1;
}


How can I implement loop in avisynth?
thanks

Gavino
26th January 2011, 23:44
The Avisynth language does not have a loop construct, but looping can be achieved using a recursive function.

Alternatively, GScript is a set of language extensions that includes a for-loop construct.

There are a couple of things to sort out in your script, though.
First, I'm sure you didn't mean to have both i++ and i=i+1 in the code.
Less obviously, you need to be aware that ImageWriter will only write to the file for frames that form part of the script's final output. So you need to join the individual ImageWriter results together.
GScript("""
for (i = 1, 3) {
string1 = String(i)
string2 = string1 + ".jpg"
ImageSource(string2)
.....
ImageWriter (string2,0,0,"jpg")
result = (i == 1 ? last : result + last)
}
return result
""")
Note that this assumes the images all have the same resolution; otherwise the splice will fail unless you convert them to the same size.

ajp_anton
27th January 2011, 01:54
Note that this assumes the images all have the same resolution; otherwise the splice will fail unless you convert them to the same size.
You could just do

result = (i == 1 ? pointresize(16,16) : result + pointresize(16,16))

or a better resizer/bigger size if you actually want to preview the images.

fenomeno83
27th January 2011, 11:39
i don't understand how write several output images with imagewriter (write only the first)

how create in alternative a recursive function?

Gavino
27th January 2011, 12:03
i don't understand how write several output images with imagewriter (write only the first)
Are you saying the script above does not work?
Note that you must 'play' the entire video for ImageWriter to write everything out.
how create in alternative a recursive function?
function f(int i, int limit) {
string1 = String(i)
string2 = string1 + ".jpg"
ImageSource(string2)
.....
ImageWriter (string2,0,0,"jpg")
PointResize(16, 16) # see ajp_anton's post
return (i < limit ? last+f(i+1, limit) : last)
}

f(1, 3)

fenomeno83
27th January 2011, 14:26
thanks!!