Overwrite image in PIL instead of opening new window - python

I'm sending a stream of JPEG images from a Raspberry Pi to my MBP via a simple socket programme in Python 2.7.
When I read the image from the stream on my MBP, it opens up in Preview and opens a new Preview window for every separate image. I have an fps of about 2/3 and obviously 2/3 new windows per second is impossible to work with.
How can I go about only opening one Preview window and simply overwriting the displayed image? Would OpenCV be the best way to go? If so I am unsure how to.
Here is how I read the stream and display the images:
image_len = struct.unpack('<L', connection.read(struct.calcsize('<L')))[0]
if not image_len:
break
image_stream = io.BytesIO()
image_stream.write(connection.read(image_len))
image_stream.seek(0)
image = Image.open(image_stream)
image.show()

OS X Preview seems to automatically reload open images at intervals (always when the window receives focus), but Image.show saves a new temporary file each time you use it. I suggest saving each new frame to the same file and then using subprocess.call with the OS X open command.
This being said, the documentation notes that Image.show is primarily for debugging purposes. For a video with more than a few FPS, you probably want something else. One solution would be an HTML interface with WebSockets, perhaps using something like AutoBahn.

Related

Python: Dynamically Updating Image Viewer

I'm building a small program that essentially outputs 'video' from a webcam using Pygame. Is there any way to easily view an image, but every half-second or so, update the image to the latest version? Most methods to load an image require an external viewer and only load the image into memory once, thus forcing the programmer into closing the viewer and reopening it every time the image updates. As I said, is there any small module that creates a basic image viewer and can be controlled easily?

adding a quit timer that closes a photo

After many hours of searching online and in my python book I can't seem to find the answer to my question which is what do I add to my code so I can put in a timer that automatically closes the photo? It pulls itself up but then I have to manually close the photo to get back to my main program. Any help would be appreciated.
from PIL import Image
img = Image.open('battleship load screen.png')
img.show()
This is not possible using PIL alone - img.show() is just launching another program, it's intended for debugging really, not for presenting things to the user.
From the docs.
Displays an image. This method is mainly intended for debugging
purposes.
On Unix platforms, this method saves the image to a temporary PPM
file, and calls the xv utility.
On Windows, it saves the image to a temporary BMP file, and uses the
standard BMP display utility to show it.
This method returns None.
If you want to display an image and have control over it, use a graphical toolkit and construct a UI for your purpose. I've linked there to an example using PySide, a set of QT bindings, but of course you could use any toolkit - each will be different.

Unable to show an image using python PIL Image.show

I'm using the Python Imaging Library and I am unable to open an image successfully in Windows Live Photo Gallery. There is a message that shows up saying "There are no photos or videos selected" instead of the image.
This is what I've tried:
import Image
img = Image.open(r"C:\Users\User\Pictures\image.jpg")
img.show()
This is pretty much the same as in the PIL handbook tutorial, so I'm not sure where I'm going wrong.
The documentation says:
On Windows, it [show()] saves the image to a temporary BMP file, and uses the standard BMP display utility to show it.
Problem is that your program exits immediately somehow, the temporary file is deleted upon exit and Windows etc. cannot find it. As a temporary solution, try adding:
import time
# Your code as above
time.sleep(30)
This will make the program wait 30 seconds before exiting. If you prefer, you could make it wait the user to press a key.
EDIT: it seems like you are experiencing problems with temporary files. As a workaround, save the image somewhere on the disk using, say, img.save("C:\Users\User\Pictures\test.jpg") and open it with your favorite image viewer. Whenever you want to show the processed image, call save again and reload the picture in the image viewer.

Python - How to record an application window to a video?

I have a python application, and I need to save this window action to a video file.
This application can be minimized, or the user can put another window in the front, but I need steel saving this window actions
Is it possible ?
ok:
I'm using Gnome, pygtk, X11
I need save all live of my application to a video
Periodically use Window.get_snapshot to get snapshots of the window. Then compose these images into a video. A simple though inefficient mechanism I can think of to do this is for each frame, convert your Pixmap into a Pixbuf using Pixbuf.get_from_drawable. Then use Pixbuf.save to save each frame to a PNG. When the application has finished, compose all of these PNGs into a video using FFmpeg.

How to programmatically capture a webcam photo

What's the simplest way in Ubuntu 11.10 to programmatically guide (either from Bash or Python) the user to capture a webcam photo of themselves?
I can launch a simple app like Cheese, but I don't see an easy way to immediately detect or retrieve the photo it captures. I can also access and record the webcam stream directly via OpenCV, but I'd have to reinvent the GUI to communicate with the user.
Is there any kind of script that's a happy medium, where I can launch it, and it prints on stdout the filename of the image the user took?
I like using pygame for that -
it does not require you to open a Pygame SDL window, unlike when you want to use it to capture keyboard events, for example.
import pygame.camera
pygame.camera.init()
cam = pygame.camera.Camera(pygame.camera.list_cameras()[0])
cam.start()
img = cam.get_image()
import pygame.image
pygame.image.save(img, "photo.bmp")
pygame.camera.quit()
Though Pygame will only save uncompressed "bmp" files - you may want to combine it with PIL to write to other formats.
If you want to do this via Python, it looks like you have a few options. The Pygame library has the ability to access cameras.
If that's unsatisfactory, you can go much lower level and access the Video 4 Linux 2 API directly using ioctl calls using Python's fcntl library.

Categories

Resources