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).
Related
i have a file with 300 .tif images, i want to open it with python and save them into an array, list or any similar structure. I´ve tried the code below but only appears 3 images. ¿Any solution?. Thanks.
raw = cv2.imread('sen_15Hz_500Hz_6_6A.tif')
print(np.shape(raw))
I´ve tried with PIL and cv2 too.
If you really have one single TIFF file with 300 images in it, the simplest is to open it with tifffile.
with TiffFile('temp.tif') as tif:
for page in tif.pages:
image = page.asarray()
If you want to use PIL look at the section "Image Sequences" here.
I have a dataset of knee bone MRI but it is in .pck format can anyone help me regarding converting it into the .jpeg format or .png
If you know what Python library was used to create or load the images that were saved as .pcks, yes.
You first need to load the pickle files as objects again using the pickle.load() method (documentation here) and then save them again using whatever library was used to make them in the first place. In the case of OpenCV, you would use the cv2.imwrite() method, for PIL it's Image.save(). If you aren't using those, just look up the documentation for whatever library you're using on saving images.
If you don't know what library was used to make the images, you'll have to see what the data you get after loading the file looks like before you can do anything else.
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.
For a project I am needing to parse pixel data from a large number of online images. I realised it could well be faster to load the images into programme memory with a get request, carry out the required operations, then move onto the next image - removing the necessity for reading and writing these into storage. However in doing this I have ran into several problems, is there a not (overly) complicated way to do this?
Edit: I didn't include code as as far as I can tell everything I've seen (scikit-image, pillow, imagemagick) is a complete dead end. Not looking for somebody to write code for me, just a pointer in the right direction.
Its easy to load image directly from url.
import PIL
from PIL import Image
import urllib2
url = "https://cdn.pixabay.com/photo/2013/07/12/12/58/tv-test-pattern-146649_1280.png"
img = PIL.Image.open(urllib2.urlopen(url))
Image is now loaded.
Getting pixels is also easy: Get pixel's RGB using PIL
I am trying to implement the package:
https://pyradiomics.readthedocs.io/en/latest/usage.html
It looks super simple, but they expect .nrrd files.
My files are .nii.gz. How do I solve this?
Also, have anyone tried to apply PyRadiomics on TCIA data? if so, can I see your github or Jupyter Notebook?
Thanks a lot.
You could turn NII into numpy array firstly and then turn it into NRRD with using:
nrrd and nibabel
import numpy as np
import nibabel as nib
import nrrd
# Download NII
example_filename = "image.nii.gz"
image = nib.load(example_filename)
# Turn into numpy array
array = np.array(img.dataobj)
# Save NRRD
nrrd_path_to = "image.nrrd"
nrrd.write(image_path_to, array)
Although the examples are in .nrrd, PyRadiomics uses SimpleITK for image operations. This allows PyRadiomics to support a whole range of image formats, including .nii.gz. You don't have to convert them.
The DWIConverter converts diffusion-weighted MR images in DICOM series into nrrd format for analysis in Slicer. It parses the DICOM header to extract necessary information about measurement frame, diffusion weighting directions, b-values, etc, and write out a nrrd image. For non-diffusion weighted DICOM images, it loads in an entire DICOM series and writes out a single DICOM volume in a .nhdr/.raw pair.
So that trying to convert your .nii.gz inside DICOM files for the nrrd format is a possibility by using this tools. Also, you can look at the SlicerDMRI that is a similar module.