Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion. Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules. |
26th February 2008, 05:28 | #1 | Link |
Resize Abuser
Join Date: Apr 2005
Location: Seattle, WA
Posts: 623
|
Auto Resize: Keep Aspect Ratio w/ AddBorder
This will resize the source keeping the aspect ratio, by adding a border when needed. Useful for resizing material before it's uploaded to youtube, ect...
Code:
# ResizeKAR() - May 8th, 2008 # Resize the source keeping the aspect ratio, by adding a border when needed. # # Input: # clip c: input clip. # int width: output width # int height: output height # Optional: # string ResizeMethod: Default(BilinearResize) # int BackgroundColor: Default($000000) # bool NoBorders: Resizes without adding borders. Default(False) # # Notes: # Respects ColorSpace Restrictions function ResizeKAR(clip c, int width, int height, string "ResizeMethod", int "BackgroundColor", bool "NoBorders") { BackgroundColor = Default(BackgroundColor, $000000) ResizeMethod = Default(ResizeMethod, "BilinearResize") NoBorders = Default(NoBorders, False) ratioS = Float(width(c))/Float(height(c)) ratioD = Float(width)/Float(height) newW = Round(height*ratioS/2)*2 newH = Round(width/ratioS/2)*2 BorderH = (NoBorders==True) ? 0 : Round((height-newH)/2) BorderW = (NoBorders==True) ? 0 : Round((width-newW)/2) #Dest Higher Then Source; Dest Wider Then Source; Same Ratio c = \ (ratioS>ratioD) ? \ Eval(ResizeMethod + "(c, " + String(width) + ", " + String(newH) + ")"). \ AddBorders(0, BorderH, 0, BorderH, BackgroundColor) : \ (ratioS<ratioD) ? \ Eval(ResizeMethod + "(c, " + String(newW) + ", " + String(height) + ")"). \ AddBorders(BorderW, 0, BorderW, 0, BackgroundColor) : \ (ratioS==ratioD) ? \ Eval(ResizeMethod + "(c, " + String(width) + ", " + String(height) + ")" ) : \ nop() #fix 1px changes, works only with 4:4:4 c = \ (IsRGB(c)) && (width>width(c)) && (NoBorders==false) ? c.AddBorders(0, 0, 1, 0) : \ (IsRGB(c)) && (height>height(c)) && (NoBorders==false) ? c.AddBorders(0, 0, 0, 1) : \ c return c } Code:
ColorBars().ResizeKAR(730, 488, "BlackmanResize") Adding borders with a width of 1px top/bot or left/right (total of 2px) doesn't happen correctly if video source is not RGB (4:4:4). Border sizes with a multiple of 4px always work. Shrinking the clip could be done better, i think. Overall this works very well, just pointing out some very minor issues. Future Ideas:
EDIT March 4th, 2008: Code Rewrite. Code is cleaner, added option to pass your own resizer to the function, 1px border fix May 8th, 2008: Added the no border option. Added header to function. Use Of ZoomBox is recommended instead of ResizeKAR
__________________
Mine: KenBurnsEffect/ZoomBox CutFrames Helped: DissolveAGG ColorBalance LQ Animation Fixer Last edited by mikeytown2; 24th May 2008 at 08:38. |
10th May 2008, 10:07 | #3 | Link |
Resize Abuser
Join Date: Apr 2005
Location: Seattle, WA
Posts: 623
|
No but... What i do is put this function in an .avsi file. The i at the end of avs makes it autoload if you place this in your plugin dir. On my computer that dir is here
C:\Program Files\AviSynth 2.5\plugins Hope this helps! http://avisynth.org/mediawiki/Import (see note 2 at bottom)
__________________
Mine: KenBurnsEffect/ZoomBox CutFrames Helped: DissolveAGG ColorBalance LQ Animation Fixer |
10th May 2008, 20:16 | #5 | Link | |
Registered User
Join Date: Sep 2005
Location: 100011110010001000001 10000011111111000001
Posts: 221
|
Quote:
This is just a productivity-enhancement tip ; you control your time, of course.
__________________
AVSLib, a free extension library for Avisynth. Current version: 1.1.0 (beta), 14/05/2007. [ Home page | Download page ] |
|
11th May 2008, 02:44 | #6 | Link |
Resize Abuser
Join Date: Apr 2005
Location: Seattle, WA
Posts: 623
|
gzarkadas, could you give an example on how to use one of these avslib functions, in order to replicate my ResizeKAR?
http://avslib.sourceforge.net/packag...rs/resize.html
__________________
Mine: KenBurnsEffect/ZoomBox CutFrames Helped: DissolveAGG ColorBalance LQ Animation Fixer |
13th May 2008, 00:07 | #7 | Link | ||
Dolphin Blue
Join Date: Mar 2007
Posts: 336
|
Quote:
as i understand, you need a target clip, so to do what resizekar() wants to do you'd have to create a dummy target clip? Quote:
in which case the manual calculation is trivial. i'd consider adding a dar setting. also, 'noborders' is fairly useless, autoscaling to either the horizontal or vertical setting. if i want to scale to a set width or height, i'd like to do just that.
__________________
injected with feelings; with no final fading |
||
13th May 2008, 00:20 | #8 | Link | |
Resize Abuser
Join Date: Apr 2005
Location: Seattle, WA
Posts: 623
|
Quote:
http://forum.doom9.org/showthread.php?t=137534
__________________
Mine: KenBurnsEffect/ZoomBox CutFrames Helped: DissolveAGG ColorBalance LQ Animation Fixer |
|
13th May 2008, 21:07 | #9 | Link |
Registered User
Join Date: Sep 2005
Location: 100011110010001000001 10000011111111000001
Posts: 221
|
Hi,
I stumbled upon a bug on this one , but again this was good because I found it . The intended behavior for ResizeToFit is for the two calls below (see example code): Code:
LoadModule("avslib", "filters", "resize") last = ...whatever source...400x400 c1 = last.ResizeToFit(720, 480, RTF_ZOOM, "LanczosResize") c2 = last.ResizeToFit(720, 480, RTF_CROP, "LanczosResize") The crop mode works ok, however in the zoom mode there is an error in line 174 of resize.avsi, inside the internal __resize_tofit_zoom function. In this line, the c variable: Code:
: Overlay(c, rsz, x=r1w, y=r1h, mode="blend", opacity=1.0) \
__________________
AVSLib, a free extension library for Avisynth. Current version: 1.1.0 (beta), 14/05/2007. [ Home page | Download page ] |
16th May 2008, 01:36 | #10 | Link |
Resize Abuser
Join Date: Apr 2005
Location: Seattle, WA
Posts: 623
|
This is interesting... My ZoomBox Function now can do this. It's higher quality then ResizeKAR so i would recommend ZoomBox over resizeKAR now! It also allows you to add borders or crop.
For the examples below, I'm going to use my HDV camera. It Records to 1440x1080 (PAR), but it plays back at 1920x1080 (DAR). Original Image: Image Used: http://en.wikipedia.org/wiki/SMPTE_color_bars Code:
#change the OutputWidth and OutputHeight and see how the final clip changes. OutputWidth=720 OutputHeight=480 #compare different resize methods Resize = "BlackmanResize" # 1000 frames long clip with a PAR of 1440x1080, Crop to Fit. Target is square pixels ImageReader("1080x1440widescreenhl3.png",0,0) ZoomBox(OutputWidth, OutputHeight, ResizeMethod=Resize, DisplayAR=1920.0/1080.0, Align=5) AssumeFPS("ntsc_video").Loop(1000) Code:
#change the OutputWidth and OutputHeight and see how the final clip changes. OutputWidth=720 OutputHeight=480 #compare different resize methods Resize = "BlackmanResize" # 1000 frames long clip with a PAR of 1440x1080, Add Borders to Fit. Target is square pixels ImageReader("1080x1440widescreenhl3.png",0,0) ZoomBox(OutputWidth, OutputHeight, ResizeMethod=Resize, DisplayAR=1920.0/1080.0, Align=-5) AssumeFPS("ntsc_video").Loop(1000) Code:
#change the OutputWidth and OutputHeight and see how the final clip changes. OutputWidth=720 OutputHeight=480 #compare different resize methods Resize = "BlackmanResize" # 1000 frames long clip with a PAR of 1440x1080, Add Borders to Fit. Target is square pixels ImageReader("1080x1440widescreenhl3.png",0,0) SeparateFields().SelectEven() #fast deinterlacer ZoomBox(OutputWidth, OutputHeight, ResizeMethod=Resize, DisplayAR=1920.0/1080.0, Align=-5) AssumeFPS("ntsc_video").Loop(1000) Footnotes: http://www.doom9.org/index.html?/aspectratios.htm http://en.wikipedia.org/wiki/Pixel_aspect_ratio
__________________
Mine: KenBurnsEffect/ZoomBox CutFrames Helped: DissolveAGG ColorBalance LQ Animation Fixer Last edited by mikeytown2; 5th June 2008 at 21:54. Reason: Made example more clear |
5th June 2008, 18:02 | #11 | Link | |
Dolphin Blue
Join Date: Mar 2007
Posts: 336
|
hi,
had time to try they're all wrong. first off, the usage of the term DisplayAR is wrong, as that's not what your variable is. it's more akin to Sample aspect ratio, but not that either. i'd say it's sample aspect ratio of target as required by defined box. something like that. anyway terminology is wrong. so with terminology wrong, usage is wrong too. for the above examples, setting DisplayAR to 1.5 is correct. and the call to bilinearresize is useless as it's overriden by DisplayAR. (aspect wise, and harmful elsewise) the approach is also wrong conceptually wise. as it forces more calculations than a manual approach would. as an example of a 16/9 720x480 resize to 4/3 720x480 Code:
ZoomBox(720, 480, ResizeMethod="lanczosResize", DisplayAR=2, Align=-5) ZoomBox(720, 480, ResizeMethod="lanczosResize", DisplayAR=2, Align=5) Code:
ZoomBox(720, 480, ResizeMethod="lanczosResize", DisplayAR=1.125, Align=-5) ZoomBox(720, 480, ResizeMethod="lanczosResize", DisplayAR=1.125, Align=5) and then further plug the SARwithinbox. I like the way +/- switches between padding and 'cropping' though. What i'd envisioned when i mentioned dar was related to this: Quote:
to handle DV, DVD -> square pixel target. so you'd need DAr=4/3, 16/9, or whatever, with default being source DAR=SAR. of course working on something more complex like having inputs for both source DAR and target DAR would be welcome. I also looked at the last examples you set in the kenburns thread. I prefer to keep life simple with digital computations, but being able to automatically do resizing based in the paper referenced would be a good bonus. your examples there all look correct, they still suffer from terminology+ issues i've already mentioned here. and i think h. in the pal example is meant to like so: Code:
h=BilinearResize(712,486).crop(0,3,0,-3).addborders(4,0,4,0) ty
__________________
injected with feelings; with no final fading |
|
5th June 2008, 22:04 | #12 | Link |
Resize Abuser
Join Date: Apr 2005
Location: Seattle, WA
Posts: 623
|
Immersion, thanks for your input! For the example, 2 posts up from here, I needed a picture that didn't have square pixels, thus i used BilinearResize to do it. This wasn't 100% clear, so I uploaded the picture without square pixels, in order to give a better example, and changed the code. I agree that the "DisplayAR" terminology is wrong. It is more like an aspect ratio modifier. You can use it to correct the aspect ratio for the source and/or destination/target. I agree that when correcting for the destination/target AR, it requires more math, mainly because I was using my "DisplayAR" to correct for my HDV cam's non square pixels; so when correcting the source AR it's simple. Having inputs for both source DAR and target DAR could be done, but I need to know the math; internally it would have to convert the 2 inputs into 1 ratio.
Also would you mind posting the math on how you came up with 2 and 1.125, and give an example on how to do it the usual way since you posted how to do it using my code. Converting for target AR is something I rarely do. Thanks for your time!
__________________
Mine: KenBurnsEffect/ZoomBox CutFrames Helped: DissolveAGG ColorBalance LQ Animation Fixer |
5th June 2008, 22:19 | #13 | Link | |
Moderator
Join Date: Nov 2001
Location: Netherlands
Posts: 6,364
|
Quote:
|
|
6th June 2008, 00:29 | #14 | Link | ||||
Dolphin Blue
Join Date: Mar 2007
Posts: 336
|
Quote:
i was pointing out, aspect wise, it was useless. you could have resized to anything, and (picture quality aside) by setting displayAR, the result would be the same. the examples are still wrong. you still need to set dispalayAR to 1.5. i'm assuming you're resizing to 720,480 with 29.97 fps as a dvd target. A safe assumption i think. otherwise if it were an arbitrary box, you should have selected something a bit more, well, arbitrary. here a simple lanczosresize(720,480) is correct in a final function it should be equally simple as zoombox(720,480) as source dar = target dar Quote:
the math shouldn't take long, once the approach is decided. Quote:
720x480 with 4/3 dar -> 640x480, actual width/1.778 = 360 so for your 16/9 image sit in the 4/3 image you need active image = 720x360 with vertical padding up to 480. 720/360=2 4/3 to 16/9 720x480 with 16/9 dar -> 843x480, actual height*1.333 = 640 640/1.185(16/9 PAR)=540 so for your 4/3 image sit in the 16/9 image you need active image = 540x480 with horizontal padding up to 720. 540/480=1.125 these are working on placing the image within the box with padding, to fit your model. there are also the crop methods. which are nicely achieved in your model by simply setting align to 5 instead of -5, but calculations have to be done based on -5 within the defined box. i don't know, maybe my explanation is a bit convoluted. it's correct though. Quote:
i've seen SAR referred to as an alternate naming for PAR, but never to mean it's inverse. i take cue from gspot, and others, in taking sample/storage aspect ratio to mean the storage frame size ratio, so that SAR x PAR = DAR [720/480=1.5] x 1.185 = [16/9=1.778] if you have some other definition for me, i'll use it. ty
__________________
injected with feelings; with no final fading |
||||
6th June 2008, 04:39 | #15 | Link |
Resize Abuser
Join Date: Apr 2005
Location: Seattle, WA
Posts: 623
|
Made a function that is tuned to sourceDAR=4/3 & targetDAR=16/9. This function needs to be more general. So right now the Destination Width and Height can be anything, the source DAR and target DAR are locked down. Eventually all variables should be changeable, and have it produce the correct image. Any help would be much appreciated. You can change the targetDAR without much thought (will give incorrect results), changing of the sourceDAR requires redrawing the square.
Code:
#Draw a black square BlankClip(1,640,480, color=$FFFFFF) a = Overlay(BlankClip(1,240,240),200, 120) Modify(a, 720,480) last + Modify(a, 640,480) last + Modify(a, 480,480) last + Modify(a, 320,480) last + Modify(a, 720,320) last + Modify(a, 720,240) last + Modify(a, 720,120) last + Modify(a, 853,480) Function Modify(clip c, int W, int H) { c SourceDAR=4.0/3.0 TargetDAR=16.0/9.0 ModAR = (1.0/(TargetDAR/(W/((H*SourceDAR)/TargetDAR)))) ModAR = W/(H*SourceDAR) #same as above zoomBox(width=W, height=H, ResizeMethod="BilinearResize", DisplayAR=ModAR, Align=-5) Subtitle(String(ModAR)) #DVD Player will resize so it plays back at 16/9 BilinearResize(Round(480*TargetDAR),480) #overlay a red square so its easy to check for correct AR Overlay(BlankClip(1,236,236, color=$FF0000),Round((480*TargetDAR-236)/2.0), 122) }
__________________
Mine: KenBurnsEffect/ZoomBox CutFrames Helped: DissolveAGG ColorBalance LQ Animation Fixer Last edited by mikeytown2; 6th June 2008 at 05:41. |
6th June 2008, 07:37 | #16 | Link |
Resize Abuser
Join Date: Apr 2005
Location: Seattle, WA
Posts: 623
|
Thank God for my TI83's stat button... I got it. Here is the magical code
Code:
ModAR = SourceDAR*(float(W)/float(H))/TargetDAR Code:
#Draw a black square BlankClip(1,640,480, color=$FFFFFF) a = Overlay(BlankClip(1,240,240),200, 120).KillAudio().AssumeFPS(24).ConvertToRGB32() Global SourceDAR=4.0/3.0 Test2(a) b = ImageReader("traingr4.jpg",0,0).ConvertToRGB32().AssumeFPS(24) Global SourceDAR=16.0/9.0 last + Test2(b) Function Test2(clip a) { Test(a, 5.0/9.0).ZoomBox(800,600, align=-5) last + Test(a, 6.0/9.0).ZoomBox(800,600, align=-5) last + Test(a, 7.0/9.0).ZoomBox(800,600, align=-5) last + Test(a, 8.0/9.0).ZoomBox(800,600, align=-5) last + Test(a, 9.0/9.0).ZoomBox(800,600, align=-5) last + Test(a, 10.0/9.0).ZoomBox(800,600, align=-5) last + Test(a, 11.0/9.0).ZoomBox(800,600, align=-5) last + Test(a, 12.0/9.0).ZoomBox(800,600, align=-5) last + Test(a, 13.0/9.0).ZoomBox(800,600, align=-5) last + Test(a, 14.0/9.0).ZoomBox(800,600, align=-5) last + Test(a, 15.0/9.0).ZoomBox(800,600, align=-5) last + Test(a, 16.0/9.0).ZoomBox(800,600, align=-5) last + Test(a, 17.0/9.0).ZoomBox(800,600, align=-5) last + Test(a, 18.0/9.0).ZoomBox(800,600, align=-5) last + Test(a, 19.0/9.0).ZoomBox(800,600, align=-5) last + Test(a, 20.0/9.0).ZoomBox(800,600, align=-5) } Function Test(clip a, float TargetDAR) { Modify(a, 853,480,TargetDAR) last + Modify(a, 720,480,TargetDAR) last + Modify(a, 640,480,TargetDAR) last + Modify(a, 480,480,TargetDAR) last + Modify(a, 320,480,TargetDAR) last + Modify(a, 240,480,TargetDAR) last + Modify(a, 720,800,TargetDAR) last + Modify(a, 720,720,TargetDAR) last + Modify(a, 720,640,TargetDAR) last + Modify(a, 720,480,TargetDAR) last + Modify(a, 720,320,TargetDAR) last + Modify(a, 720,240,TargetDAR) last + Modify(a, 720,120,TargetDAR) last + Modify(a, 640,320,TargetDAR) last + Modify(a, 480,360,TargetDAR) last + Modify(a, 640,640,TargetDAR) } Function Modify(clip c, int W, int H, float TargetDAR) { c ModAR = SourceDAR*(float(W)/float(H))/TargetDAR zoomBox(width=W, height=H, ResizeMethod="BilinearResize", DisplayAR=ModAR, Align=-5) #Subtitle(" ModAR=" + String(ModAR) + " width=" + String(W) + " height=" + String(H)) #DVD Player will resize so it plays back at 16/9 BilinearResize(Round(480*TargetDAR),480) #overlay a red square so its easy to check for correct AR Overlay(BlankClip(1,236,236, color=$FF0000),Round((480*TargetDAR-236)/2.0), 122, opacity=.8) } Let me know what you think. The Red square is there to help with checking for squareness, it doesn't work correctly if the TargetDAR<1, but it still aids in checking the shape.
__________________
Mine: KenBurnsEffect/ZoomBox CutFrames Helped: DissolveAGG ColorBalance LQ Animation Fixer Last edited by mikeytown2; 6th June 2008 at 10:16. Reason: Got rid of Pow() |
6th June 2008, 09:49 | #17 | Link | |
Moderator
Join Date: Nov 2001
Location: Netherlands
Posts: 6,364
|
Quote:
http://forum.doom9.org/showthread.ph...658#post685658 http://forum.doom9.org/showthread.ph...958#post686958 So i would use a different abbreviation for storage aspect ratio. |
|
6th June 2008, 19:10 | #18 | Link | ||
Dolphin Blue
Join Date: Mar 2007
Posts: 336
|
Quote:
i was thinking more along the lines of fixing both terminology and function of displaydar in zoombox, but whatever works. yes, it's a huge hog. nice effect, doesn't really demonstrate well practical use, i think. i tried it and it seems to work with everything. so, something along these lines: Code:
Function Modify(clip c, int W, int H, float "SDAR", float "TDAR", string "Rz", int "A", int "color") { c SDAR = Default(SDAR, Float(c.width())/Float(c.height())) TDAR = Default(TDAR, Float(W)/Float(H)) A = Default(A, -5) color = default(color, $000000) Rz = default(Rz, "lanczosresize") ModAR = SDAR*Pow(float(W)/float(H),1)*Pow(TDAR,-1) zoomBox(width=W, height=H, ResizeMethod=Rz, DisplayAR=ModAR, Align=A, color=color) } and how do make it so: if Sdar AND Tdar are not user defined, THEN set Sdar=Tdar ? Quote:
I'm siding with seemoredigital, and gspot, but i guess i could use StAR as symbol for storage aspect ratio. ty mikeytown2
__________________
injected with feelings; with no final fading Last edited by 45tripp; 6th June 2008 at 19:35. |
||
6th June 2008, 23:18 | #19 | Link | |
Resize Abuser
Join Date: Apr 2005
Location: Seattle, WA
Posts: 623
|
Quote:
__________________
Mine: KenBurnsEffect/ZoomBox CutFrames Helped: DissolveAGG ColorBalance LQ Animation Fixer |
|
7th June 2008, 01:15 | #20 | Link | |
Dolphin Blue
Join Date: Mar 2007
Posts: 336
|
Quote:
any chance of this? any plans to incorporate the analogue calculations you made an exmaple of using the pars from the paper referenced?
__________________
injected with feelings; with no final fading |
|
|
|