question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve docs related to reading from bytes

See original GitHub issue

I’m trying to save bytes as frames of an animation of a plot but

import matplotlib.pyplot as plt
import imageio

plt.plot(1, 1)
fig = plt.gcf()
fig.canvas.draw()
data = fig.canvas.tostring_rgb() # These are bytes
imageio.imread(data)

File "<string>", line unknown
SyntaxError: cannot handle 76-bit layers

I also tried converting it to a numpy array of uint8s but imread just spits the data back into my terminal and doesn’t do anything. Is this the way to read in raw bytes? Is something missing from the docs?

Thanks

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
mef51commented, Mar 2, 2017

Ah okay, well I’ve figured it out! Just had to reshape the data a little bit before giving it to imageio. Here’s a little snippet that saves an animation:

import matplotlib.pyplot as plt
import numpy as np
import imageio
from numpy import sin, pi

print('Working...')
time = np.linspace(0, 10, 100)
with imageio.get_writer('test.gif', mode='I', fps=24) as writer:
	for t in time:
		x = np.linspace(-2*pi,2*pi, 100)
		plt.plot(x, (x/(2*pi))*sin(t+x))
		plt.grid()
		plt.xlim(-2*pi, 2*pi), plt.ylim(-1.2, 1.2)
		fig = plt.gcf()
		fig.canvas.draw()
		data = fig.canvas.tostring_rgb()
		rows, cols = fig.canvas.get_width_height()

		imarray = np.fromstring(data, dtype=np.uint8).reshape(cols, rows, 3)
		writer.append_data(imarray)
		plt.close()
print('Done.')

test

0reactions
almarkleincommented, Mar 2, 2017

Ah I see. I was confused by the imageio.imread(data). Glad you got it working. Nice animation!

Read more comments on GitHub >

github_iconTop Results From Across the Web

BinaryReader.ReadBytes(Int32) Method (System.IO)
Reads the specified number of bytes from the current stream into a byte array and advances the current position by that number of...
Read more >
Using readable byte streams - Web APIs | MDN
Readable byte streams are readable streams that have an underlying byte source of type: "bytes" , and which support efficient zero-copy ...
Read more >
Introduction to optimizing query performance | BigQuery
Optimization improves query speed and reduces cost. ... The query plan shows execution statistics such as bytes read and slot time consumed.
Read more >
Multilingual Language Processing From Bytes
rectly on unicode bytes rather than language- ... results similar to or better than the state-of- ... LSTM models are reading bytes, it...
Read more >
Encoding | Protocol Buffers - Google Developers
First you drop the MSB from each byte, as this is just there to tell us whether we've reached the end of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found