Skip to content

Instantly share code, notes, and snippets.

@korakot
Last active May 17, 2024 03:38
Show Gist options
  • Save korakot/8409b3feec20f159d8a50b0a811d3bca to your computer and use it in GitHub Desktop.
Save korakot/8409b3feec20f159d8a50b0a811d3bca to your computer and use it in GitHub Desktop.
Drawing on Google Colab
from IPython.display import HTML, Image
from google.colab.output import eval_js
from base64 import b64decode
canvas_html = """
<canvas width=%d height=%d></canvas>
<button>Finish</button>
<script>
var canvas = document.querySelector('canvas')
var ctx = canvas.getContext('2d')
ctx.lineWidth = %d
var button = document.querySelector('button')
var mouse = {x: 0, y: 0}
canvas.addEventListener('mousemove', function(e) {
mouse.x = e.pageX - this.offsetLeft
mouse.y = e.pageY - this.offsetTop
})
canvas.onmousedown = ()=>{
ctx.beginPath()
ctx.moveTo(mouse.x, mouse.y)
canvas.addEventListener('mousemove', onPaint)
}
canvas.onmouseup = ()=>{
canvas.removeEventListener('mousemove', onPaint)
}
var onPaint = ()=>{
ctx.lineTo(mouse.x, mouse.y)
ctx.stroke()
}
var data = new Promise(resolve=>{
button.onclick = ()=>{
resolve(canvas.toDataURL('image/png'))
}
})
</script>
"""
def draw(filename='drawing.png', w=400, h=200, line_width=1):
display(HTML(canvas_html % (w, h, line_width)))
data = eval_js("data")
binary = b64decode(data.split(',')[1])
with open(filename, 'wb') as f:
f.write(binary)
return len(binary)
@Dsm0
Copy link

Dsm0 commented Apr 2, 2020

how would I add this to colab?

@criptovaldo
Copy link

how would I add this to colab?

!git clone https://gist.github.com/8409b3feec20f159d8a50b0a811d3bca.git

@rickkk856
Copy link

I git clone it on colab
and

%run /content/8409b3feec20f159d8a50b0a811d3bca/draw.py

nothing happens :S

@hypagedev
Copy link

I use your code and it could draw. However, the file write is incorrect because opencv couldn't load.

@ZhuoyueLyu
Copy link

ZhuoyueLyu commented Jul 16, 2021

I git clone it on colab
and

%run /content/8409b3feec20f159d8a50b0a811d3bca/draw.py

nothing happens :S

@rickkk856 You then need to call draw(),

so in summary, to run this in colab:

!git clone https://gist.github.com/8409b3feec20f159d8a50b0a811d3bca.git

%run /content/8409b3feec20f159d8a50b0a811d3bca/draw.py

draw(filename = "your_name_here.png", w=400, h=400, line_width=15)

Where you can specify thefilename, w(width), h(height) and line_width to whatever you want.

@rickkk856
Copy link

This gist was very useful for part of my project with Pix2Pix models, and I found that collab has some difficulties in making things interactive.
So I made a modified version of this project but with color buttons. Hope it can be useful for other people, just copy/paste on collab that will work.

https://gist.github.com/rickkk856/6a2800cc84dd8fd456074e5a467edc47

@Github-user-12
Copy link

Hello, this code was very useful for me.
However, you can't draw using your finger with a touchscreen, it only works with a mouse.
Is there a way to add this feature?
Thanks

@mmagrilov
Copy link

I use your code and it could draw. However, the file write is incorrect because opencv couldn't load.

Hi everybody!
If you want to load the drawing from the file, fill the canvas with some background color, white for example, before you start drawing:

var ctx = canvas.getContext('2d')
ctx.fillStyle = "#FFFFFF";
ctx.fillRect(0, 0, canvas.width, canvas.height);

It solved the problem for me, hope it'll help you too

@eugenechow5
Copy link

Hello, may I please ask how to load an image to the canvas first and then draw on it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment