Launch Default Image Viewer from Pygtk Program

Launch default image viewer from pygtk program

I don't know specifically using PyGTK but: xdg-open opens the default app for a file so running something like this should work:

import os
os.system('xdg-open ./img.jpg')

EDIT: I'd suggest using the subprocess module as in the comments. I'm not sure exactly how to use it yet so I just used os.system in the example to show xdg-open.

How to open a file with the standard application?

os.startfile is only available for windows for now, but xdg-open will be available on any unix client running X.

if sys.platform == 'linux2':
subprocess.call(["xdg-open", file])
else:
os.startfile(file)

How to show a png image in Gtk3 with Python?

You can use Gtk.Image. If you generate a file, you could use:

img = Gtk.Image.new_from_file('/path/to/my_file.png')

and add img to the container (GtkGrid in your case). Or, if you already have the Gtk.Image there, you can use:

img.set_from_file('/path/to/my_file.png')

Instead of ...from_file you can use from_pixbuf, and you can create a Gdk.Pixbuf from a stream.

In general, you can use the documentation for C and change the idiom to Python. Also, you can check the demos available in PyGObject, in particular, the demo for handling images.

Showing an image from console in Python

Using the awesome Pillow library:

>>> from PIL import Image                                                                                
>>> img = Image.open('test.png')
>>> img.show()

This will open the image in your default image viewer.

pyGTK : pack and unpack

In GTK+ all widgets are hidden by default (which I think was a stupid design decision, but oh well). You usually call show_all() on a window, so indirectly show all widgets contained in it by the time of the call. If you add (pack, whatever) a widget later, don't forget to show() it manually.

Read file continuously and update Python GTK application accordingly

Not sure if I understand you correctly, but I would expect what you want to do is to schedule a call to ai() in your main GTK loop, so that your program checks if there is input on a regular base.

If this is what you want you have two choices: scheduling the call periodically or schedule the call for when the program is idle (it is not doing anything else).

Your code should look something like:

gobject.idle_add(callback, ...)  #execute callback when idle
gobject.timeout_add(interval, callback, ...) #execute callback every interval millisecs

The documentation should be here but presently there is a server error. Here are the relevant passages:

The gobject.idle_add() function adds a function (specified by callback) to be called whenever there are no higher priority events pending to the default main loop. The function is given the default idle priority, gobject.PRIORITY_DEFAULT_IDLE. Additional arguments to pass to callback can be specified after callback. The idle priority can be specified as a keyword-value pair with the keyword "priority". If callback returns FALSE it is automatically removed from the list of event sources and will not be called again.

The gobject.timeout_add() function sets a function (specified by callback) to be called at regular intervals (specified by interval, with the default priority, gobject.PRIORITY_DEFAULT. Additional arguments to pass to callback can be specified after callback. The idle priority may be specified as a keyword-value pair with the keyword "priority".

A catch: you have your callbacks to return True if you want to keep them in the scheduler, failing to do so will make them execute only once.

HTH!

Pixel coordinates using PyGTK

I don't think you will be able to do manipulate images in the way you want with the APIs given by PyGTK.

For accessing image data, I recommend looking at the Python Imaging Library (PIL).

If you're a beginner with GTK, don't miss the tutorial of the Gnome Developer Center. There is even an example for an image viewer.

Furthermore, please keep in mind that recent applications for Ubuntu are written in GTK 3. Many many examples you find on the internet are still for PyGTK 2.x and you're not encouraged to use code from them (in case you're getting them to work).

How do you show a picture in python?

The easiest way is to use PIL and the Image.show method. This brings up an external viewer program on the image.

How to show PIL images on the screen?

From near the beginning of the PIL Tutorial:

Once you have an instance of the Image class, you can use the methods
defined by this class to process and manipulate the image. For
example, let's display the image we just loaded:

     >>> im.show()

Update:

Nowadays the Image.show() method is formally documented in the Pillow fork of PIL along with an explanation of how it's implemented on different OSs.

Taking a snapshot from webcam monitor in python (pygtk)

I'm missing the point of the question. There's a take_snapshot function with the bit that actually takes the snapshot commented out. Modify your code to look like the following

def take_snapshot(self,widget):
filename = str(time.time()) + ".jpg"
pixbuf = gtk.gdk.Pixbuf.get_from_drawable(self.movie_window.window, self.movie_window.window.get_colormap(), 0, 0, 0,0 500, 400)
pixbuf.save(filename, "jpeg", {"quality":"100"})

that should take a snapshot and save the image with the current time



Related Topics



Leave a reply



Submit