View Full Version : InsertImage() - Simple function to add an image to a video
thetoof
12th April 2008, 08:08
As I was doing an amateur movie for a school project, I came across the following problem: I wanted to add a still image with a given size, location, lenght and opacity, but the only functions I found for avisynth were to remove such images (logo removal...).
So, I created this simple script and I thought that I could post it here to help anyone that would need to do something similar. [cropping, resizing, spatial positioning, temporal positioning, filtering]
Description of functions
Image = Path of the image you want to add
Start = Your image will appear at this frame
End = Last frame on which your image will appear
Left = # of pixels to crop on the left side of the image
Top = # of pixels to crop at the top of the image
Right = # of pixels to crop on the right side of the image
Bottom = # of pixels to crop at the bottom of the image
ResizeX = Your image will appear with this width
ResizeY = Your image will appear with this height
Opacity = Opacity of your image 0.5 means the video and the image will have the same opacity (50% opacity),1.0 (or 1) means your image will completely replace the original image (100% opacity), etc. etc. etc. (you can use any percentage you want... 32% opacity would be 0.32)
X = Location of your image on the horizontal axis
Y = Location of your image on the vertical axis
Filter = User-defined filter to be applied on the image only. Single filter = "yourfilter(parameters of this filter)" Multiple filters = "yourfilter(parameters).yourotherfilter(parameters).yourotherfilter(parameters)" (you can add as many as you want, simply separate them with a .)
Example:
InsertImage("C:\your image.jpg",260,3547,0,0,0,0,60,40,0.5,550,10,"greyscale()")
Frame start = 260
Frame end = 3547
Cropping = None
Image size on the video = 60x40
Opacity = 50%
Location = (550,10)
Filter applied on the image: greyscale(), so now the image is in black and white
To use it, download the attached .avsi and put in in the "plugins" folder of Avisynth.
2008/04/14 - New parameter: "filter"
2008/04/12 - Original release
sh0dan
12th April 2008, 09:29
What's the difference to the built-in Overlay() function?
thetoof
12th April 2008, 09:44
None. It's aimed towards less experienced users that could have a hard time finding a good way to do it.
It simplifies the cropping + resizing of the image as well as the temporal positioning of that image, that's all. The first time I had to do that, I went through transforming the image into a .ass and then importing it with textsub(), which is horrible, so I thought it could be somewhat useful to others who might not know how to do it.
I'm not offering a whole new function, just a way of using the built-in ones.
Gavino
12th April 2008, 19:33
Why not get rid of the fps parameter and just use video.frameRate instead? After all, the overlay will only work if the correct frame rate is used.
Also (a common error) video.trim(0,start-1) won't work if start is 0 or 1.
Writing general purpose functions is not easy - you have to ensure that you cater for all valid values of the parameters and, ideally, reject invalid ones with a helpful message.
Usability can also often be improved by using optional parameters with suitable defaults.
To see how to do it well, I highly recommend all budding function writers to spend some time studying stickboy's functions (http://avisynth.org/stickboy/).
thetoof
12th April 2008, 20:26
Thanks for spotting those bugs!
I made the following changes:
- removed the "fps" parameter and used fps=video.framerate
- changed how the output is done
a= (start==1) ? video.trim(0,-1) : a
[...]
output = a+b+c
output = (start==0) ? b+c : output
return(output)
Gavino
13th April 2008, 10:27
[a=video.trim(0, start-1)]
a= (start==1) ? video.trim(0,-1) : a
[...]
output = a+b+c
output = (start==0) ? b+c : output
That looks right now, although you could write it more concisely as:
a = video.trim(0, -start)
[...]
output = (start==0) ? b+c : a+b+c
(Using the trim(0, -start) trick works for all positive values of start, removing the need to special case start=1)
Nagisa_chan
13th April 2008, 18:56
And what other filters I put images
Or where can I download insertimage.zip?
thetoof
13th April 2008, 20:01
And what other filters I put images
I don't understand what you mean... do you want to add other filters to the images? Or are you asking if you need other filters in addition to InsertImage.avsi?
Or where can I download insertimage.zip?
It's an attachment in the first post of this thread ;)
thetoof
14th April 2008, 03:54
(Using the trim(0, -start) trick works for all positive values of start, removing the need to special case start=1)
Used it + return (start==0) ? b+c : a+b+c to make it even more concise :p Thanks!
Nagisa_chan's post made me think of a new function for my script: using a filter (like greyscale()) only on the image added to the video... but I'm having trouble with the syntax. I'm able to make a script that'll work with one of the situation (i.e. image is only cropped and resized or user-defined filters are also applied on it), but not for both. I know I should add a string parameter and use a boolean function like "defined(filter)" in the script... but I can't find a good way to write it.
Any hints?
Gavino
14th April 2008, 15:27
I know I should add a string parameter and use a boolean function like "defined(filter)" in the script... but I can't find a good way to write it.
Something like this should do the trick:
last = imagex
imagex = isDefined(filter) ? Eval(filter) : imagex
Nagisa_chan
14th April 2008, 18:44
i canīt understand english correctly....
for this.. sorry....
"i am bad encoder" :( i am super novice....
thetoof
14th April 2008, 19:03
@ Gavino
I added "string filter" to the parameters, but there is no function named "isDefined", so I used imagex = Defined(filter) ? Eval(filter) : imagex For some reason, it didn't work, so I split it like this :imagey = imagesource(image,fps=video.framerate).crop(left,top,-right,-bottom).spline36resize(resizex,resizey)
converttoyv12(imagey)
filtered = eval(filter)
imagex = Defined(filter) ? filtered : imagey.converttoyv12()
because every other use of eval failed and it was the only way that made it work. But, for some reason, when I don't specify a filter in the script that calls insertimage(), it says that I have "invalid arguments", which is back to being where I was yesterday (I had tried the same approach... that's what I was talking about when I said I couldn't write a script that would work for both cases).
@ Nagisa_chan
i am super novice....
It doesn't matter! That's why we're here :p Again, what was it that you wanted to know in your previous post?
Gavino
14th April 2008, 21:26
@ Gavino
I added "string filter" to the parameters, but there is no function named "isDefined", so I used imagex = Defined(filter) ? Eval(filter) : imagex
Sorry, my mistake - I meant Defined.
For some reason, it didn't work, so I split it like this :imagey = imagesource(image,fps=video.framerate).crop(left,top,-right,-bottom).spline36resize(resizex,resizey)
converttoyv12(imagey)
filtered = eval(filter)
imagex = Defined(filter) ? filtered : imagey.converttoyv12()
because every other use of eval failed and it was the only way that made it work. But, for some reason, when I don't specify a filter in the script that calls insertimage(), it says that I have "invalid arguments"
You missed out the other line of my solution - you need to set last so that "filter" has something to work on. In your solution, last gets set implicitly (on the 2nd line).
However, it doesn't work when no filter is provided because eval is given an undefined argument, that's why the eval has to be inside the Defined(filter) conditional. So, tidying it up a bit, you could use:
imagesource(image,fps=video.framerate)
crop(left,top,-right,-bottom)
spline36resize(resizex,resizey)
converttoyv12()
imagex = Defined(filter) ? eval(filter) : last
thetoof
14th April 2008, 22:29
I've got the same issue... "Invalid arguments to function "insertimage".
This is the script:
avisource("file.avi")
insertimage("path\file.png",1,13,25,0,25,270,186,156,1,254,194)
and this is the function:
function InsertImage(clip video, string image, int start, int end, int left, int top, int right, int bottom, int resizex, int resizey, float opacity, int x, int y, string filter)
{
imagesource(image,fps=video.framerate)
crop(left,top,-right,-bottom)
spline36resize(resizex,resizey)
converttoyv12()
imagex = Defined(filter) ? eval(filter) : last
overlayed=overlay(video,imagex,x,y,opacity=opacity)
a=video.trim(0,-start)
b=overlayed.trim(start,end)
c=video.trim(end+1,0)
return (start==0) ? b+c : a+b+c
}
If I add ,"greyscale()" right after 194, it works...
Gavino
14th April 2008, 22:43
You need to make the filter an optional parameter, by enclosing it's name in quotes, ie
function InsertImage(clip video, string image, int start, int end, int left, int top, int right, int bottom, int resizex, int resizey, float opacity, int x, int y, string "filter")
Once again, I recommend you (and anyone else writing functions) to study the code in stickboy's functions (http://avisynth.org/stickboy/). I learnt lots of useful techniques just doing that.
thetoof
14th April 2008, 22:52
Doh! Actually, in all my previous attempts, I had used "filter"... but I simply forgot to do it when I implemented your solution.
However, it doesn't work when no filter is provided because eval is given an undefined argument, that's why the eval has to be inside the Defined(filter) conditional.
and... I didn't know about that info, so thanks a bunch for helping me through it.
Nagisa_chan
15th April 2008, 00:23
thetoof thankīs for this avsi
:)
Comatose
15th April 2008, 11:59
A job well done :)
Nagisa_chan
15th April 2008, 14:28
Opacity can be changed from 0 to 1 as a transition image?
http://img294.imageshack.us/img294/6540/tsukasa0201rs4.gif
Gavino
15th April 2008, 16:19
Opacity can be changed from 0 to 1 as a transition image?
This could be done by applying Animate (http://avisynth.org/mediawiki/Animate) to InsertImage - messy, because of the number of parameters it has and slow because the entire function would be 'recompiled' on each frame.
A better approach might be to use the conditional variables feature of Overlay (http://avisynth.org/mediawiki/Overlay#Conditional_Variables). For example, replace the lines
overlayed=overlay(video,imagex,x,y,opacity=opacity)
a=video.trim(0,-start)
b=overlayed.trim(start,end)
by the following:
a=video.trim(0,-start)
overlayed=overlay(video.trim(start,end),imagex,x,y,opacity=0) #start opacity at 0
b=FrameEvaluate(overlayed, "OL_opacity_offset=float(current_frame)/"+string(end-start))
That assumes you always want a transitional effect (and then, of course, the opacity parameter of InsertImage can be removed). If you want to make the transition optional, it gets a bit more messy, but still do-able along the same lines.
J!M
15th April 2008, 17:49
Sorry, I'm newbie, how to use it??? I want to add automatically watermark, when I convert with Allok FLV Converter from mpg to flv. How to do this?
thetoof
16th April 2008, 00:09
I'll add some other functions (transition, move, zoom, etc. + possibility of having user-defined ones) in the next days... good idea!
@ J!M
What is it that you don't know? Is it related to avisynth usage, something not clear in the function description, or something else?
Also, if you only want to add text (i.e. not a logo or an image) to some video, Aegisub (http://www.malakith.net/aegiwiki/Main_Page) can do very good things. By setting the position, size, font and transparency of your text, you can have a very good watermark. I'd recommend that you read the help folder of that program and then try it a little bit. Then, you can simply add the text to your video using Textsub(your sub file) at the end of your avisynth script.
lansing
26th May 2009, 21:05
I think named parameter would be more convinient, and setting all cropping and position default to 0 will also help for ppl who only want a few features of the function
thetoof
26th May 2009, 22:15
Hehe, I wrote this about 1 month or so after learning what avisynth was... so yeah, there's many many things that'd need to be improved. Honestly, I don't feel like it should be pushed forward since the built-in functions (animate, overlay and so forth) are much more useful and versatile than "InsertImage" - a noob's first function :p
edit - Of course, if you need help with overlaying images on top of videos, I'd be pleased to assist you (post your script)
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.