1

Let's draw a line and a circle:

canvas.create_line(x0,y0,x1,y1)
canvas.create_oval(x0,y0,x1,y1)

How to count the number of pixels each one of them has ?

  • not sure what you mean but i think that's just limited by your screen resolution – Julien Spronck May 07 '15 at 14:50
  • @JulienSpronck I want to count the number of pixels the line and the oval have –  May 07 '15 at 14:54
  • 1
    Do you want an exact value, or is an approximation okay? In the latter case, just calculate the circumference. Otherwise, how to deal with anti-aliased pixels and stuff? – tobias_k May 07 '15 at 14:56

1 Answers1

1

You can't. Tkinter gives you no way to get that information.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Couldn't you convert the canvas to an image, then load the image and iterate through the pixels, counting them if they're a certain color? http://stackoverflow.com/a/10432892/2415524 – mbomb007 Feb 06 '17 at 15:30
  • @mbomb007: yes you can, but there's no way in tkinter to convert a canvas to an image. You can only export a canvas as postscript. You would need an external tool to covert the postscript to an image, and it's quite possible that the resulting image has more or less pixels than the actual canvas item. – Bryan Oakley Feb 06 '17 at 17:24
  • @mbomb007: I think you proved my point. You have to convert the canvas to postscript, then the postscript to an image. You have to use a separate library (in this case, PIL) to convert the postscript to an image. When I run the code in the answer you pointed to, the resulting image is definitely smaller than the image in my canvas. Counting the pixels in the resulting image will yield a different result than the number of pixels used to generate the image. – Bryan Oakley Feb 06 '17 at 17:56
  • PIL is the standard way to create images in Python. Any solution for creating an image would be *expected* to use PIL. You were referring to a special library to handle postscript. That's not what PIL is. – mbomb007 Feb 06 '17 at 19:36
  • You are missing the point. Regardless if PIL is special or standard, the fact remains you have to use something other than Tkinter, and the image you end up with is not the same as the image in the canvas. Counting pixels of the resulting image won't give you a correct answer. – Bryan Oakley Feb 06 '17 at 19:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/135002/discussion-between-mbomb007-and-bryan-oakley). – mbomb007 Feb 06 '17 at 21:40