New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to convert dicom to jpeg #352
Comments
Not tested, but you should be able to use pillow and numpy: from PIL.Image import fromarray
ds = read_file('file-in.dcm')
im = fromarray(ds.pixel_array)
im.save('file-out.jpg') You will likely have to do some work to ensure you have the image data in a suitable format prior to saving. |
I think this can be closed as answered. |
This doesn't work for me, so I'm guessing it doesn't work for others. I'm including some code that converts a dicom file into a png for the next person who ends up here via google :-) import numpy as np
import png
import pydicom
ds = pydicom.dcmread(path)
shape = ds.pixel_array.shape
# Convert to float to avoid overflow or underflow losses.
image_2d = ds.pixel_array.astype(float)
# Rescaling grey scale between 0-255
image_2d_scaled = (np.maximum(image_2d,0) / image_2d.max()) * 255.0
# Convert to uint
image_2d_scaled = np.uint8(image_2d_scaled)
# Write the PNG file
with open(destination, 'wb') as png_file:
w = png.Writer(shape[1], shape[0], greyscale=True)
w.write(png_file, image_2d_scaled) |
@tedivm wonderful |
Thanks @tedivm. Wrapped your code into a function for convenient copy-paste
|
@Rohit-Patel - you forgot to |
Only solution that worked for my problem, thank you! |
@tedivm
|
I'm working on this problem recently. It turns out that there are many things to consider on converting DICOM to JPG.
Here is my preliminary package , please have a look, hope it would help.
|
I think there's some fundamental issues here. The Dicom image data has much larger range (eg. CT: -1024 HU to 3071 HU)and if you convert it to uint8, you loss information. |
@tommy-qichang so, is there some recommend ways to convert that not lost info? maybe convert to 3 channel ? |
I think PIL support I16 so @scaramallion 's solution should be better. |
PIL's JPEG2000 plugin doesn't support encoding signed data. It might "work" but it won't be correct. |
None of the answers worked for me with a single frame dicom, the colors were always off. Strange when using a multi frame dicom the following code worked fine
|
what i want is to get a jpeg file for each dcm file ?is there any best practices of pydicom ,i find nothing looking over documentation
The text was updated successfully, but these errors were encountered: