Looking for a nice graphics package for Python

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

Aldarion
Posts: 133
Joined: Thu Jul 19, 2007 7:00 pm UTC

Looking for a nice graphics package for Python

Postby Aldarion » Mon May 09, 2011 4:37 pm UTC

I've stumbled upon (no, not Stumbled Upon, just ran across) this Instructable for making a paper Enigma-like machine.

Now, the author doesn't enclose a program to make your own rotors, only two sets of premade ones, and of course, I'd like to have rotors with another, unique wiring. So now I want to write a program that can produce something like the attached picture.

I'm used to working with PIL for making images, but I think this kind of job would be too daunting to make with PIL; I probably need something more high-level for it.

Any suggestions?
Attachments
Reflector.png
Reflector.png (77.44 KiB) Viewed 2104 times
Rotor.png
Last edited by Aldarion on Wed May 11, 2011 6:33 pm UTC, edited 1 time in total.
I'm not good, I'm not nice, I'm just right.

User avatar
thoughtfully
Posts: 2253
Joined: Thu Nov 01, 2007 12:25 am UTC
Location: Minneapolis, MN
Contact:

Re: Lookign for a nice graphics package for Python

Postby thoughtfully » Mon May 09, 2011 7:14 pm UTC

It doesn't look like the usual gang of plotting libraries is what you want. You probably want a canvas type drawing primitive. It'll be sorta low-level, though. All the GUI toolkits have one. You might also try pycairo, but you'll still need a GUI toolkit (although I imagine you can output to image files directly somehow). pyGTK uses a lot of cairo under the hood, and there's some support for directly using it. I've never tried that, although I've used the canvas.

Old versions of Python included a module called "rotor" that implemented enigma-like encryption. I'm sure it's around somewhere as an add-on. Try PyPI.
Image
Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.
-- Antoine de Saint-Exupery

User avatar
diabolo
Posts: 72
Joined: Fri Aug 08, 2008 4:17 pm UTC
Location: france

Re: Lookign for a nice graphics package for Python

Postby diabolo » Mon May 09, 2011 9:47 pm UTC

I think you're good with Cairo only, and it can do vector output (svg/pdf) so it's pretty interesting for something you're going to print.
Attachments
rnd-wiring.pdf
I had some time to waste testing
(10.23 KiB) Downloaded 82 times

Aldarion
Posts: 133
Joined: Thu Jul 19, 2007 7:00 pm UTC

Re: Lookign for a nice graphics package for Python

Postby Aldarion » Tue May 10, 2011 10:41 am UTC

That looks excellent, diabolo!

But I've got a problem installing Cairo... is it the only graphics package for Python?
I'm not good, I'm not nice, I'm just right.

User avatar
diabolo
Posts: 72
Joined: Fri Aug 08, 2008 4:17 pm UTC
Location: france

Re: Lookign for a nice graphics package for Python

Postby diabolo » Tue May 10, 2011 11:15 am UTC

Aldarion wrote:But I've got a problem installing Cairo... is it the only graphics package for Python?

I'm not an expert in python so I can't suggest any other package right now but I know one approach that would work in any language would be to output SVG data.
SVG is XML code so you don't need anything special to produce it. Once you got your svg files you can convert them to some other format or include them directly onto your latex document to print or whatever.
I've also heard of pySVG which is apparently a bit higher-level than working with XML code directly but I never used it.

Aldarion
Posts: 133
Joined: Thu Jul 19, 2007 7:00 pm UTC

Re: Lookign for a nice graphics package for Python

Postby Aldarion » Tue May 10, 2011 12:51 pm UTC

Thanks again!
But I think I'll just edit it by hand in Photoshop; it's too much bother to study a new library or language (in case of XML) to make a single image.
I'm not good, I'm not nice, I'm just right.

rflrob
Posts: 235
Joined: Wed Oct 31, 2007 6:45 pm UTC
Location: Berkeley, CA, USA, Terra, Sol
Contact:

Re: Lookign for a nice graphics package for Python

Postby rflrob » Wed May 11, 2011 1:36 am UTC

Aldarion wrote:Thanks again!
But I think I'll just edit it by hand in Photoshop; it's too much bother to study a new library or language (in case of XML) to make a single image.


What kind of attitude is that? Unless you've got time constraints, it's always good to learn powerful, general-purpose libraries/languages. Hell, I'm tempted to try and figure out how to do it now.

