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.

does getting image from same cell work?

See original GitHub issue

I have tried to get an image of the canvas from the same cell as described in the documentation. I tried the following code:

from ipycanvas import Canvas
import numpy as np

canvas = Canvas(width=200, height=200, sync_image_data=True)
display(canvas)

def get_array(*args, **kwargs):
    print("here")
    arr = canvas.get_image_data()
    print(arr)
    np.save("whatever.txt", arr)
    # Do something with arr

# Listen to changes on the ``image_data`` trait and call ``get_array`` when it changes.
canvas.observe(get_array, 'image_data')

canvas.stroke_line(50, 10, 150, 10)

Unfortunately nothing happens (except the canvas being drawn). Can you confirm that this works for you?

PS: What I am trying to achieve is creating a gif/video from an animation, which is still an open issue as far as I have seen. But I try it “by hand”.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Weihnachtshasecommented, Jul 6, 2021

This was actually the problem 😃

This code works now for me:

from ipycanvas import Canvas, hold_canvas
from threading import Thread
import numpy as np
import time


class Test:
    
    def __init__(self):
        self.first = True
        self.images = []
        self.canvas = None
        
    def get_array(self, *args, **kwargs):
        arr = self.canvas.get_image_data()
        self.images.append(arr)
        
    def render(self, x):
        if self.first:
            self.canvas.observe(self.get_array, 'image_data')
            self.first = False
        self.canvas.stroke_line(50, 10+(x*5), 150, 10+(x*5))
        
class MyThread(Thread):
    
    def __init__(self, test):
        super(MyThread, self).__init__()
        self.test = test
    
    def run(self):
        for i in range(10):
            self.test.render(i)
            time.sleep(1)
        

test = Test()
canvas = Canvas(width=200, height=200, sync_image_data=True)
display(canvas)
test.canvas = canvas
MyThread(test).start()

Hope that somehow helps you in the future with your GIF animation creation.

0reactions
martinRenoucommented, Jul 6, 2021

This is huge ahah. I haven’t thought of threading when trying the first time.

Thanks a lot for trying! This will definitely help. We would need to think of a way to expose this as a nice and friendly user-API.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Lookup Pictures in Excel based on a Cell Value - YouTube
Use Data Validation/Drop Down Lists or Slicers to lookup pictures or images in Excel. ... Your browser can 't play this video.
Read more >
Excel IMAGE function to insert picture in cell - Ablebits
With the newly introduced IMAGE function, you can insert a picture in a cell with a simple formula, place images within Excel tables,...
Read more >
How to change images based on cell values (3 ways)
Ultimately, the purpose is to link an image or picture to a cell; change the cell value and the picture automatically changes.
Read more >
Microsoft Excel: How to insert an image into a cell
A. Yes, you can insert an image into an Excel cell as follows. Paste an image into Excel, then resize the image and...
Read more >
Picture Lookup in Excel using Named Ranges
Learn how to use Named Ranges to do a Picture Lookup in Excel. This is a cool excel dashboard trick where pictures update...
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