<p>I need to generate an easy animation video in Python (I use PIL to create every frame, then combine them using <code>avconv</code>). The animation is just an image being zoomed out.</p>

<p>That's my code:</p>

<pre><code>for i in xrange(zoom_length):

    # zoom level on this step
    zoom = 1.0 - 1.0 * i * (max_zoom / zoom_length)

    # "size" is the resulting video frame size
    # "img.size" is the source image size (around 2.5x bigger than the frame)
    anti_zoomed_frame_size = int(size[0] / zoom), int(size[1] / zoom)
    anti_zoomed_frame_margins = (img.size[0] - anti_zoomed_frame_size[0]) / 2, (img.size[1] - anti_zoomed_frame_size[1]) / 2

    anti_zoomed_frame = img.crop(anti_zoomed_frame_margins +
                                 (anti_zoomed_frame_size[0] + anti_zoomed_frame_margins[0],
                                  anti_zoomed_frame_size[1] + anti_zoomed_frame_margins[1]))

    frame = anti_zoomed_frame.resize(size, resample=Image.BILINEAR)

    print i, img.size, anti_zoomed_frame_size, anti_zoomed_frame_margins

    # frame No for debugging
    draw = ImageDraw.Draw(frame)
    draw.text((size[0] - 200, size[1] - 100), str(i), (0,0,0), font=font)

    yield frame
</code></pre>

<p>So what happens is I crop out of the source image a rectangle of size <code>frame_size / zoom</code> , then I resize it <code>zoom</code> times to get the needed frame size. <code>zoom &lt;= 1.0</code> so doing it in this order seems to be a good idea, though I also tried doing it the other way (resize, then crop).</p>

<p>Anyway in both cases the resulting video is kind of shaking at times. It seems that the problem is in rounding the numbers. The math I'm doing inevitably leads to non-integers, yet PIL requires integers everywhere (also the resulting frame size is obviously a pair of integer constants). This rounding operation seems to affect the smoothness of the animation badly.</p>

<p>How smooth zooming is implemented in real life? </p>

CC BY-SA 3.0
2017-01-16T14:04:26.587Z
I'm voting to close this question as off-topic because it doesn't seem to be about computer graphics, but rather video/image/signal processing.
Julien Guertault
CC BY-SA 3.0
2017-01-16T19:20:57.480Z
I think it's an interesting question. Essentially temporal aliasing problem which is too often ignored by CG people. There are a huge number of algorithms out there which look cool on still frames but break down when you start animating input parameters.
Olivier
CC BY-SA 3.0
2017-01-16T19:30:47.860Z
I meant quantization, not aliasing, but I still think it's worth discussing why this fails to be smooth and what can be done about it besides the obvious "get a toolkit with the proper floating point zoom support".
Olivier
CC BY-SA 3.0
2017-01-17T15:06:35.160Z
We have a [tag for image-processing](http://computergraphics.stackexchange.com/questions/tagged/image-processing), and there is support for the place of image processing in our scope on Meta [here](http://meta.computergraphics.stackexchange.com/questions/38/is-this-site-only-about-3d-topics) and [here](http://meta.computergraphics.stackexchange.com/questions/9/how-do-we-draw-the-line-between-questions-that-are-appropriate-for-computer-grap).
trichoplax is on Codidact now
<animation>
smooth zoom animation formulas (and rounding numbers)
Valentin Golev
# smooth zoom animation formulas (and rounding numbers)

(Asked by: username_0 on 2017-01-15)

I need to generate an easy animation video in Python (I use PIL to create every frame, then combine them using `avconv`). The animation is just an image being zoomed out.

That's my code:

```
for i in xrange(zoom_length):

    # zoom level on this step
    zoom = 1.0 - 1.0 * i * (max_zoom / zoom_length)

    # "size" is the resulting video frame size
    # "img.size" is the source image size (around 2.5x bigger than the frame)
    anti_zoomed_frame_size = int(size[0] / zoom), int(size[1] / zoom)
    anti_zoomed_frame_margins = (img.size[0] - anti_zoomed_frame_size[0]) / 2, (img.size[1] - anti_zoomed_frame_size[1]) / 2

    anti_zoomed_frame = img.crop(anti_zoomed_frame_margins +
                                 (anti_zoomed_frame_size[0] + anti_zoomed_frame_margins[0],
                                  anti_zoomed_frame_size[1] + anti_zoomed_frame_margins[1]))

    frame = anti_zoomed_frame.resize(size, resample=Image.BILINEAR)

    print i, img.size, anti_zoomed_frame_size, anti_zoomed_frame_margins

    # frame No for debugging
    draw = ImageDraw.Draw(frame)
    draw.text((size[0] - 200, size[1] - 100), str(i), (0,0,0), font=font)

    yield frame

```

So what happens is I crop out of the source image a rectangle of size `frame_size / zoom` , then I resize it `zoom` times to get the needed frame size. `zoom <= 1.0` so doing it in this order seems to be a good idea, though I also tried doing it the other way (resize, then crop).

Anyway in both cases the resulting video is kind of shaking at times. It seems that the problem is in rounding the numbers. The math I'm doing inevitably leads to non-integers, yet PIL requires integers everywhere (also the resulting frame size is obviously a pair of integer constants). This rounding operation seems to affect the smoothness of the animation badly.

How smooth zooming is implemented in real life?

**Tags:** animation

> **Comments:**
> **username_1**: I'm voting to close this question as off-topic because it doesn't seem to be about computer graphics, but rather video/image/signal processing.  
> **username_2**: I think it's an interesting question. Essentially temporal aliasing problem which is too often ignored by CG people. There are a huge number of algorithms out there which look cool on still frames but break down when you start animating input parameters.  
> **username_2**: I meant quantization, not aliasing, but I still think it's worth discussing why this fails to be smooth and what can be done about it besides the obvious "get a toolkit with the proper floating point zoom support".  
> **username_3**: We have a [tag for image-processing](http://computergraphics.stackexchange.com/questions/tagged/image-processing), and there is support for the place of image processing in our scope on Meta [here](http://meta.computergraphics.stackexchange.com/questions/38/is-this-site-only-about-3d-topics) and [here](http://meta.computergraphics.stackexchange.com/questions/9/how-do-we-draw-the-line-between-questions-that-are-appropriate-for-computer-grap).  

---

## 0 answers

