Log in

View Full Version : How to save a certain frame to BMP at command line?


zee944
1st December 2008, 13:08
Hey,

Is there an easy way to save a certain frame of an AviSynth script to a bitmap file?

What I really want to do is to analyze the pixels of one frame. Currently I'm thinking something of...:
1. Generate a 1 frame long .AVS file;
2. Convert it with AVS2AVI to AVI;
3. Save the 1-frame-long AVI to BMP with some cmd tool;
4. Convert the BMP to text format values something like this or similar:

Xpos, Ypos, R, G, B,
0, 0, 254, 135, 44
1, 0, 128, 879, 23

There's probably much easier, less awkward way to achieve this. I'd be happy with an easy way to only export a frame in .BMP (the first three step), then I'd somehow solve the conversion problem to txt format with another command line tool.

J_Darnley
1st December 2008, 13:33
Does ImageWriter() solve 1-3? [EDIT] Or rather, just solve 3. You still need to do 1 your self and 2 can be skipped.

zee944
1st December 2008, 13:42
Does ImageWriter() solve 1-3? [EDIT] Or rather, just solve 3. You still need to do 1 your self and 2 can be skipped.

It seems it does, thank you!

Perhaps there's a simple way for putting the color values into a .TXT or .CSV as well? :)

zee944
1st December 2008, 13:52
Oh, no. I realized it won't work from command line. I need to achieve this from a .CMD script. :(

J_Darnley
1st December 2008, 14:12
avsutil -play should do that. As for getting the values as plain text that might be simple to do but I am not aware of any program to do it. If you are going to process the values anyway why not leave them as the binary values?

hanfrunz
1st December 2008, 15:12
i am curios what are you doing with this data?
hanfrunz

zee944
1st December 2008, 15:32
If you are going to process the values anyway why not leave them as the binary values?

I'm not going to process the values (at least not this way).

i am curios what are you doing with this data?

I just analyze them. Average luma, RGB averages etc. Something like RGBAdjust(analyze=True) does. The final purpose is to adjust the video through the .AVS file to look similar to another one. The emphasis is on the automatism.

