0

I'm working on a photo editor program on python called Photo Editor Max.But I've came across a problem. I need to be able to get the data off of the python canvas and transfer it into a png so that I can save images that people are creating. Now I was reading through some things in the internet trying to find a way on how to convert python canvas into a png file. And I've came across two solutions on how to do it. But I'm not too sure how to get some data for some of it's processes and how it works. So the first solution was: To get an RGB Array from python canvas then use the following code which I can implement into my program to save the image

from PIL import Image

newimage = Image.new('RGB', (len(rgbArray[0]), len(rgbArray)))  # type, size
newimage.putdata([tuple(p) for row in rgbArray for p in row])
newimage.save("filename.png")  # takes type from filename extension

The variable rgbArray is the RGB array that I've taken from the canvas. But I'm not too sure on how to achieve the data for this array. (Which will be like a string of values in brackets. Check : Convert an integer array to a PNG image in Python to know what I'm on about )

Now my second solution is to convert the things in the python canvas into bit64. Then maybe try to use that data to convert it into a png which is able to be saved. Example code :

canvas = document.getElementById("canvas"),
dataURL = canvas.toDataURL();

So please may I have some help with my problem? It'll do me wonders if you can help!

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • What do you mean by "python canvas"? Is it a Tkinter Canvas? BTW, that code in your last code block is JavaScript for extracting image data from a HTML5 canvas, which is a totally different thing. – PM 2Ring Dec 28 '17 at 15:12
  • If this _is_ a Tkinter Canvas then I'm afraid you're out of luck. The Tkinter Canvas widget doesn't provide a way to access the pixel data. Allegedly, there _is_ a hack which uses another library to do it, but it's slow. As Bryan mentions [here](https://stackoverflow.com/a/28015006/4014959), you'd be better off using a PhotoImage instead of a Canvas. – PM 2Ring Dec 28 '17 at 15:20
  • Yes it is a Tkinter canvas. And when I try install PIL for the full range of Photo Imaging stuff it doesn't work out well. So that's why I've been trying to use the Tkinter canvas. – Ambrose Odongkara Dec 28 '17 at 15:49
  • What does "it doesn't work out well" mean, specifically? I assume you're trying to install Pillow, not the ancient original PIL. Are you using pip to install PIL? What version of Python are you using? And which OS? – PM 2Ring Dec 28 '17 at 16:07

0 Answers0