How to compress PNG image with S3TC/DXT algorithm in python? - python

I try to find way for compressing images(PNG as an example) with any S3TC/DXT algorithm using python libraries.
As I can see in Pillow(PIL) library DDS format in Read-only formats section. Therefore Pillow can't be used for this purpose.
Searching in google didn't give positive results.
Question:
Is it possible to do with python?
Could someone please provide link to libraries with such functional?(which is checked on practice)
DDS format is not mandatory for my case. I need only compressed file.
PS:
It's required for creating textures for future use.
Library should support different algorithms of compression.

You could use Python Wand. Here I create a pseudo image with a magenta-yellow gradient and save as DDS:
from wand.image import Image
with Image(width=200, height=80, pseudo='gradient:magenta-yellow') as img:
img.save(filename='result.dds')
Or, if you want to load a PNG file and save as DDS:
with Image(filename='input.png') as img:
img.save(filename='result.dds')

Related

"imagio.imsave" vs "imageio.core.util.Array.tofile"

I am expanding my limited Python knowledge by converting some MATLAB image analysis code to Python. I am following Image manipulation and processing using Numpy and Scipy. The code in Section 2.6.1 saves an image using both imageio.imsave and face.tofile, where type(face)=<class 'imageio.core.util.Array>'.
I am trying to understand why there are two ways to export an image. I tried web-searching tofile, but got numpy.ndarray.tofile. It's very sparse, and doesn't seem to be specific to images. I also looked for imageio.core.util.Array.tofile, but wasn't able to find anything.
Why are there two ways to export files? And why does imageio.core.util.Array.tofile seem to be un-findable online?
The difference is in what the two functions write in the file.
imageio.imsave() saves a conventional image, like a picture or photo, in JPEG/PNG format that can be viewed with an image viewer like GIMP, feh, eog, Photoshop or MSxPaint.
tofile() saves in a Numpy-compatible format that only Numpy (and a small number of other Python tools) use.

Adding Image in excel using pyexcelerate

I am using pyexcelerate library to generate an excel sheet in python. I need to add an image at the top using an image url but I could not find any solution for that library. Can anyone tell whether its possible to add image using pyexcelerate?
I'm afraid, this isn't possible. The library pyexcelerate is aiming to fast writing data. If you want add an image, use an different library, for example openpyxl: https://openpyxl.readthedocs.io/en/stable/usage.html?highlight=image#inserting-an-image

Opening image files for load_sift in scikit-image

The documentation for load_sift
from skimage import io
img = open('g.png')
rv = io.load_sift(img)
This code is not working. It seems that this is not how I'm supposed to open the image file.I could not understand the documentation.
The load_sift routine is not meant for operating on numpy arrays or image files. As the f parameter is documented, it states:
Input file generated by the feature detectors from
http://people.cs.ubc.ca/~lowe/keypoints/ or
http://www.vision.ee.ethz.ch/~surf/
I.e., these are specially formatted files with the SIFT features already extracted by the binaries found at those URLs. The reason we do not calculate SIFT features inside scikit-image is because those routines are patent encumbered, therefore you have to use an external utility or library to calculate them.
In scikit-image, you read in images as follows:
from skimage import io
image = io.imread('g.png')
This returns a numpy array, that you can manipulate any way you wish. To additionally extract SIFT features:
Download the external binary
Run it on your images
Load the resulting file with load_sift
Because there was uncertainty about the docstring, I have made a patch to clarify that an external tool is needed.

How to detect checked box using python

I have the below PNG image and I am trying to identify which box is checked using Python.
I installed the OMR (optical mark recognition) package https://pypi.python.org/pypi/omr/0.0.7 but it wasn't any help and there wasn't any documentation about OMR.
So I need to know if there is any API or useful package I can use with Python.
Here is my image:
If you're not afraid of a little experimenting, the Python Imaging Library (PIL, download from http://www.pythonware.com/products/pil/ or your favorite repo. Manual: http://effbot.org/imagingbook/pil-index.htm) permits loading the PNG, and accessing it.
You can extract a section of the image (eg. the interior of a checkbox. See crop in the library), and sum the pixels in that sub-image (see point). Compare that with a threshold (say > 10 pixels = checked).
If the PNG comes from scanning forms, you may have to add some positional checking.

How convert image into 16bit zip (deflate) compressed TIF in python?

We got 50TB of 16bit uncompressed TIF images from a industrial sensor in our server, and we want to compress them all with lossless zip compression using python. Using python because it's easier to use Python to communicate our database.
However after hours of search and documentation reading, I found that there's not even a matured python library that can convert 16bit TIF into zip compressed tif. The latest PIL cannot write compressed tif, OpenCV hardcoded output file into LZW tif not zip(deflate). And there is no sufficient documentation in smc.freeimage, PythonImageMagick so I don't know if they can do it. I also found this tifffile.py, there seems something about compression in its source code, but there is no example code that let me understand how to config compression option for output.
Of course I can use an external executable, but I just don't want to use python as scripting language here.
So that I really appreciate if anyone give me an efficient example here, thanks.
Update:
cgohlke's code works, here I provide another light weight solution.
Checkout the patched pythontifflib code from here https://github.com/delmic/pylibtiff.
The original PythonTiffLib from google code doesn't handle RGB information well and it didn't work on my data, this patched version works, however because the code is very old, it implies PythonTiffLib may be not maintained very well.
Use the code like this:
from libtiff import TIFF
tif = TIFF.open('Image.tiff', mode='r')
image = tif.read_image()
tifw = TIFF.open('testpylibtiff.tiff', mode='w')
tifw.write_image(image, compression='deflate', write_rgb=True)
PythonMagick works for me on Windows:
from PythonMagick import Image, CompressionType
im = Image('tiger-rgb-strip-contig-16.tif')
im.compressType(CompressionType.ZipCompression)
im.write("tiger-rgb-strip-contig-16-zip.tif")
Scikit-image includes a wrapper for the FreeImage library:
import skimage.io._plugins.freeimage_plugin as fi
im = fi.read('tiger-rgb-strip-contig-16.tif')
fi.write(im, 'tiger-rgb-strip-contig-16-zip.tif',
fi.IO_FLAGS.TIFF_ADOBE_DEFLATE)
Or via tifffile.py, 2013.11.03 or later:
from tifffile import imread, imsave
im = imread('tiger-rgb-strip-contig-16.tif')
imsave("tiger-rgb-strip-contig-16-zip.tif", im, compress=6)
These might not preserve all other TIFF tags or properties but that wasn't specified in the question.

Categories

Resources