Is there a way in Python or any other language to convert .raw/mhd image data to Nifti/nii?
I can load the .raw/mhd file in python via SimpleITK as in this post:
Reading *.mhd/*.raw format in python
import skimage.io as io
img = io.imread('file.mhd', plugin='simpleitk')
I am having a hard time exporting as nii with proper dimensions... would ideally use the header information in the original mhd file...
Thanks
You should be able to just do in with SimpleITK. You would do something like this:
import SimpleITK as sitk
img = sitk.ReadImage("input.mhd")
sitk.WriteImage(img, "output.nii")
If you don't have SimpleITK in python, installing it as follows:
pip install SimpleITK
SimpleITK does its best effort of preserving all header information, although it's not perfect. Hopefully the voxel dimensions will be preserved.
Related
Through some http requests I have been able to receive an image in binary form as
b'\xff\xd8\xff\xe0\x00\...
and with:
with open('image.jpg', 'wb') as out_file:
out_file.write(binary_content)
where binary_content is a string containing the data received through request I saved an image into a file.
Afterwards I can read this image with OpenCV methods. But I wanted to do a direct pass from binary string to OpenCV Mat without any in-betweens. cv2.decode method didn't work.
io.BytesIO and PIL worked well. Closing this q.
If you want to stay in the SciPy ecosystem, then the imageio library (previously part of SciPy) works well.
from imageio import imread
image_array = imread("image_path.jpg")
The code above gives you an uint8 array, if you want a float array, you can cast it easily
from imageio import imread
image_array = imread("image_path.jpg").astype(float)
I currently have a .h5 file containing grayscale imagery. I need to convert it to a .jpg.
Does anybody have any experience with this?
Note: I could possible convert the h5 file to a numpy array and then use an external library like pypng to convert that to a png. But I am wondering if there is a more efficient way to convert to an image, and preferrably a .jpg.
Key ingredients:
h5py to read the h5 file.
Determine the format of your image and use PIL.
Let us suppose it's RGB format (https://support.hdfgroup.org/products/java/hdfview/UsersGuide/ug06imageview.html)
Suppose your image is located at Photos/Image 1 then you can do.
import h5py
import numpy as np
from PIL import Image
hdf = h5py.File("Sample.h5",'r')
array = hdf["Photos/Image 1"][:]
img = Image.fromarray(array.astype('uint8'), 'RGB')
img.save("yourimage.thumbnail", "JPEG")
img.show()
I'm trying to learn how to use pydicom for reading and processing dicom images. I'm using Python 3.
import dicom
import numpy
ds = pydicom.read_file(lstFilesDCM[0])
print(ds.pixel_array)`
I get an error NameError: name 'pydicom' is not defined. If I change
ds = pydicom.read_file(lstFilesDCM[0])
to
ds = dicom.read_file(lstFilesDCM[0])
(using dicom.read_file instead), I get the following error:
NotImplementedError: Pixel Data is compressed in a format
pydicom does not yet handle. Cannot return array
I also verified that pydicom is properly installed and updated.
How do i fix this?
You are trying to call a class that you have not imported before:
Use:
import pydicom
import numpy
ds = pydicom.read_file(lstFilesDCM[0])
print(ds.pixel_array)
or
import dicom
ds = dicom.read_file("the_name_of_file.dcm")
Documentation: http://pydicom.readthedocs.io/en/stable/pydicom_user_guide.html
If you want to get your hands on the pixel data, I suggest to use the convert program from the ImageMagick suite. You can either call this program from Python using the subprocess module. (See this example, where I convert them to JPEG format), or you can use one of the Python bindings.
If you want to manipulate the images, using the bindings might be preferable. But note that not all the bindings have been converted to ImageMagick version 7.
I'm using scikit-image to read an image:
img = skimage.io.imread(filename)
After doing some manipulations to img, I'd like to save it to an in-memory file (a la StringIO) to pass off to another function, but it looks like skimage.io.imsave requires a filename, not a file handle.
I'd like to avoid hitting the disk (imsave followed by read from another imaging library) if at all possible. Is there a nice way to get imsave (or some other scikit-image-friendly function) to work with StringIO?
Update: 2020-05-07
We now recommend using the imageio library for image reading and writing. Also, with Python 3, StringIO changes to BytesIO:
from io import BytesIO
import imageio
buf = BytesIO()
imageio.imwrite(buf, image, format='png')
scikit-image stores images as numpy arrays, therefore you can use a package such as matplotlib to do so:
import matplotlib.pyplot as plt
from StringIO import StringIO
s = StringIO()
plt.imsave(s, img)
This may be worth adding as default behaviour to skimage.io.imsave, so if you want you can also file an issue at https://github.com/scikit-image/scikit-image.
I am using the frombuffer command to save DICOM image data as TIFF images. But somehwere throughout this process, the image intensities are inverted (inverted LUT). Any idea on how to overcome this?
I have tried using the ImageOps.invert function from PIL, but if gives me "not supported for this image mode" error.
This is the code I'm using:
import dicom
import Image
import PIL.ImageOps
meta=dicom.read_file("DicomImage.dcm")
imHeight=meta.Rows
imWidth=meta.Columns
imSize=(imWidth,imHeight)
TT=Image.frombuffer("L",imSize,meta.PixelData,"raw","L",0,1)
TT.save("testOUTPUT.tiff","TIFF",compression="none")
Any guidance is appreciated ...
Python 2.7
PIL 1.1.7
Pydicom 0.9.6
Rather than "" for the raw mode, you should be using one of the mode strings from the documentation. Try "L" or "L;I", one or the other should be correct.