wxPython: Drawing a vector-based image from file - python

How can I draw a vector-based image from a file in wxPython? I know nothing of image formats for such a thing, so please recommend.

You can do this with using cairo & librsvg python bindings. There is a small example here.

I have done something similar, I had a custom vector image format that I needed to render in a wxPython window. In order to accomplish this I used the GDI interface for drawing commands and I wrote my own parser module. There is a great GDI tutorial at this site that should help you out: http://www.zetcode.com/wxpython/gdi/

Related

Create a basic Python interface to save images drawn with a mouse

I am working on the following project and I am having really difficulties in finding the right way of doing that. I would like to build in Python (but I am open to other possibilities) a very basic interface that allows the user to draw with the mouse (or the pen if used on a surface laptop) something and then save the image. Ideally I would like this to work on a website or at least in a jupyter notebook (at least I imagine this to be utterly difficult).
Anyone can point me in the right direction? The goal would be to use the images as input to a neural network model to demonstrate its result with real life examples.
I am looking at tk but I don't seem to find much in terms of examples.
Thanks in advance, Umberto
I'd take a look at pyautogui to capture the mouse location then "draw" it in matplotlib -- should be able to do this in a loop. You'll want to watch the tkinter window size to sync the mouse coordinates with the relative location.
Why not just have your script open create a new blank img and automatically open it with paint - then read it on close? Seems easier than creating a drawing GUI.
Have a look at my Github repository which have exactly what you need.
Link : CanvasDraw Repo
Depending on the complexity you could either use tkinter which is a package for complex GUIs or something from the gaming community like pygames. You have user input and graphical output so libraries made for games will do what you want but provide way more stuff then you need. This site might help you: Drawing Libarys
Also the answere draw-on-python-tkinter-canvas-using-mouse-and-obtain-points-to-a-list might help you.

Python Graphviz Editor or GUI Framework

I have written a python application which generates output in graphviz DOT format. I convert the generated file into PNG using the external 'dot' utility and open the generated PNG in an image viewer to visualise the graph.
My application is an interactive command-line tool which allows few operations on the graph like changing the colours of edges based on the edge weights, deleting few nodes, etc. After these operations I generate the DOT file again and convert it to PNG for visualisation.
I came across xdot.py which allows me to embed the interactive viewer into my application. Now I do not have to go to the shell to generate the PNG every time.
I want to take this idea further and build a full-fledged GUI application on top of this. I have few questions regarding this:
Do I have to start from scratch (use wxPython or any other GUI framework)? Use a canvas to display the generated image and refresh it whenever there are operations performed on the graph.
Is there any other package which serves this purpose? I am looking for some package which can edit the graph and just provide me with the ability to bind my graph operations to the mouse events on the viewer.
Any other ideas? Greatly appreciate your inputs.
wxPython can display images very easily using wx.StaticBitmap. Here's a tutorial on the subject:
http://www.blog.pythonlibrary.org/2010/03/26/creating-a-simple-photo-viewer-with-wxpython/
You will probably need to do something like this:
img = wx.Image(filepath, wx.BITMAP_TYPE_ANY)
self.imageCtrl = wx.StaticBitmap(self.panel, wx.ID_ANY,
wx.BitmapFromImage(img))
Then just refresh it whenever you update the image. Depending on your graphing needs, there is the matplotlib project which can be integrated into wxPython.
There is a project that use wxPython and pydot that you might find useful: http://code.google.com/p/pyflowuca/. I saw it mentioned here: http://pythonhaven.wordpress.com/2009/12/09/generating_graphs_with_pydot/

GUI-embeddable Python drawing widget with anti-aliasing

