def save_as(cls, figure_or_data, filename, format=None, width=None,
height=None, scale=None):
"""Save a image of the plot described by `figure_or_data` locally as
`filename`.
Valid image formats are 'png', 'svg', 'jpeg', and 'pdf'.
The format is taken as the extension of the filename or as the
supplied format.
positional arguments:
- figure_or_data: The figure dict-like or data list-like object that
describes a plotly figure.
Same argument used in `py.plot`, `py.iplot`,
see https://plot.ly/python for examples
- filename: The filepath to save the image to
- format: 'png', 'svg', 'jpeg', 'pdf'
- width: output width
- height: output height
- scale: Increase the resolution of the image by `scale` amount
Only valid for PNG and JPEG images.
example:
import plotly.plotly as py
fig = {'data': [{'x': [1, 2, 3], 'y': [3, 1, 5], 'type': 'bar'}]}
py.image.save_as(fig, 'my_image.png', scale=3)
```
"""
# todo: format shadows built-in name
(base, ext) = os.path.splitext(filename)
if not ext and not format:
filename += '.png'
elif ext and not format:
format = ext[1:]
elif not ext and format:
filename += '.' + format
img = cls.get(figure_or_data, format, width, height, scale)
f = open(filename, 'wb')
f.write(img)
f.close()
```