Capturing images from a camera in Python: easy, reliable, cross-platform - python

What is the most suitable way to capture a still image from Python and convert to a PIL image?
Google gives me numerous ways with OpenCV and lesser-known libraries. I want an easy, reliable, mature, cross-platform library for this purpose. And with minimal dependencies and extra packages.
If possible, it must also support displaying live images with major windowing toolkits, although the performance (frame rate, clarity) is not important.

I use gphoto2 and with subprocess. But it should be possible to access this library from ctypes, if you prefer this.

OpenCV's Python API is pretty solid at this point, and OpenCV is easily installed on all major platforms. It's good, and it's probably your best bet. Getting a live feed from a webcam can be done in as little as 10 lines of code.
PIL or its newer fork Pillow are also good, mature, and cross platform.
PyGame also has a camera module, an intro is here: http://www.pygame.org/docs/tut/camera/CameraIntro.html but I have noticed that getting PyGame to integrate gracefully into other toolkits (wx, Qt) can be difficult.

Related

Is there an skimage alternative to OpenCV's ArUco library?

I have used OpenCV in the past for a project of mine, and I remember the AruCo library being very useful to specify certain regions, to calibrate the camera, etc.
I am currently working on another project that will get involved with image processing, and I've been working with SciPy in general for data-related tasks. As I will be using SciPy, I've been considering to work with skimage, as it may be more related to the other packages I will use.
Here's the question: I want to use something similar to, if not the same as, the AruCo library for skimage as it will be convenient to work with. Is there such a solution for skimage?
Here's the library in question,
AruCo library for OpenCV
After some digging around, I've noticed that OpenCV and skimage are compatible with each other to a great extent. The only issue I've encountered so far is with io.imshow() and cv2.imshow() yielding negative-like colors with respect to each other (in Colab, that is).
If that (and other potential minor issues) could be resolved, it is quite easy to just use AruCo with cv2 and use skimage wherever needed. It could take some work, but it is possible.
Here's a great example from the skimage library itself, where the library cv is used to capture webcam feeds. So they are actually compatible.
Note: This is deprecated, so just consider it as a proof. There may be better solutions to use a webcam feed than this (could just use cv2 for it)
https://gist.github.com/stefanv/5160329

Python custom GUI

I googled and search stackoverflow before asking this question
Answers that I don't expect:
wxWidgets is the best Python GUIUse TkInter (BIM) for GUI development.
Q. How to make a GUI without using any module/library? i.e make a GUI from scratch. Modules like tkinter not allowed.
I've made several GUIs from scratch using SDL which is a low level drawing library. The advantage of doing that is that it will look exactly the same on any platform down to the pixel and you can get it to work on embedded systems. Full screen GUIs are really easy too. Disadvantages are that it is a lot of work.
In python the pygame library wraps SDL so you would use that, and in fact that is how I made the GUI for a lab instrument which had a large colour LCD screen. The controller ran linux, but not X-windows.
pygame is an extra library, yes, but I can't think of a way of making a GUI with only what python provides.
The easiest GUI to make without "module/library" is a web-based one. I.e. generate HTML with Javascript from your Python code, and let the Javascript interact via AJAX with your Python app. This can be implemented without too much effort with just the standard Python library (and some JS code, of course), or with modules that don't require "heavy" installation of platform-specific extensions.

Pure python cross-platform font renderer?

I'm looking for a pure Python font renderer. I don't need it to render to anything special, I just need the pixel colors/alpha. I'm using PIL right now, but I am not sure if it needs to hook into any system libraries that may not be installed.
Pyglet manages this, somehow, but I wasn't able to determine the relevant code. I am working on a gui for pyglet, so if I could use this, that would probably be even better.
Pyglet uses FreeType where available. I'd stick with that, even though it's not pure Python.
ReportLab can render to pixmaps:
http://www.reportlab.com/apis/reportlab/2.4/graphics.html#module-reportlab.graphics.renderPM
probably a bit heavyweight for your needs though.

How I can develop a screen-capture tool in Python

I'm learning Python now and I want to develop a screen capture tool in Python.How I can do this work?
If you're on windows, use ImageGrab module along with Imaging library.
Something like:
from PIL import ImageGrab
ImageGrab.grab().save("screenshot.jpg", "JPEG")
I think the best way to do this would be create a higher level interface to ffmpeg. Unless you want to write the code to deal with X11 and record the screen, then it would be the best option.
If you test it on various platforms, then give useful error messages on things like incompatible sound systems, where to get certain video drivers, etc then this could be a pretty useful program.

Python: default/common way to read png images

I haven't found a standard way in Python to read images. Is there really none (because there are so many functions for so many custom stuff that I really wonder that there are no functions to read images)? Or what is it? (It should be available in the MacOSX standard installation and in most recent versions on Linux distributions.)
If there is none, what is the most common lib?
Many search results hint me to Python Imaging Library. If this is some well known Python-lib for reading images, why isn't it included in Python?
No, there are no modules in the standard library for reading/writing/processing images directly. But the most common library might be PIL (Python Imaging Library). Many projects are not included in the standard library because they are 1) totally optional and 2) cannot be maintained by the few Python core developers.
Coming late to the party, I would strongly suggest one of the Python interfaces to the ImageMagick library (Wand worked well in my testing, I'll know more soon...)
ImageMagick is a more powerful library and also pretty well a de-facto standard across many languages. Appealing to a wider base, they also have a wider developer base as a result.
THe suggested PIL does not support interlaced PNGs. It can be quite anoying when dealing with lots of PNGs from different origins.
It is possible to open them, but can only read headerinformation from them, all other operations fail.

Categories