PIL vs RMagick/ruby-gd - python

For my next project I plan to create images with text and graphics. I'm comfortable with ruby, but interested in learning python. I figured this may be a good time because PIL looks like a great library to use. However, I don't know how it compares to what ruby has to offer (e.g. RMagick and ruby-gd). From what I can gather PIL had better documentation (does ruby-gd even have a homepage?) and more features. Just wanted to hear a few opinions to help me decide.
Thanks.
Vince

PIL is a good library, use it. ImageMagic (what RMagick wraps) is a very heavy library that should be avoided if possible. Its good for doing local processing of images, say, a batch photo editor, but way too processor inefficient for common image manipulation tasks for web.
EDIT: In response to the question, PIL supports drawing vector shapes. It can draw polygons, curves, lines, fills and text. I've used it in a project to produce rounded alpha corners to PNG images on the fly over the web. It essentially has most of the drawing features of GDI+ (in Windows) or GTK (in Gnome on Linux).

PIL has been around for a long time and is very stable, so it's probably a good candidate for your first Python project. The PIL documentation includes a helpful tutorial, which should get you up to speed quickly.

ImageMagic is a huge library and will do everything under the sun, but many report memory issues with the RMagick variant and I have personally found it to be an overkill for my needs.
As you say ruby-gd is a little thin on the ground when it comes to English documentation.... but GD is a doddle to install on post platforms and there is a little wrapper with some helpful examples called gruby thats worth a look. (If you're after alpha transparency make sure you install the latest GD lib)
For overall community blogy help, PIL's the way.

Related

How can i align star field FIT images taken with CCD in PYTHON?

I have seven of star field images taken with CCD. Extensions of them are FIT. I'm trying to align them by Python but, i have confused. This time is my very first attempt to align images. I found a few module related with alignment of fits images but they seem to me very confusing. I need a help.
The APLpy module (https://aplpy.github.io/) does what you need to do.
However, it might not be the most straightforward thing to use for a first-timer.
What I would recommend is using PyRAF, which is a python wrapper for the IRAF data reduction software developed by NOAO (National Optical Astronomy Organization) in the 80's/90's to deal with CCD data reduction.
You can get pyraf by typing pip install pyraf. Once you have pyraf, I would recommend following Josh Wallawender's IRAF tutorial; skip to Section V ("Basic Reduction Steps for Imaging Data"). Keep in mind you are using PyRAF, so any IRAF-specific things (sections I-IV) don't necessarily apply to you. PyRAF is a much easier to use system.
The specific PyRAF tasks you need are imalign and imcombine. You'll also need to give a file with the rough shifts between each image (the help file for imalign is a fantastic resource, btw, and you can access it via epar imalign and clicking on the "Help" button when the GUI pops up).
I hope this gives you a starting point. There are other ways to do image combining in python, but astropy is kind of finicky for first-time users.

Python: Manipulating a 16-bit .tiff image in PIL &/or pygame: convert to 8-bit somehow?

Hello all,
I am working on a program which determines the average colony size of yeast from a photograph, and it is working fine with the .bmp images I tested it on. The program uses pygame, and might use PIL later.
However, the camera/software combo we use in my lab will only save 16-bit grayscale tiff's, and pygame does not seem to be able to recognize 16-bit tiff's, only 8-bit. I have been reading up for the last few hours on easy ways around this, but even the Python Imaging Library does not seem to be able to work with 16-bit .tiff's, I've tried and I get "IOError: cannot identify image file".
import Image
img = Image.open("01 WT mm.tif")
My ultimate goal is to have this program be user-friendly and easy to install, so I'm trying to avoid adding additional modules or requiring people to install ImageMagick or something.
Does anyone know a simple workaround to this problem using freeware or pure python? I don't know too much about images: bit-depth manipulation is out of my scope. But I am fairly sure that I don't need all 16 bits, and that probably only around 8 actually have real data anyway. In fact, I once used ImageMagick to try to convert them, and this resulted in an all-white image: I've since read that I should use the command "-auto-levels" because the data does not actually encompass the 16-bit range.
I greatly appreciate your help, and apologize for my lack of knowledge.
P.S.: Does anyone have any tips on how to make my Python program easy for non-programmers to install? Is there a way, for example, to somehow bundle it with Python and pygame so it's only one install? Can this be done for both Windows and Mac? Thank you.
EDIT: I tried to open it in GIMP, and got 3 errors:
1) Incorrect count for field "DateTime" (27, expecting 20); tag trimmed
2) Sorry, can not handle images with 12-bit samples
3) Unsupported layout, no RGBA loader
What does this mean and how do I fit it?
py2exe is the way to go for packaging up your application if you are on a windows system.
Regarding the 16bit tiff issue:
This example http://ubuntuforums.org/showthread.php?t=1483265 shows how to convert for display using PIL.
Now for the unasked portion question: When doing image analysis, you want to maintain the highest dynamic range possible for as long as possible in your image manipulations - you lose less information that way. As you may or may not be aware, PIL provides you with many filters/transforms that would allow you enhance the contrast of an image, even out light levels, or perform edge detection. A future direction you might want to consider is displaying the original image (scaled to 8 bit of course) along side a scaled image that has been processed for edge detection.
Check out http://code.google.com/p/pyimp/wiki/screenshots for some more examples and sample code.
I would look at pylibtiff, which has a pure python tiff reader.
For bundling, your best bet is probably py2exe and py2app.
This is actually a 2 part question:
1) 16 bit image data mangling for Python - I usually use GDAL + Numpy. This might be a bit too much for your requirements, you can use PIL + Numpy instead.
2) Release engineering Python apps can get messy. Depending on how complex your app is you can get away with py2deb, py2app and py2exe. Learning distutils will help too.