There are other graphics libraries in Python (I'm a recovering Matlab user, so matplotilb is my bread and butter plotting package), but I'm not familiar with any other general-purpose graphics libraries. Doesn't mean they aren't out there, though.
Ten is approximately infinity (It's very large)
Ten is approximately zero (It's very small)

Aldarion
Posts: 133
Joined: Thu Jul 19, 2007 7:00 pm UTC

Re: Lookign for a nice graphics package for Python

Postby Aldarion » Wed May 11, 2011 7:07 am UTC

[quote="rflrob"
What kind of attitude is that? Unless you've got time constraints, it's always good to learn powerful, general-purpose libraries/languages. Hell, I'm tempted to try and figure out how to do it now.
[/quote]

True. So I'm taking a combined approach: The reflector I already rewired by hand in Inkscape, and to change the rotors I'll try today to edit the SVG code. A nice text-processing problem it will be!
I'm not good, I'm not nice, I'm just right.

User avatar
diabolo
Posts: 72
Joined: Fri Aug 08, 2008 4:17 pm UTC
Location: france

Re: Lookign for a nice graphics package for Python

Postby diabolo » Wed May 11, 2011 7:59 am UTC

Aldarion wrote:True. So I'm taking a combined approach: The reflector I already rewired by hand in Inkscape, and to change the rotors I'll try today to edit the SVG code. A nice text-processing problem it will be!

Editing SVG as a text-processing problem? as in parsing-xml-using-regex?

Is there any reason not to generate everything from scratch? The actual drawing is pretty simple once you have the characters arrays for the rotors. I don't know how they are generated, is that difficult? I guess since you're going to make your own you know how to do it and therefore you have already solved the most important part of the problem.
If you have the code to generate the rotors characters and the wiring I believe it would be easier (and better) to generate everything at once from the same program than to modify each part.
"I drew and printed the cutout for an enigma machine", cool.
"... actually I made it draw itself, and I can now produce the cutout for any possible enigma machine as a ready to print PDF file on the push of a button", waaaaaaaaaaay cooler, even if you ever build only one.


By the way, I checked pySVG after posting my previous answer and it turns out you don't have to deal with the underlying XML, the code looks like:

Code: Select all

svgdoc = svg()
sp = ShapeBuilder()
svgdoc.addElement(sp.createCircle(x, y, r, stroke="black"))
svgdoc.save('circle.svg')

Aldarion
Posts: 133
Joined: Thu Jul 19, 2007 7:00 pm UTC

Re: Lookign for a nice graphics package for Python

Postby Aldarion » Wed May 11, 2011 6:33 pm UTC

Thanks for the encouragement, guys! It's done.

Used pySVG; it took time (mostly because the tutorial is no longer right), but I did it.
It's not really neat — every rotor is generated separately, and I can't get the arcs on the reflector to be of different size, — but it definitely works. Also, to make it more Enigmaform, I made three rotors, not two.
I'm not good, I'm not nice, I'm just right.

User avatar
diabolo
Posts: 72
Joined: Fri Aug 08, 2008 4:17 pm UTC
Location: france

Re: Lookign for a nice graphics package for Python

Postby diabolo » Wed May 11, 2011 8:01 pm UTC

Aldarion wrote:Thanks for the encouragement, guys! It's done.

Used pySVG; it took time (mostly because the tutorial is no longer right), but I did it.

Nice.
About the outdated tutorial (the pdf one, first result on google?), this one works, a bit late now but you'll know for your next project.

Aldarion wrote:I can't get the arcs on the reflector to be of different size

If by different size you mean making them go more or less close to the center here's how I did it in my earlier example:
The arcs go closest to the center when their end points form a right angle (with regards to the center).
You have the 2 end points in polar coordinates (a,r) and (b,r), and the arc is a (cubic) curve between those endpoints via the control points (a,r') and (b,r'), where r' is computed so that it goes from 0 to r as the angle goes from pi to 0.
I'm not sure how much sense my explanation makes so here's a simple case with the "handles" on the curves.
Attachments
wiring-arcs.pdf
(1.68 KiB) Downloaded 52 times

Aldarion
Posts: 133
Joined: Thu Jul 19, 2007 7:00 pm UTC

Re: Looking for a nice graphics package for Python

Postby Aldarion » Wed May 11, 2011 8:40 pm UTC

Thanks! I'll use the tutorial next time (if there is one).

And about the control points - that's a nice way of doing it! I tried using the difference between their numbers (points 12 and 17 are close, 13 and 33 are farther etc.), but I bumped into the circle caveat: 70 and 1 are close, but go figure that one comfortably!

So yes, I'll try and adapt the angle trick for my code.
I'm not good, I'm not nice, I'm just right.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 3 guests