Does PIL ImageDraw create shape objects or stain pixels?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Does PIL ImageDraw create shape objects or stain pixels?

Jeff-268
Hi,

I am working on code to draw tens or hundreds of thousands of squares and connecting lines using the tkinter canvas, and it is incredibly slow because, as I understand it, the canvas keeps the individual shape objects around in memory and tests for the current visible bounding box to touch them, and redraws them.

I am wondering whether PIL's ImageDraw module behaves in this manner, or whether it instead just paints the shapes onto a raw pixel buffer, effectively "staining" the pixels, rather than holding onto thousands of identical objects. I assume that this would be much faster if it is the case.

Thanks for your help,
Jeff

_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: Does PIL ImageDraw create shape objects or stain pixels?

jumpfroggy
AFAIK, ImageDraw is a raster-based library, not object or vector
based.  So I don't think any objects are kept around, and changes
simply happen to a buffer.  I could be wrong.

That's not to say there aren't other performance issues to watch out
for.  But I don't imagine that simply drawing a ton of shapes will
overload memory in ImageDraw like it would in a lib that saves object
information.

-James
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: Does PIL ImageDraw create shape objects or stain pixels?

Edward Cannon
I have written code that draws maybe 10K objects (circles) on a single
canvas. No real performance issues there at all, at least not in
regards to memory. The bottleneck in my program was another part of
the code, not drawing the shapes so I can't be positive that it is not
slow to draw, but it didn't seem so to me.

On Wed, Nov 2, 2011 at 11:42 AM, James <[hidden email]> wrote:

> AFAIK, ImageDraw is a raster-based library, not object or vector
> based.  So I don't think any objects are kept around, and changes
> simply happen to a buffer.  I could be wrong.
>
> That's not to say there aren't other performance issues to watch out
> for.  But I don't imagine that simply drawing a ton of shapes will
> overload memory in ImageDraw like it would in a lib that saves object
> information.
>
> -James
> _______________________________________________
> Image-SIG maillist  -  [hidden email]
> http://mail.python.org/mailman/listinfo/image-sig
>
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: Does PIL ImageDraw create shape objects or stain pixels?

Jeff-268
Thank you both for the feedback. In the meantime, I made some major changes to my drawing algorithm that resulted in about a 20X reduction in width and probably a similar factor reduction in the number of line objects drawn. Now the tk canvas performance problems are virtually gone, so it seems I can continue prototyping with it.

But it is nice to know in future if the drawing size scales up again by an order of magnitude, that ImageDraw may be the answer.

Thanks again,
Jeff

On Wed, Nov 2, 2011 at 4:35 PM, Edward Cannon <[hidden email]> wrote:
I have written code that draws maybe 10K objects (circles) on a single
canvas. No real performance issues there at all, at least not in
regards to memory. The bottleneck in my program was another part of
the code, not drawing the shapes so I can't be positive that it is not
slow to draw, but it didn't seem so to me.

On Wed, Nov 2, 2011 at 11:42 AM, James <[hidden email]> wrote:
> AFAIK, ImageDraw is a raster-based library, not object or vector
> based.  So I don't think any objects are kept around, and changes
> simply happen to a buffer.  I could be wrong.
>
> That's not to say there aren't other performance issues to watch out
> for.  But I don't imagine that simply drawing a ton of shapes will
> overload memory in ImageDraw like it would in a lib that saves object
> information.
>
> -James
> _______________________________________________
> Image-SIG maillist  -  [hidden email]
> http://mail.python.org/mailman/listinfo/image-sig
>


_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: Does PIL ImageDraw create shape objects or stain pixels?

Chris Barker
In reply to this post by Jeff-268
On 10/26/11 2:49 PM, Jeff Brantley wrote:
> I am working on code to draw tens or hundreds of thousands of squares
> and connecting lines using the tkinter canvas, and it is incredibly slow
> because, as I understand it, the canvas keeps the individual shape
> objects around in memory and tests for the current visible bounding box
> to touch them, and redraws them.

That may or not be why it's slow...

> I am wondering whether PIL's ImageDraw module behaves in this manner, or
> whether it instead just paints the shapes onto a raw pixel buffer,
> effectively "staining" the pixels, rather than holding onto thousands of
> identical objects.

you are right -- PIL just does the drawing, and is not any kind of
object canvas.

>I assume that this would be much faster if it is the
> case.

perhaps, and perhaps not. Keeping the objects around per-se isn't
necessarily a slow process. But certainly PIL's drawing is simpler.

I've found PIL drawing to be pretty slow, but for your use case, it may
well be fine.

For higher quality, you may want to look into Agg-draw:

http://effbot.org/zone/pythondoc-aggdraw.htm

I don't know if it's been rolled into PIL or not.

Do you need a GUI of some sort? or are you just drawing an image to save
to a file or something?

-Chris









> Thanks for your help,
> Jeff
>
>
> _______________________________________________
> Image-SIG maillist  -  [hidden email]
> http://mail.python.org/mailman/listinfo/image-sig


--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

[hidden email]
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: Does PIL ImageDraw create shape objects or stain pixels?

Chris Barker
In reply to this post by Jeff-268
On 11/2/11 2:17 PM, Jeff Brantley wrote:
> Thank you both for the feedback. In the meantime, I made some major
> changes to my drawing algorithm that resulted in about a 20X reduction
> in width and probably a similar factor reduction in the number of line
> objects drawn. Now the tk canvas performance problems are virtually
> gone, so it seems I can continue prototyping with it.

Are you using TK for a GUI here? or just for drawing? If GUI, I'd stick
with it (or choose another GUI toolkit, wx drawing is pretty fast, or
even openGL if you need to quickly re-draw the same stuff at different
scales or views.

If you're not making a GUI app and just need drawing, I'd go with a
drawing API -- PIL, or maybe Kiva or Cairo or something.

-Chris



--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

[hidden email]
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig