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')
Related
I am coding a python script which uses Pillow to create images.
In my code I import a jpeg background and another jpeg image which I'll combine with draw.rounded_rectangle() and draw.text().
Problem is when I save the image directly to a jpeg file, the colors are very different from the png image. It's like the jpeg image has a greyscale filter applied.
Here's my code to save the images :
background.save('fin2.jpg', format="jpeg", quality=100, subsampling=0)
background.save('fin2.png', format="png")
os.system('sips -s format jpeg fin2.png --out ' + 'fin3.jpg')
As you can notice I use Apple's sips (scriptable image processing system) to convert the png to jpeg and the output is fine.
Here are the output images :
fin2.jpg
fin2.png
fin3.png
Any idea how to fix this in the code ?
(I am unable to upload the pictures directly to stackoverflow atm this is why I use external links)
I am using OpenCV Python to read in images. I read them in as grayscale as reading in the full colour image is expensive. However, I still need to identify the number of colour channels in the original image.
Is there a simple method of extracting the number of colour channels in an image using EXIF tags, PIL or any other libraries without reading in the full colour image?
imagemagick: $ magick identify -ping <file> reads as little as possible of a file and dumps a few basic properties of the picture file. discussion: https://legacy.imagemagick.org/discourse-server/viewtopic.php?t=18042
you can run and read that using python's subprocess module. check_output() is probably most useful here.
I've just tried that on a 5 GB TIFF file and it runs in no time at all.
you will have to interpret that line somewhat. it says here "8-bit sRGB", which would imply three channels. sRGB is an RGB color space.
you can pass -format ... and a format string to get custom output.
imagemagick has a command line interface but I hear it also has APIs you can call from python. other answer with details on that: Can I access ImageMagick API with Python?
if that's unsuitable you will need to use specific libraries (libjpeg, libpng, libtiff, ...)
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.
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
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).