Generate image for each font on a linux system using Python - python

I'm looking for a way to list all fonts installed on a linux/Debian system, and then generate images of some strings using these fonts. I'm looking for your advice as I kind of see how to do each part, but not to do both:
To list all fonts on a UNIX system, xlsfonts can do the trick:
import os
list_of_fonts=os.popen("xslfonts").readlines()
To render a string into an image using a font, I could use PIL (Python Imaging Library) and the ImageFont class.
However, ImagesFont.load expects a file name, whereas xlsfonts gives a kind of normalized font name, and the correspondence between the two doesn't seems obvious (I tried to search my system for files named as the output of xlsfonts, without results).
Does anyone has an idea on how I can do that? Thanks!

You can do this using pango, through the pygtk package. Pango can list fonts and render them.

you best bet is to do a find on all the fonts on the system, and then use ImagesFont.load() on the results of that list. I don't know where the fonts are on Debian, but they should be in a well known folder you can just do an os.walk and then feed the filenames in that way.

Related

Python Tkinter PhotoImage file formats supported?

I appreciate this is a VERY novice question but I just want to check in regard to Tkinter Photoimage class, is it only GIF/PGM/PPM images that it can read from files and nothing else unless I download the Python Image Library.
If thats the case I now know exactly where I went wrong in the code I'm writing. IE: wrong file format
That sounds right. There may be another format or two that native Tkinter supports, but it's very limited. There's a more up-to-date version of PIL named Pillow that you might want to look into. It doesn't seem like PIL is being actively maintained, last I looked. If you want to work with JPEG for example, you need PIL (or Pillow).

Generating an image-report with Python

Hy,
I'm working on a project, where I have to generate a image (e.g. .png, .bmp etc) with a python script.
The Image must have:
Small boxes (8x8px) in 3 different colours
Horizontal(normal) text in 2 different sizes
and 3) vertikal text (rotate normal text) (like this: http://devcity.net/Data/ArticleImages/Dual_Labels.jpg)
So not very complex things.
I spent the last days with PiL (Python Image Library). For the small boxes, it works fine and easy. But to generate a text in the image, it doesn't work fine.
What also works is to write a normal text, with the standard font (pilfont-type).
But I can't set the px-size of this text. When using truetypes, the following error comes:
"The _imagingft C module is not installed"
I allready "googled" this and this seems to be a popular problem. My Problem is, that the script also has to run on other python systems. What I can accept is, that I have to install Pil on each system/computer, but I can't fix the problem with the truetypes each time!
I'm using Python 2.7 with pil 1.1.7.
So to my question:
For the named "forms" my script has to generate, what library (or other ways to generate an image with a script) would you recomment to me?
Would it be possible to create, e.g writing a bitmap-file with text and pixels with colour, with my script in "Pure-Python", so without any extension?(Would be the optimal solution for me)
Have you thought about using PyCairo instead? See this link for an example: https://stackoverflow.com/a/6506825/514031
This is not quite what matplotlib was designed for, but is definitely capable of producing what you're after. Have a look at the gallery, it has usage examples for almost everything you mentioned.

In python, how do you open a file or picture in a new window?

The content of the file or the picture should show up in a new window.
That will depend a lot on your operating system, since there are different programs on different systems to view images, etc. But one trick you might use is
import webbrowser
webbrowser.open(THE_FILE)
That should open up your default web browser pointed to the file, which will display images, and might do what you want for certain types of files.
you could try
os.system("fspot picture.jpeg")
But, obviously, I'm assuming you're using fspot to view images, and that might only work in linux.
Check out the documentation for os.
-EDIT-
Mu Mind's solution works pretty well in Ubuntu Karmic. Not sure what it will do on a windows machine.
you can use
os.system("start "+"anyfile.txt")
assuming you have windows. This basically opens the file in it's extension (For example if you had a .txt it would open in notepad.)

Python: Simplest (non-PIL) way to get image metadata (mainly size)

I'm gathering basic metadata for images - mainly their dimensions, although it'd be nice to get any other available metadata as well. The image formats I'm interested in are png, jpg, and gif.
I'm using PIL at the moment, but it occurred to me there may be a simpler way that doesn't involve external dependencies or binary libraries. Is there one?
I don't think there is anything built in, but if you look up those file formats, you will find that the size is encoded near the beginning of the file.
You can use the struct module to parse just enough of the header to work out the size
Answer: No there is not a simpler way than using an external library.
If you are only going to care about one and one file format only, then yes. Then it's easy to implement something specific for that. But if you want to be generic, you need to support a lot of file formats, and then you don't want to do all that work yourself.
To simplify install of PIL, you might look at Pillow, a friendly forkĀ§ that makes PIL easy_installable.
See ImageMagick, a fantastic library for dealing with bitmap images. The identify tool from the command line suite will do what you want. There are also a few Python interfaces.

Dealing with huge (potentially over 30000x30000) images in Python?

I'm trying to use a python script called deepzoom.py to convert large overhead renders (often over 1GP) to the Deep Zoom image format (ie, google maps-esque tile format), but unfortunately it's powered by PIL, which usually ends up crashing due to memory limitations. The creator has said he's delving into VIPS, but even nip2 (the GUI frontend for VIPS) fails to open the image. In another question by someone else (though on the same topic), someone suggested OpenImageIO, which looks like it has the ability, and has Python wrappers, but there aren't any proper binaries provided, and trying to compile it on Windows is a nightmare.
Are there any alternative libraries for Python I can use? I've tried PythonMagickWand (wrapper for ImageMagick) and PythonMagick (wrapper for GraphicsMagick), but both of those also run into memory problems.
I had a very similar problem and I ended up solving it by using netpbm, which works fine on windows. Netpbm had no problem with converting huge .png files and then slicing, cropping, re-combining (using pamcrop, pamdice, and pamundice) and converting back to .png without using much memory at all. I just included the necessary netpbm binaries and dlls with my application and called them from python.
It sounds like you're trying to use georeferenced imagery or something similar, for which a GIS solution sounds more appropriate. I'd use GDAL -- it's an excellent library and comes with easy-to-use Python bindings via Swig.
On Windows, the easiest way to install it is via Frank Warmerdam's FWTools package.
I'm able to use pyvips to read images with size (50000, 50000, 3):
img = pyvips.Image.new_from_file('xxx.jpg')
arr = np.ndarray(buffer=img.write_to_memory(),
dtype=np.uint8,
shape=[img.height, img.width, img.bands])
Is a partial load useful? If you use PIL and the image format is .BMP: you can open() an image file (which doesn't load it), then do a crop(), and then load - which will only actually load the part of the image which you've selected by crop. Will probably also work with TGA, maybe even for JPG and less efficiently for PNG and other formats.
libvips comes with a very fast DeepZoom creator that can work with images of any size. Try:
$ vips dzsave huge.tif mydz
Will write the tiles to mydz_files and also write a mydz.dzi info file for you. It's typically 10x faster than deepzoom.py and has no size limit.
See this chapter in the manual for an introduction to dzsave.
You can do the same thing from Python using pyvips like this:
import pyvips
my_image = pyvips.Image.new_from_file("huge.tif", access="sequential")
my_image.dzsave("mydz")
The access="sequential" tells pyvips it can stream the image rather than having to read the whole thing into memory.

Categories