Gavino
1st December 2008, 16:15
I just analyze them. Average luma, RGB averages etc. Something like RGBAdjust(analyze=True) does.
In that case (if you don't actually need the raw pixel data itself), you could possibly do the whole thing inside Avisynth using Writefile (http://avisynth.org/mediawiki/WriteFile).

zee944
1st December 2008, 16:42
In that case (if you don't actually need the raw pixel data itself), you could possibly do the whole thing inside Avisynth using Writefile (http://avisynth.org/mediawiki/WriteFile).

How can I write RGBAdjust(analyze=True) output to a file with it? Am I overlooking something?

Gavino
1st December 2008, 17:48
How can I write RGBAdjust(analyze=True) output to a file with it? Am I overlooking something?
You use the Run-time functions (http://avisynth.org/mediawiki/ConditionalFilter#Runtime_Functions) to extract the statistics on a per-frame basis. OK, you can't replicate RGBAdjust exactly, and there are more functions available for YUV than RGB, but it might be enough for your needs (only you can decide that, of course).

Consider it as an additional possible tool.

EDIT:
Note that you can derive additional data by combining some functions, for example, the average red can be calculated as RGBDifference(ShowRed(), BlankClip(last))

zee944
2nd December 2008, 19:46
You use the Run-time functions (http://avisynth.org/mediawiki/ConditionalFilter#Runtime_Functions) to extract the statistics on a per-frame basis.

Thank you very much, this works. It's enough at the moment, although probably I'll still need the pixel data for more complicated things.

ImageReader("Sample.jpg",0,0,pixel_type="RGB32")
filename = "c:\Sample.txt"

#Version()
WriteFile(filename, """ "current frame:" """, "current_frame", append=false)
WriteFile(filename, """ "average red:" ""","RGBDifference(ShowRed(), BlankClip(last))",append=false)
WriteFile(filename, """ "average green:" ""","RGBDifference(ShowGreen(), BlankClip(last))",append=false)
WriteFile(filename, """ "average blue:" ""","RGBDifference(ShowBlue(), BlankClip(last))",append=false)
WriteFile(filename, """ "average luma:" ""","((21*RGBDifference(ShowRed(), BlankClip(last)))+(72*RGBDifference(ShowGreen(),

BlankClip(last)))+(7*RGBDifference(ShowBlue(), BlankClip(last))))/100",append=false)

RGBAdjust(Analyze=True) # just for checking

Is the last RGBDifference() line a good measure of the luma?

kemuri-_9
2nd December 2008, 20:22
if you put up a sample i can run it on a filter i have been developing when i have had time.
it has a function to dump per pixel data to .txt files.
so it could be a solution to what you're looking for.

Gavino
2nd December 2008, 21:19
Thank you very much, this works. ...
...
WriteFile(filename, """ "current frame:" """, "current_frame", append=false)
WriteFile(filename, """ "average red:" ""","RGBDifference(ShowRed(), BlankClip(last))",append=false)
...
WriteFile(filename, """ "average luma:" """, \
"((21*RGBDifference(ShowRed(), BlankClip(last)))+(72*RGBDifference(ShowGreen(), BlankClip(last)))+(7*RGBDifference(ShowBlue(), BlankClip(last))))/100",append=false)
I'm surprised that append=false does not cause each line to overwrite the previous one, since each instance of WriteFile opens the file separately. You might be better to use a single long call to WriteFile (with explicit newlines where you want line breaks in the output).
Is the last RGBDifference() line a good measure of the luma?
It's a reasonable one.
Another possibility is to use AverageLuma(ConvertToYV12()), possibly adding matrix=... to get whatever definition of luma suits you.

IanB
2nd December 2008, 22:29
Also for luma :-... RGBDifference(GreyScale(Matrix="Rec709"), BlankClip(last)) ...

zee944
3rd December 2008, 17:36
if you put up a sample i can run it on a filter i have been developing when i have had time.
it has a function to dump per pixel data to .txt files.
so it could be a solution to what you're looking for.

Any image would do... but here (http://i179.photobucket.com/albums/w286/zee944/TD_sample_original.jpg) is one.

I'm surprised that append=false does not cause each line to overwrite the previous one, since each instance of WriteFile opens the file separately. You might be better to use a single long call to WriteFile (with explicit newlines where you want line breaks in the output).

This behaviour is quite confusing actually. Originally I only wanted to overwrite the file if existed. I'm not sure about this long call...

Also for luma :-... RGBDifference(GreyScale(Matrix="Rec709"), BlankClip(last)) ...

Thanks, will try it out. Is it more accurate?

Gavino
3rd December 2008, 17:51
This behaviour is quite confusing actually. Originally I only wanted to overwrite the file if existed. I'm not sure about this long call...
Perhaps a better way is to keep the separate calls, but use append=false for the first one and append=true for the others, together with flush=true for all of them. That should give you what you want.

If I understand what you're doing, your 'clip' has only one frame anyway so using flush is no great overhead.

kemuri-_9
3rd December 2008, 22:32
Any image would do... but here (http://i179.photobucket.com/albums/w286/zee944/TD_sample_original.jpg) is one.

here's the .txt dumps i have generated from my filter:
TD_sample_original.rar (http://kemuri9.net/forumpics/TD_sample_original.rar)
it's outputted in a space delimited+padded matrix style (numbers are padded to maintain col widths)
i tested a few pixels to photoshop values to confirm that they were correct.

i found it easiest to have the R,G,B values dump to separate files,
especially since my filter supports RGB32, RGB24, YUY2, and YV12 colorspaces.
for the reason of the latter 2 of the 4 don't have 1:1 pixel:color values.

zee944
4th December 2008, 16:32
If I understand what you're doing, your 'clip' has only one frame anyway so using flush is no great overhead.

Indeed. Thanks for all the help, Gavino.

here's the .txt dumps i have generated from my filter:
TD_sample_original.rar (http://kemuri9.net/forumpics/TD_sample_original.rar)
it's outputted in a space delimited+padded matrix style (numbers are padded to maintain col widths)
i tested a few pixels to photoshop values to confirm that they were correct.

It seems fine. But where is your plugin? :p

kemuri-_9
4th December 2008, 16:38
It seems fine. But where is your plugin? :p

still working on it before i'm completely satisfied, i have finals creeping up on me :rolleyes: