|
62 | 62 | # how-to-encode-series-of-images-into-h264-using-x264-api-c-c )
|
63 | 63 |
|
64 | 64 |
|
65 |
| -def correct_roundoff(x, dpi, n): |
66 |
| - if int(x*dpi) % n != 0: |
67 |
| - if int(np.nextafter(x, np.inf)*dpi) % n == 0: |
68 |
| - x = np.nextafter(x, np.inf) |
69 |
| - elif int(np.nextafter(x, -np.inf)*dpi) % n == 0: |
70 |
| - x = np.nextafter(x, -np.inf) |
71 |
| - return x |
| 65 | +def adjusted_figsize(w, h, dpi, n): |
| 66 | + '''Compute figure size so that pixels are a multiple of n |
72 | 67 |
|
| 68 | + Parameters |
| 69 | + ---------- |
| 70 | + w, h : float |
| 71 | + Size in inches |
| 72 | +
|
| 73 | + dpi : float |
| 74 | + The dpi |
| 75 | +
|
| 76 | + n : int |
| 77 | + The target multiple |
| 78 | +
|
| 79 | + Returns |
| 80 | + ------- |
| 81 | + wnew, hnew : float |
| 82 | + The new figure size in inches. |
| 83 | + ''' |
| 84 | + |
| 85 | + # this maybe simplified if / when we adopt consistent rounding for |
| 86 | + # pixel size across the whole library |
| 87 | + def correct_roundoff(x, dpi, n): |
| 88 | + if int(x*dpi) % n != 0: |
| 89 | + if int(np.nextafter(x, np.inf)*dpi) % n == 0: |
| 90 | + x = np.nextafter(x, np.inf) |
| 91 | + elif int(np.nextafter(x, -np.inf)*dpi) % n == 0: |
| 92 | + x = np.nextafter(x, -np.inf) |
| 93 | + return x |
73 | 94 |
|
74 |
| -def adjusted_figsize(w, h, dpi, n): |
75 | 95 | wnew = int(w * dpi / n) * n / dpi
|
76 | 96 | hnew = int(h * dpi / n) * n / dpi
|
77 | 97 | return (correct_roundoff(wnew, dpi, n), correct_roundoff(hnew, dpi, n))
|
@@ -350,6 +370,9 @@ def grab_frame(self, **savefig_kwargs):
|
350 | 370 | verbose.report('MovieWriter.grab_frame: Grabbing frame.',
|
351 | 371 | level='debug')
|
352 | 372 | try:
|
| 373 | + # re-adjust the figure size in case it has been changed by the |
| 374 | + # user. We must ensure that every frame is the same size or |
| 375 | + # the movie will not save correctly. |
353 | 376 | self.fig.set_size_inches(self._w, self._h)
|
354 | 377 | # Tell the figure to save its data to the sink, using the
|
355 | 378 | # frame format and dpi.
|
|
0 commit comments