How to convert an image with PILLOW temporarily? - python

I know that PILLOW can convert an image from say jpg to png using the save method but is there a way to convert the image to another format and just keep it as an Image object without actually saving it as another file?
So I want to convert a user supplied image to common format for working with in the program because certain tools I am using only support png.

jpg and png are just compression techniques for saving an image to a file. An image as an object, is just an array of RGB(or any other colorspace/format used by the library which you used to read the file) values of all the pixels.
So technically, you can use the image object as the common format for working with other tools. But you need to keep in mind about the colorspace which is used by each library. Like, OpenCV considers an image object in BGR format, so you need to convert the image object to this format before you use it in OpenCV.

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 should python convert tiff image to jpg format without having to save to harddrive?

I'm trying to process some images and obtain numerical output. The skimage library only works with jpg format images. I only have tiff images on hand. Most converting functions work by loading a tiff image and saving it in jpg format. I do agree that the easiest way is
PIL.Image.open('pic.tiff').save('pic.jpg','jpeg')
I'm, on the other hand, trying to abstain from using hard drive for several reasons, but mainly due to the complexity file handling on heroku. Hence the question.

How can I insert EXIF/other metadata into a JPEG stored in a memory buffer?

I have created a JPEG using Python OpenCV, EXIF data being lost in the process and apparently not being able to be re-added when calling imwrite (reference: Can't keep image exif data when editing it with opencv in python).
Two questions:
In general, how can I write the original EXIF data/new custom metadata into a JPEG that exists in memory rather than a file?
Would pillow/PIL be able to maintain the EXIF data and allow supplementary metadata to be added? As of 2013 (reference: how maintain exif data of images resizes using PIL) this did not seem possible except via a tmp file (which is not an option for me).
Thanks as ever
I'm not certain I understand what you are trying to do, but I think you are trying to process an image with OpenCV and then re-insert the EXIF data you lost when OpenCV opened it...
So, hopefully you can do what you are already doing, but also open the image with PIL/Pillow and extract the EXIF data and then write it into the image processed by OpenCV.
from PIL import Image
import io
# Read your image with EXIF data using PIL/Pillow
imWithEXIF = Image.open('image.jpg')
You will now have a dict with the EXIF info in:
imWIthEXIF.info['exif']
You now want to write that EXIF data into your image you processed with OpenCV, so:
# Make memory buffer for JPEG-encoded image
buffer = io.BytesIO()
# Convert OpenCV image onto PIL Image
OpenCVImageAsPIL = Image.fromarray(OpenCVImage)
# Encode newly-created image into memory as JPEG along with EXIF from other image
OpenCVImageAsPIL.save(buffer, format='JPEG', exif=imWIthEXIF.info['exif'])
Beware... I am assuming in the code above, that OpenCVImage is a Numpy array and that you have called cvtColor(cv2.COLOR_BGR2RGB) to go to the conventional RGB channel ordering that PIL uses rather than OpenCV's BGR channel ordering.
Keywords: Python, OpenCV, PIL, Pillow, EXIF, preserve, insert, copy, transfer, image, image processing, image-processing, dict, BytesIO, memory, in-memory, buffer.

Poor quality with Wand convert function (Python) [duplicate]

I convert files of different formats (JPEG, PNG, TIFF, PDF) to JPEG using Wand, a ctypes-based ImageMagick binding for Python. The resulting files are very low-quality. If there is text in original file, it becomes almost unreadable in the resulting file.
Before Wand i used Imagemagick console commands, and with the option -density i could achieve great quality. For example: convert -density 200 file.pdf file.jpg.
What is the most idiomatic way to improve image quality of the resulting image file in Wand? Or, at least, how do i set the density option in Wand?
This would help you. Pass resolution option to the constructor of Image e.g.:
with Image(filename='file.pdf', resolution=200) as image:
image.compression_quality = 99
image.save(filename='file.jpg')

digital image read as an array of integers

Using Python's PIL module, we can read an digital image into an array of integers,
from PIL import Image
from numpy import array
img = Image.open('x.jpg')
im = array(img) # im is the array representation of x.jpg
I wonder how does PIL interpret an image as an array? First I tried this
od -tu1 x.jpg
and it indeed gave a sequence of numbers, but how does PIL interpret a color image into a 3D array?
In short, my question is that I want to know how can I get a color image's array representation without using any module like PIL, how could do the job using Python?
Well, it depends on the image format I would say.
For a .jpg, there is a complete description of the format that permits to read the image .
You can read it here
What PIL does is exactly what you did at first. But then it reads the bytes following the specifications, which allow it to transform this into a human readable format (in this case an array).
It may seem complex for JPEG, but if you take png (the version without compression) everything can seem way more simple.
For example this image
If you open it, you will see something like that :
You can see several information on top that corresponds to the header.
Then you see all those zeroes, that are the numerical representation of black pixels of the image (the top left corner).
If you open the image with PIL, you will get an array that is mostly filled with 0.
If you want to know more about the first bytes, look at the png specifications chapter 11.2.2.
You will see that some of the bytes corresponds to the width and height of the image. This is how PIL is able to create the array :).
Hope this helps !
Depends on the color mode. In PIL an image is stored as a list of integers with all channels interleaved on a per-pixel basis.
To illustrate this:
Grayscale image: [pixel1, pixel2, pixel3, ...]
RGB: [pixel1_R, pixel1_G, pixel1_B, pixel2_R, pixel_2_G, ...]
Same goes for RBGA and so on.

Categories

Resources