Decompressing a compressed image using PIL - python

I have compressed an image in python2.7 using PIL library like this:
picture.save("Compressed_"+file,"JPEG",quality=85)
Can I decompress it?
I have searched so many sites, but couldn't find decompression related information.
I have to compress an image (should reduce file size) and decompress it back,what to do?

No.
JPEG use a lossy compression method to do compression, which means there will be some data lost during compression. This data lost is irreversible.
You may try some lossless compressing image format, for example PNG.

Related

How to find the original format of images (pages) present in a tiff file using python?

I have a multi-page tiff file (merged.tiff) out of which I need to extract individual images in their original format. PIL allows you to iterate through pages and writing them to disk in a format I need (png/jpg).
Ex:
from PIL import Image
img = Image.open('merged.tiff')
for i in range(img.n_frames):
try:
img.seek(i)
img.save(f'individual_{i}.jpg')
img.save(f'individual_{i}.png')
except EOFError:
break
But is there a way to know the original format of those images?
I have tried with tifffile and tiffany which allow me to convert the pages to a numpy array and then write to disk as an image, but they don't allow me to know the source format of the images contained in the TIFF file.
In the most general case, I believe this is impossible, because it is perfectly feasible to take, say, a JPEG image and include it in the TIFF file as an uncompressed RGB array.
Realistically, though, you should be able to look at some of the tags of the TIFF file, e.g. Compression, to make an educated guess about what the image used to be. Tools like tiffinfo and tiffdump (from the libtiff package) can be used to examine the TIFF file.

How to split multi-page TIFF

I have a multi-page TIFF file and I want to split it to several png files. Normally, imageMagick and graphicMagick tools are used to deal with it.
But error occurs. I already try to use many other codes like PIL but it does not work.
gm convert: JPEG compressed data indicates unexpected subsampling values. (OJPEGReadHeaderInfoSecStreamSof).
If your TIFF is poorly formed and generally "unhappy", you'll probably find that the tools that accompany libtiff are most permissive in their handling, so I would try:
tiffsplit document.tiff
Then use regular ImageMagick to convert the component parts to PNG individually.

RLE8 image support/decompression with Pillow (PIL fork)

I'm using Pillow (version 5.2.0) on Python3 to open both PNG and BMP images, and display them with a Tkinter GUI. The PNG images display correctly with no issues, however, I'm encountering an IOError ("Unsupported BMP compression") with some of the BMP images, when Pillow's BmpImagePlugin.py is used.
Using the bitmap plugin's source and some print statements, I found that the exception is thrown at line 193, and that the images causing the exception are compressed using RLE8 (denoted by the dictionary on line 63); all others work because they're a RAW format.
It would seem to me that if a compression type is listed in that dictionary it should be supported, but apparently that isn't the case.
My question: is anyone aware of a workaround in Pillow or of any other python library that can open RLE8 bitmap images?
Here's an image displaying my PATH environment, as well as the command-line error described in a comment below.
Path issues
I note that your first image (test1.bmp) appears to be corrupt and ImageMagick reports it has incorrect length.
Your second image does not appear to be compressed with RLE8 compression and also is a palettised image, but with alpha/transparency.
Your third image is palletised, non-alpha with RLE8 compression.
My version of PIL can read only the second file - the first and third, which are RLE encoded cannot be read.
You asked for a workaround - may I suggest pyvips which can read the files without issues:
import pyvips
from PIL import Image
# Load troublesome file using vips, and write to a memory buffer
image = pyvips.Image.new_from_file('test1.bmp')
mem_img = image.write_to_memory()
# Read from memory buffer into Numpy array
imgnp=np.frombuffer(mem_img, dtype=np.uint8).reshape(image.height, image.width, 3)
# Convert Numpy array to PIL Image and write to disk
Image.fromarray(imgnp).save('result.png')

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.

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