Skip to content

Commit

Permalink
Make video saving for plots lossless
Browse files Browse the repository at this point in the history
  • Loading branch information
frankong committed Jul 31, 2019
1 parent a905501 commit e432fc9
Showing 1 changed file with 19 additions and 42 deletions.
61 changes: 19 additions & 42 deletions sigpy/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
$\bf{[/]:}$ change brightness.
$\bf{\{/\}:}$ change contrast.
$\bf{s:}$ save as png.
$\bf{g/v:}$ save as gif/mp4 by along current axis.
$\bf{g/v:}$ save as gif/video by along current axis.
$\bf{q:}$ refresh.
$\bf{0-9:}$ enter slice number.
$\bf{enter:}$ set current axis as slice number.
Expand All @@ -60,8 +60,8 @@ class ImagePlot(object):
and log mode. {'m', 'p', 'r', 'i', 'l'}.
title (str): title.
interpolation (str): plot interpolation.
save_basename (str): saved png, gif, and mp4 base name.
fps (int): frame per seconds for gif and mp4.
save_basename (str): saved png, gif, and video base name.
fps (int): frame per seconds for gif and video.
"""
def __init__(
Expand Down Expand Up @@ -288,7 +288,7 @@ def key_press(self, event):
'-s',
'{}x{}'.format(int(bbox.width * self.fig.dpi),
int(bbox.height * self.fig.dpi)),
'-r',
'-framerate',
str(self.fps),
'-i',
'{} %05d.png'.format(temp_basename),
Expand All @@ -299,7 +299,7 @@ def key_press(self, event):
'-s', '{}x{}'.format(
int(bbox.width * self.fig.dpi),
int(bbox.height * self.fig.dpi)),
'-r', str(self.fps),
'-framerate', str(self.fps),
'-i', '{} %05d.png'.format(temp_basename),
'-i', '{} palette.png'.format(temp_basename),
'-lavfi', 'paletteuse', filename])
Expand All @@ -311,7 +311,7 @@ def key_press(self, event):
elif event.key == 'v':
filename = self.save_basename + \
datetime.datetime.now().strftime(
' %Y-%m-%d at %I.%M.%S %p.mp4')
' %Y-%m-%d at %I.%M.%S %p.mov')
temp_basename = uuid.uuid4()

bbox = self.fig.get_tightbbox(self.fig.canvas.get_renderer())
Expand All @@ -325,16 +325,9 @@ def key_press(self, event):
format='png', bbox_inches=bbox, pad_inches=0)

subprocess.run(['ffmpeg',
'-f', 'image2',
'-s', '{}x{}'.format(
int(bbox.width * self.fig.dpi),
int(bbox.height * self.fig.dpi)),
'-r', str(self.fps),
'-framerate', str(self.fps),
'-i', '{} %05d.png'.format(temp_basename),
'-vf', 'scale=trunc(iw/2)*2:trunc(ih/2)*2',
'-vcodec', 'libx264',
'-preset', 'veryslow',
'-pix_fmt', 'yuv420p',
'-vcodec', 'png',
filename])

for i in range(self.shape[self.d]):
Expand Down Expand Up @@ -572,7 +565,7 @@ class LinePlot(object):
l: log mode
s: save as png.
g: save as gif by traversing current dimension.
v: save as mp4 by traversing current dimension.
v: save as video by traversing current dimension.
"""

def __init__(self, arr, x=-1, hide_axes=False, mode='m', title='',
Expand Down Expand Up @@ -701,7 +694,7 @@ def key_press(self, event):
'-s',
'{}x{}'.format(int(bbox.width * self.fig.dpi),
int(bbox.height * self.fig.dpi)),
'-r',
'-framerate',
str(self.fps),
'-i',
'{} %05d.png'.format(temp_basename),
Expand All @@ -712,7 +705,7 @@ def key_press(self, event):
'-s', '{}x{}'.format(
int(bbox.width * self.fig.dpi),
int(bbox.height * self.fig.dpi)),
'-r', str(self.fps),
'-framerate', str(self.fps),
'-i', '{} %05d.png'.format(temp_basename),
'-i', '{} palette.png'.format(temp_basename),
'-lavfi', 'paletteuse', filename])
Expand All @@ -724,7 +717,7 @@ def key_press(self, event):
elif event.key == 'v':
filename = self.save_basename + \
datetime.datetime.now().strftime(
' %Y-%m-%d at %h.%M.%S %p.mp4')
' %Y-%m-%d at %h.%M.%S %p.mov')
temp_basename = uuid.uuid4()

bbox = self.fig.get_tightbbox(self.fig.canvas.get_renderer())
Expand All @@ -738,16 +731,9 @@ def key_press(self, event):
format='png', bbox_inches=bbox, pad_inches=0)

subprocess.run(['ffmpeg',
'-f', 'image2',
'-s', '{}x{}'.format(
int(bbox.width * self.fig.dpi),
int(bbox.height * self.fig.dpi)),
'-r', str(self.fps),
'-framerate', str(self.fps),
'-i', '{} %05d.png'.format(temp_basename),
'-vf', 'scale=trunc(iw/2)*2:trunc(ih/2)*2',
'-vcodec', 'libx264',
'-preset', 'veryslow',
'-pix_fmt', 'yuv420p',
'-vcodec', 'png',
filename])

for i in range(self.shape[self.d]):
Expand Down Expand Up @@ -992,7 +978,7 @@ def key_press(self, event):
'-s',
'{}x{}'.format(int(bbox.width * self.fig.dpi),
int(bbox.height * self.fig.dpi)),
'-r',
'-framerate',
str(self.fps),
'-i',
'{} %05d.png'.format(temp_basename),
Expand All @@ -1003,7 +989,7 @@ def key_press(self, event):
'-s', '{}x{}'.format(
int(bbox.width * self.fig.dpi),
int(bbox.height * self.fig.dpi)),
'-r', str(self.fps),
'-framerate', str(self.fps),
'-i', '{} %05d.png'.format(temp_basename),
'-i', '{} palette.png'.format(temp_basename),
'-lavfi', 'paletteuse', filename])
Expand All @@ -1015,7 +1001,7 @@ def key_press(self, event):
elif event.key == 'v':
filename = self.save_basename + \
datetime.datetime.now().strftime(
' %Y-%m-%d at %h.%M.%S %p.mp4')
' %Y-%m-%d at %h.%M.%S %p.mov')
temp_basename = uuid.uuid4()

bbox = self.fig.get_tightbbox(self.fig.canvas.get_renderer())
Expand All @@ -1029,21 +1015,12 @@ def key_press(self, event):
format='png', bbox_inches=bbox, pad_inches=0)

subprocess.run(['ffmpeg',
'-f',
'image2',
'-s',
'{}x{}'.format(int(bbox.width * self.fig.dpi),
int(bbox.height * self.fig.dpi)),
'-r',
'-framerate',
str(self.fps),
'-i',
'{} %05d.png'.format(temp_basename),
'-vf',
"scale=trunc(iw/2)*2:trunc(ih/2)*2",
'-vcodec',
'libx264',
'-pix_fmt',
'yuv420p',
'png',
filename])

for i in range(self.shape[self.d]):
Expand Down

0 comments on commit e432fc9

Please sign in to comment.