tanyaschlusser / array2gif Goto Github PK
View Code? Open in Web Editor NEWProvides a function to write a NumPy array to a GIF, or a list of arrays to an animated GIF.
License: BSD 3-Clause "New" or "Revised" License
Provides a function to write a NumPy array to a GIF, or a list of arrays to an animated GIF.
License: BSD 3-Clause "New" or "Revised" License
I have an array of shape (60, 3, 428, 1184) in the format (frame, rgb, row, column)
When I do
write_gif([np.squeeze(big[ii,:,:,:]) for ii in range(n_frames)], 'video.gif', fps=30)
I get the error
---------------------------------------------------------------------------
error Traceback (most recent call last)
<ipython-input-50-497aa6684826> in <module>()
1 from array2gif import write_gif
2
----> 3 write_gif([np.squeeze(big[ii,:,:,:]) for ii in range(n_frames)], 'echo_filter.gif', fps=30)
/opt/virtualenvs/echo_outcome/lib/python3.6/site-packages/array2gif/core.py in write_gif(dataset, filename, fps)
395 with open(filename, 'wb') as outfile:
396 outfile.write(HEADER)
--> 397 for block in encode(dataset):
398 outfile.write(block)
399 outfile.write(TRAILER)
/opt/virtualenvs/echo_outcome/lib/python3.6/site-packages/array2gif/core.py in _make_animated_gif(datasets, delay_time)
345
346 def _make_animated_gif(datasets, delay_time=10):
--> 347 images = [get_image(d) for d in datasets]
348 color_sets = (get_colors(image) for image in images)
349 colors = Counter()
/opt/virtualenvs/echo_outcome/lib/python3.6/site-packages/array2gif/core.py in <listcomp>(.0)
345
346 def _make_animated_gif(datasets, delay_time=10):
--> 347 images = [get_image(d) for d in datasets]
348 color_sets = (get_colors(image) for image in images)
349 colors = Counter()
/opt/virtualenvs/echo_outcome/lib/python3.6/site-packages/array2gif/core.py in get_image(dataset)
104 )
105 for j in range(ncol)]
--> 106 for i in range(nrow)]
107 return image
108
/opt/virtualenvs/echo_outcome/lib/python3.6/site-packages/array2gif/core.py in <listcomp>(.0)
104 )
105 for j in range(ncol)]
--> 106 for i in range(nrow)]
107 return image
108
/opt/virtualenvs/echo_outcome/lib/python3.6/site-packages/array2gif/core.py in <listcomp>(.0)
103 dataset[2, i, j]
104 )
--> 105 for j in range(ncol)]
106 for i in range(nrow)]
107 return image
error: required argument is not an integer
Hey, I'm using this for my work. I really like how simple this library is, but it takes like 5 minutes to render my gifs because only one core is being used (at 100%). If this could be improved it would make my life much easier.
How do I change the size (height and width) of the gif file? The file created by default is extremely small. I have used a 20X20 matrix.
"The maximum number of distinct colors in a GIF is 256 but this image has 4896 colors and can't be encoded properly."
I have printed my ndarray, and all the values in the array are the integers between 0 and 255, I don't understand why it always give me this error? my array shape is [3,128,128,32]. Please help, thanks
Currently, write_gif
supports RGB only, it would be nice to have support for grayscale to avoid adding a new axis with zeros
I'm working on rendering some very long gifs that take 10s of minutes to finish. Including some sort of progress bar for would be super helpful for that.
This API looks fairly simple to use & convenient but could you consider adding one small convenience feature?
I tried inputting a 4d numpy array and got the following:
Processing /home/cpaxton/.costar/data/2018-04-24-14-19-15_example000001.failure.h5f: 0%| | 0/454 [00:00<?, ?it/s]Traceback (most recent call last):
File "scripts/make_gif.py", line 78, in <module>
main(args)
File "scripts/make_gif.py", line 74, in main
write_gif(images, gif_filename, fps=10)
File "/home/cpaxton/.local/lib/python2.7/site-packages/array2gif/core.py", line 361, in write_gif
check_dataset(dataset)
File "/home/cpaxton/.local/lib/python2.7/site-packages/array2gif/core.py", line 56, in check_dataset
check_dataset_shape(dataset)
File "/home/cpaxton/.local/lib/python2.7/site-packages/array2gif/core.py", line 45, in check_dataset_shape
raise ValueError('The dataset needs 3 dimensions: rgb, nrows, ncols')
ValueError: The dataset needs 3 dimensions: rgb, nrows, ncols
Exception KeyError: KeyError(<weakref at 0x7fe647f1fd08; to 'tqdm' at 0x7fe6634d8910>,) in <bound method tqdm.__del__ of Processing /home/cpaxton/.costar/data/2018-04-24-14-19-15_example000001.failure.h5f: 0%| | 0/454 [00:00<?, ?it/s]> ignored
cpaxton@babylon:~/catkin_ws/src/costar_plan/ctp_integration$
I then tried a list of 3d numpy arrays with jpeg images decoded into numpy arrays in the standard format output by pillow (PIL) and got the following:
1.failure.h5f: 0%| | 0/453 [00:00<?, ?it/s]Traceback (most recent call last):
File "scripts/make_gif.py", line 78, in <module>
main(args)
File "scripts/make_gif.py", line 74, in main
write_gif(images, gif_filename, fps=10)
File "/home/cpaxton/.local/lib/python2.7/site-packages/array2gif/core.py", line 361, in write_gif
check_dataset(dataset)
File "/home/cpaxton/.local/lib/python2.7/site-packages/array2gif/core.py", line 71, in check_dataset
.format(err, i)
ValueError: The dataset's first dimension must have all 3
Here is some code you can use to test if you have two jpegs lying around:
def JpegToNumpy(jpeg):
stream = io.BytesIO(jpeg)
im = Image.open(stream)
return np.asarray(im, dtype=np.uint8)
def ConvertJpegListToNumpy(data, format='numpy'):
""" Convert a list of binary jpeg files to numpy format.
# Arguments
data: a list of binary jpeg images to convert
format: default 'numpy' returns a 4d numpy array,
'list' returns a list of 3d numpy arrays
"""
length = len(data)
imgs = []
for raw in data:
imgs.append(JpegToNumpy(raw))
if format == 'numpy':
imgs = np.array(imgs)
return imgs
Is there any chance you can add support for:
list of 3d numpy arrays in the default pil format (HWC)
4d numpy formats
Each of those is probably only a couple lines, plus perhaps a data_format
parameter.
I was trying to open this gif using your library, with this code:
frames, details, NA = gif2numpy.convert(options['to_create'])
'to_create' is just the path to the gif.
This is the error I got:
Traceback (most recent call last):
File "./get_images.py", line 448, in
create_image()
File "./get_images.py", line 410, in create_image
frames, details, NA = gif2numpy.convert(options['to_create']) #just gets the frames in numpy array
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/gif2numpy.py", line 631, in convert
transp_idx = color_table[exts[-1]['transparent_idx']][::-1] # RGB -> BGR
IndexError: list index out of range
How can I get around this issue?
This reproduces:
import numpy as np
from array2gif import write_gif
np.random.seed(42)
x = 255 * np.random.rand(10, 3, 5, 6) # 300 distinct colors
write_gif(x, 'out.gif')
Which yields:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/~path~/array2gif/core.py", line 410, in write_gif
for block in encode(dataset):
File "/~path~/array2gif/core.py", line 365, in _make_animated_gif
yield _get_logical_screen_descriptor(images[0], colors)
File "/~path~/array2gif/core.py", line 163, in _get_logical_screen_descriptor
pixel_aspect_ratio
struct.error: ubyte format requires 0 <= number <= 255
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.