how to convert jpeg to tiff file in python - python

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.

Related

Save 32-bit floating point TIFF image

I'm trying to save a 32-bit floating point image (stored as a Numpy array) as a TIFF file using tifffile.py.
import numpy as np
import tifffile
image = np.random.rand(500, 500, 3).astype(np.float32)
tifffile.imsave('image.tiff', image)
However, when viewing the output of the above code in Eye of Gnome, the image is entirely blank.
I think the problem is that not all tools support multi-channel TIFFs with 32-bits per channel. For example, as far as I can tell Python's PIL library does not. But I think tifffile.py does, because if I use your code I get a TIFF that opens, and looks reasonable, in GIMP:
From what I read, Photoshop can read 32-bit TIFFs too. So I think the TIFF file contains your image, but whether it works for you or not depends on what you want to do with it next.
This question might be relevant too, although it's about using 16-bit integers not floats: Python: Read and write TIFF 16 bit , three channel , colour images

How can I convert a .dds file to a .png in python?

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')

reading pgm images with cv2 in python

I want to read pgm image in python. I use cv2.imread('a.pgm') but it returns wrong results. In Matlab, I use imread and get the right result which is a single channel 16-bit image. But cv2.imread in python returns a 3-channel image and the pixel values are also wrong.
Why it happens?
How should I read the 16-bit pgm images in python?
And what libraries?
Thanks in advance.
I got it.
cv2.imread('a.pgm',-1)
works.
You can also use skimage.io library
from skimage.io import imread
image = imread("a.pgm")

How we can read 16 un signed integer(16 uint) jpeg files in python

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

Convert DICOM to TIFF

I'm new to Python so forgive my ignorance If I don't have all the info correct. I'm trying raster through a directory and convert all the DICOM files within to TIFF files. I have gotten the search functionality to work, but I am having a hard time saving the images as TIFFs. I'm using the pydicom libraries to read in the DICOM and manipulate the header information. Also, I have tried using the save_as function in pydicom to save to TIFF, but I would rather use the save function in PIL to properly set the compression of the TIFF. I think the problem is that I can't/don't understand how to extract the actual image data from a DICOM and place it in a new image.Any Help would be greatly appreciated ... Cheers
Python 2.7
PIL 1.1.7
Pydicom 0.9.6
Found an answer online to the same query sometime back, although I don't remember the site but as I applied it to my code, sharing it here for others as well :
import pylab
import dicom
ImageFile=dicom.read_file(<SourceFilePath>) #Path to "*.dcm" file
pylab.imshow(ImageFile.pixel_array,cmap=pylab.cm.bone) #to view image
or if you want to save the image then instead use:
pylab.imsave('<DestinationFilePath>',ImageFile.pixel_array,cmap=pylab.cm.bone)
The imsave will by default save the image in .png format though. You can specify the desired format in the imsave() if it is supported.
Hope it is useful.
If you know how to use PIL to save image data as .tiff, this example should help you to pass image data from pydicom to PIL (there is more here in the comments).

Categories