Python text to image generation problems

I'm using PIL to load in various fonts and draw text to images. At the basic level, it all works.
However, I am running into a number of problems such as letters being clipped (mainly cursive or stylistic fonts with lots of tails and such). textsize() does return width/height values, yet letters are still clipped. There also doesn't seem to be methods in PIL to specify larger image sizes for the character generating. Another issue is the vertical spacing. It seems PIL returns large height values for certain fonts and thus the vertical spacing between lines is overly large.
I'm in search of a more advanced font and text handling system than PIL, given its apparent limitations.
I've been researching this a lot over the last week (Google, Python docs, Stackoverflow, etc) and I've seen people recommending to use either Imagemagick or a combination of pango and cairo. However, as much as I've read and searched for these respective technologies I am simply not finding any usable documentation that pertains to what I am trying to do. There are some Python bindings for Imagemagick, but they all seem several years out of date.
Can some of the helpful souls here on SO point me to some tutorials on how to use Pango/Cairo and/or Imagemagick?
The Cairo cookbook has a number of examples for using Cairo, and the Python routines are almost mirror images of the C routines.
I've had some fine results with PyGame, but I don't know if it will necessarily solve your problem.

Can Python + Qt combination produce a real time spectral analysis tool?

I want to develop a tool that does the following things.
take in a live voice recording
produce a real time spectrogram
show the time-domain signal
output few values extracted from the spectral analysis
All of these have to be kept updated in a window as the voice is recorded.
I have worked with numpy. But I'm completely new to Qt and other GUI builder tools. What's the best way to proceed given this situation? My peers recommended Qt after I explained them the task. If someone knew of a better tool to be used with python for this task, please let me know. Also, please help me with technical details as to how to capture the live stream and process it in python which is to be shown in a GUI window. One link that gave me some hope is http://www.swharden.com/blog/2010-03-05-realtime-fft-graph-of-audio-wav-file-or-microphone-input-with-python-scipy-and-wckgraph/ . But it is a bit difficult to comprehend it. May be a less intensive solution will help me in getting started.
In Qt 4.6, the QAudioInput API was added. This provides a cross-platform abstraction for getting an audio input signal, and therefore would be of use in achieving point (1).
As for (2) and (3), the Spectrum Analyzer demo which ships with Qt may be of interest.
Screenshot of Spectrum Analyzer demo running on Symbian http://labs.trolltech.com/blogs/wp-content/uploads/2010/05/spectrum.png
The implementation is in C++ rather than in Python, but it may be of use as a reference. Basically what you need for (2) is to calculate the Fast Fourier Transform of the input signal. You'll probably want to use a library which provides an FFT implementation rather than writing your own - that's the approach I took when writing the demo :)
As for (3), this is conceptually pretty simple, but requires a bit of thought in order to get a smoothly scrolling waveform. Take a look at the tiling approach used in the Waveform class in the demo for some tips.
I think by (4) you mean: reduce the large number of points in the FFT output to a small number of values. This is what the demo does in order to plot a bar chart for the spectrum. Again, refer to the demo code to see how the binning of frequency amplitudes is implemented.
Another example of a real-time audio spectrum analyzer using PyAudio, scipy, Chaco in one script can be found in the list of examples for Chaco. (Worked out-of-the-box on my Precise).
On Linux, this is definitely feasible. Other platforms too, but I can really only answer for Linux. Python isn't necessarily your sharpest tool for real-time DSP, but on a suitably modern machine and suitably modest goals you will be fine.
First, you need an interface to the Linux audio drivers. ALSA is pretty universal. There are several different Python wrappers for the ALSA libraries, see Python In Music for a list of libs and applications using them.
Then you do your spectral analysis. SciPy and NumPy have all that.
Then you draw into your Qt window. My expertise is in GTK but you probably want to create a QtCanvas (tutorial), which is an object-oriented drawing area that's designed for this kind of use.
Or you could just use SciPy, which can probably be convinced to do all of this! AudioLab in particular looks like it might be a big help.

Fractal image scaling with Python

I am in a position where relatively low resolution images are provided (via an API, higher resolution images are not available) and high resolution images need to be generated.
I've taken a look at PIL and it's just great for about everything... Except scaling up images.
It has the common resizing algorithms:
Nearest Neighbor
Bilinear
Bicubic
Anti-aliased
I would like to use Fractal Resizing (as per jeff's post on coding horror), but alas, PIL has no support for this kind of resizing.
Further Google searches yield no alternative libraries to provide fractal image resizing either.
Does such a thing exist or do I really have to buckle down and write my own fractal resizing algorithm?
I'm no expert but from my current vantage point, that looks like a pretty steep learning curve :(
If no such library exists, maybe you have some advice where to learn about fractal compression algorithms?
There are algorithms, and you're definitely not going to find them in Python. To start with, you can take this paper :
Daniel Glasner, Shai Bagon, and Michal Irani, "Super-Resolution from a Single Image," in Proceedings of the IEEE International Conference on Computer Vision, Kyoto, Japan, 2009.
It is very much state of the art, highly sophisticated, and producing promising results. If you ever make it into a python implementation please release it to the public :)
Fractal-based image scaling is still quite unusual and hasn't settled down onto one accepted-best algorithm yet. I'm afraid you aren't going to find it in standard image processing libraries yet.
It's also not invariably preferable to bicubic. It can have artefacts which will be undesirable for some kinds of images. For me, Jeff's example image looks a bit weird and unnatural around the sharp edges like the right-hand-side of the nose. Better for some values of ‘better’, sure, but I wouldn't blanket-apply it to all my images.
(This is the case with other ‘advanced’ upscaling techniques, too, including the better-known and more widely-implemented Lanczos/Sinc method.)
Check out this solution using Residual Dense Networks:
https://github.com/idealo/image-super-resolution
Good documentation and rather easy to implement. They even have docker builds and google collab notebooks available. See the docs

Categories