I am trying to use PIL to convert a PNG to a TGA. I want it to be a non compressed 32-bit image.
I don't know if, or where, it's documented, but some experimentation shows that PIL retains the compression of the input file. I mean, if you open an RLE ("Run Length Encoded") compressed file, it saves it with the same compression, whereas if you open an uncompressed file and subsequently save it, an uncompressed file will be written.
So, if you are getting a compressed file out, I am guessing you must be putting a compressed file into PIL. So, you need to explicitly tell PIL to override the compression like this:
from PIL import Image
# Open an RLE compressed file
im = Image.open('compressed.tga')
# Explicitly save uncompressed
im.save('uncompressed.tga', compression=None)
Keywords: Python, image processing, Targa, TGA, compressed, RLE, uncompressed
Related
Is there any way to convert .jpeg to .tiff file?
If yes, then how to do that?
There are many library in Python that can convert file from one format to another.
But, I have not found anything for this problem.
Thanks in advance!
see this
from PIL import Image
im = Image.open('yourImg.jpg')
im.save("pathToSave/hello.tiff", 'TIFF')
You can use PIL (Python Imaging Library) for this:
import Image
im = Image.open('test.jpg')
im.save('test.tiff') # or 'test.tif'
Also, this was the first result for your problem on Google, make sure you google extensively first.
According to OpenCV docs for functions used for image and video reading and writing imread does support JPEG files and imwrite can save TIFF files, though with some limitations:
Only 8-bit (or 16-bit unsigned (CV_16U) in case of PNG, JPEG 2000, and TIFF) single-channel or 3-channel (with ‘BGR’ channel order) images can be saved using this function.
I am trying to create some images by manipulating image pieces stored in .dds files, and then write the finished image as a .png. I see that there is a dds python module in Direct Python 11, which seems possibly sufficient except that it saves to .dds format. Is there a way to save to another image format?
The Python Imaging Library has a open() method that supports dds files and has a read and save() can save an image to many formats (png included).
Note that, at the moment, only DXT1, DXT3, and DXT5 pixel formats are supported and only in RGBA mode.
Since Python Imaging Library link is not awailable, I will show actual solution with wand library:
from wand import image
with image.Image(filename="white_rect_dxt3.dds") as img:
img.compression = "no"
img.save(filename="white_rect_dxt3.png")
And same from .png to .dds
from wand import image
with image.Image(filename='white_rect.png') as img:
img.compression = "dxt3"
img.save(filename='white_rect_dxt3.dds')
I know that ghostscript can convert pdf to tiff and even has python bindings but I'm wondering whether there is a way to avoid writing the resulting tiff to disk (-SOutputFile=/path/to/file.tiff. Rather I want to keep the resulting tiff in memory and use it as a PIL image.
Basically, no, not using the standard devices. That's because the strip size of the TIFF file (and potentially other entries in the TIFF header) can't be written until after the compressed bitmap has been created because the sizes aren't known. So you need to be able to seek back to the beginning and update the header.
Now you could modify the standard TIFF output devices so that they maintain the output in memory, rather than writing to disk, but that's not how they currently work.
Is it possible to use the Python wrappers for GDCM to decode the image data in a DICOM file?
I have a byte array / string with the bytes of the image data in a DICOM file (i.e. the contents of tag 7fe0,0010 ("Pixel Data")) and I want to decode the image to something raw RGB or greyscale.
I am thinking of something along the lines of this but working with just the image data and not a path to the actual DICOM file itself.
You can read the examples, there is one that shows how one can take an input compressed DICOM and convert it to an uncompressed one. See the code online here:
Decompress Image
If you are a big fan of NumPy, checkout:
This module add support for converting a gdcm.Image to a numpy array.
This is sort of a low level example, which shows how to retrieve that actual raw buffer of the image.
A much nicer class for handling Transfer Syntax conversion would be to use gdcm.ImageChangeTransferSyntax class (allow decompression of icon)
gdcm::ImageChangeTransferSyntax Class Reference
If you do not mind reading a little C++, you can trivially convert the following code from C++ to Python:
Compress Image
Pay attention that this example is actually compressing the image rather than decompressing it.
Finally if you really only have access to the data values contains in the Pixel Data attribute, then you should really have a look at (C# syntax should be close to Python syntax):
Decompress JPEG File
This example shows how one can decompress a JPEG Lossless, Nonhierarchical, First- Order Prediction file. The JPEG data is read from file but it should work if the data is already in memory for your case.
How we can read the 16 uint jpeg images in python
please suggest me the libraries which can read the these type of files in python.
i tried matplotlib, scipy, scikit-image, medpy ,Pil ,opencv, numpy libraries.
when we are using these libraries i am getting the out put as:
raise IOError("cannot identify image file")
IOError: cannot identify image file
please help me
find the file from the link
https://drive.google.com/file/d/0B4l5GiM7kBXraDEyMXdseENfUlE/edit?usp=sharing
Having 16-bit JPEG images sounds a bit strange, as the JPEG standard does not support 16-bit images. It has 12-bit images, though. Unfortunately, most readers only support the usual 8-bits/pixel RGB images, so even with the 12-bit images this may be a bit challenging.
One useful test could be to try:
hdr = open("myimage.jpeg", "rb").read(2)
print "{:02x} {:02x}".format(ord(hdr[0]), ord(hdr[1]))
If your file is a JPEG file, it should start with:
ff d8
If you do not get those, then the file is something else. Is there any program you can use to open the file? Which program produced the files?
This is the standard for 16bits grayscale lossless jpeg (recommendations ITU-T T.81). Now replaced with JPEG-LS and JPEG-2000.
This specific type of JPEG has single channel grayscale on a 16bits wide word, unlike 3 components RGB one on a 24bits/8 bits per channel.
Try using thorfdbg's libjpeg as it supports this type of old jpeg standard: https://github.com/thorfdbg/libjpeg