How convert image into 16bit zip (deflate) compressed TIF in python? - 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.

Related

OpenCV tiling TIFF image on loading with imread

I have a 32-bit 3-band TIF image that I am trying to load using OpenCV with Python. I'm specifically avoiding GDAL as it is not user-friendly to install on Windows, and this script is targeted at Windows machines.
When I try to load the image with imread (
img = cv2.imread(file, flags=(cv2.IMREAD_UNCHANGED | cv2.IMREAD_ANYDEPTH))
), and either write it out or imshow it, the 3 bands appear to be tiled, like so:
For comparison, rendering in Windows looks like this:
So there should be no issue from an OS support perspective.
GIMP Properties for image:
Is there a way to override this behaviour? Is there a known cause to this?
I found a solution. Using the tifffile library in conjunction with scikit-image, I was able to load my TIFFs in a format understandable by OpenCV.
Thus, my load statement became:
img = skimage.io.imread(file,plugin='tifffile')
And the image:

How to compress PNG image with S3TC/DXT algorithm in 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')

pillow jpeg file size

I'm trying to write a Python script that loads a jpeg file, rotates the image by 90 degrees, and then saves the result at about the same level of compression as the original. I've looked at the documentation at https://pillow.readthedocs.org/en/latest/reference/ImageFile.html, but don't see any way to control the size/quality of the output file.
Do you know the incoming quality? Which functions are you using? I just jumped over to the docs and the save function takes a named parameter "quality".
http://pillow.readthedocs.org/en/latest/handbook/image-file-formats.html
If you are looking for lossless rotation, you should not use Pillow. There is jpegtran program. It is part of libjpeg and libjpeg-progs package in Ubuntu.

Using TIFF G4 image in PIL

I wrote a pure python TIFF G4 decompress for use with tifffile.py. I know there are ways to add libtiff to a custom PIL, but I never could get that working very well in a mixed virtualenv. I want to manipulate the image in PIL. I am looking for pointers in hooking my decompressor to stock PIL for TiffImagePlugin.py.
Any ideas?
It appears that TiffImagePlugin does not easily allow me to hook in additional decompressors. Replacing TiffImageFile._decoder with a dictionary of decoders might work, but you would have to examine and test each release of PIL to ensure it hasn't broken. This level of maintenance is just as bad as a custom PIL. I appreciate the design of tifffile.py for using a dictionary of decoders. It made it very easy.
Final solution? I couldn't hook my code into PIL. I had to use PIL.Image.fromarray() to using my decompressed images.

Compress Images in Python (No Archive)

I'm writing a Python script that deals with images. Is there a module that can compress an image without putting it into an archive, and decompress it back? (e.g. A 1MB image is now 0.8MB after compression, then 1MB after decompression).
Can I see example code of compressing and decompressing an image in Python without the use of archives?
I've already taken a look at some modules, but those compress strings.
You probably want to take a look at the Python Image Library (PIL), and the PNG and JPEG formats.
The PIL Image.save() method will let you save PNG or JPEG images.
PNG - Lossless, good for "cartoony"/logo images with solid colors or small numbers of colors.
JPEG - Lossy, good for photos, images with lots "going on".
Modern image formats such PNG and JPEG are already compressed and my general recommendation is take Brendan Long's advice and use those formats and exploit all the work that's been put into them.
That said, if you want to compress the contents of any arbitrary file in Python, here's a very simple example:
import zlib
with open("MyImage.jpg", "rb") as in_file:
compressed = zlib.compress(in_file.read(), 9)
with open("MyCompressedFile", "wb") as out_file:
out_file.write(compressed)

Categories

Resources