Python Tkinter PhotoImage file formats supported? - python

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).

Related

Importing FIles on IDLE (python code editor)

This may seem like a stupid question, but I am genuinely having a lot of trouble with this: I need to be able to import a file, like an image, to IDLE so I can open it when the program runs. Am I missing something really obvious or is this not possible on IDLE?
Thanks.
BTW I work on a Chromebook, in Python.
from PIL import Image
with Image.open(image_directory) as image:
pass
From there you can use PIL's various functions to edit the image. Don't forget to save it, though.
You can use open() and read() function.
https://www.techwalla.com/articles/how-to-a-read-text-file-in-python
It is properly shown on the above site.

How to prevent upload of "Bad images" in Django? (PILLOW, easy-thumbnails)

Intro
I have been using Django with easy-thumnails for quite a while, and today I stumbled upon one really nasty bug, but because I have always permitted easy-thumbnails to do as it pleases I consider myself a noob at this.
TL;DR
I need to validate if an image can be read by easy-thumbnails or Pillow before it is saved to the django model in an optimal way.
Explanation
When I tried to convert an .svg image, apparently Pillow crashes on the spot. I don't know if it is because of the format, even though according to some other stack issues, by installing libz or zlib1g, there should not be a problem (they are already installed in my system btw).
But the format doesn't matter, if I insert a corrupt file as an image it could make the library crash every time.
I need to be able to validate (inside my django-forms or my drf-serializers) if an image can be read by pillow before saving it and prevent this to ever happen again.
Any ideas to validate the file in an optimal way would be greatly appreciated.

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.

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