I am writing a small diagram drawing application (similar to Graphviz in spirit), and need a GUI library that would allow me to embed a canvas capable of drawing anti-aliased lines and text. I want to have a text editor in one half of the window to edit the diagram code and a (perhaps live) preview pane in the other.
Right now I have the text editor in a tkinter window and the rendered diagram in a separate pygame one. This technically works, but it's messy (e.g. having two event loops), and in general I would much prefer having both parts in one window. I have searched for ways of integrating the two, but haven't been able to find anything cross-platform, and pygame explicitly suggests not trying to do it.
An alternative would be to have pygame export the image into a file and load it back into tkinter, but tkinter can read only GIF/PPM without PIL (and I use Python 3, which PIL doesn't support) and pygame can't write GIF/PPM. I could backport to Python 2, since it's a tiny app, but even then, having a large extra library for a simple image conversion doesn't seem right, and the round-trip to a file will probably be too slow for live preview (not to mention ugly).
Finally, a simple tkinter canvas is almost what I want, except it can't draw anti-aliased lines, and for a program whose main purpose is to draw line figures, that is not acceptable.
I'm using Python 3 so libraries that support it are preferred, but if there's no way to do that whatsoever Python 2 libs are Ok as well. The library needs to be cross-platform, and of course, the fewer external packages are required, the better.
If you don't mind the way GTK looks, pygtk has an option for antialising in their canvas widget (see this) and is considered by many to be as powerful as Tkinter, though it is not included in standard Python installs.
Also, it's Python 3.x compatible, which can't be said of most non-standard library modules and packages.
Screwing around with Tkinter+pygame is silly. I would use wxPython. In fact, I've done a diagramming widget using wxPython, and it has anti-aliasing:
Unfortunately it was for work, so I can't distribute the code.
The wxPython classes you want to look at for anti-aliasing are wx.GCDC and/or wx.GraphicsContext.
After a thorough search I ended up using PyQt4. It does fit all my requirements (Python 3, cross-platform, anti-aliasing), and now that I've gotten through the basics, it's also quite intuitive and easy to use.
Posting this as an answer to my own question and accepting it for future reference.

Selecting area of the screen with Python

I'm developing a screen shot utility in Python. At the moment it is specifically for Linux. So far I have the ability to take a screen shot of the full desktop, and have it upload to Imgur, then copy the link to clipboard. Now I want to expand into functions such as screen shots of the active window, or of a specific selection. If anyone could help, I'd love to know what kind of module would work best for this, and how to implement such a module.
The functionality will depend on what you are using for image grabbing.
With PIL
http://effbot.org/imagingbook/imagegrab.htm
With GTK
To take a screenshot of active window :
http://faq.pygtk.org/index.py?req=show&file=faq23.039.htp
Also look at the pixbuf api
http://library.gnome.org/devel/gdk-pixbuf/
http://developer.gimp.org/api/2.0/gdk-pixbuf/gdk-pixbuf-gdk-pixbuf.html
Off topic
There are some screen cast tools: http://pypi.python.org/pypi/castro/1.0.4

Python: OSX Library for fast full screen jpg/png display

Frustrated by lack of a simple ACDSee equivalent for OS X, I'm looking to hack one up for myself. I'm looking for a gui library that accommodates:
Full screen image display
High quality image fit-to-screen (for display)
Low memory usage
Fast display
Reasonable learning curve (the simpler the better)
Looks like there are several choices, so which is the best? Here are some I've run across:
PyOpenGL
PyGame
PyQT
wxpython
I don't have any particular experience with any of these, nor any strong desire to become an expert - I'm looking for the simplest solution.
What do you recommend?
[Update]
For those not familiar with ACDSee, here's what it does that I care about:
Simple list/thubmnail display of images in a directory
Sort by name/size/type
Ability to view images full screen
Single-key delete while viewing full screen
Move to next/previous image while viewing full screen
Ability to select a group of images for:
move to / copy to directory
delete
resize
ACDSee has a bunch of niceties as well, such as remembering directories you've moved images to in the past, remembering your resize settings, displaying the total size of the images you've selected, etc.
I've tried most of the options I could find (including Xee) and none of them quite get there. Please keep in mind that this is a programming/library question, not a criticism of any of the existing tools.
I will recommend using wxPython to create such a viewer, wxPython is easy to learn, free, cross platform and blends well in OSX. Even if you want to use pyopengl, wxPython would be good with pyopengl.
see such tutorials http://showmedo.com/videotutorials/video?name=1790000&fromSeriesID=179
and there is already cornice written in wxpython/PIL, may be you can modify that. It has been inspired by the famous Windows-only ACDSee :)
it's not an answer to your coding question but for (a big part of) the lack of ACDsee equivalent (requires OSX 10.5+):
Simple list/thubmnail display of images in a directory: Finder.app
Sort by name/size/type: Finder.app will do name & type, not image size (but does file size)
Ability to view images full screen: quick preview (spacebar / eye icon)
Single-key delete while viewing full screen: command-backspace while viewing in quickpreview, both windowed and fullscreen
Move to next/previous image while viewing full screen: both quickprewiew (after selecting a group of images or whole directory with cmd-a) and Preview.app
Ability to select a group of images for[...]: Finder.app will does all but resize
seems like you have everything except resize just pressing the spacebar while in finder.
Preview.app will resize both a single image or multiple ones in one batch.
Use an App like Picasa (now available on mac). Use AppleScript through Python to control it from your application.
Failing that, use PyObjC to create Cocoa image display component and dialogs, and so on.
I ended up using PyGame, has been pretty good so far.

Categories

Resources