Convert Jpg images to DCM images using python script

Question:

How are JPEGs and PNGs converted to dicom readable images? There doesn’t seem to be information about that on the internet. I’ve seen posts about converting dicom images to JPEGs, but not the other way round.
I tried the following code,but the image(dcm) I got is not opening-

import os
import tempfile
import datetime

import pydicom
from pydicom.dataset import Dataset, FileDataset

# Create some temporary filenames
suffix = '.dcm'
filename_little_endian ='bb.dcm'
filename_big_endian = 'testr.dcm'

print("Setting file meta information...")
# Populate required values for file meta information
file_meta = Dataset()
file_meta.MediaStorageSOPClassUID = "1.2.840.10008.5.1.4.1.1.7"
file_meta.MediaStorageSOPInstanceUID = "1.2.3"
file_meta.ImplementationClassUID = "1.2.3.4"

print("Setting dataset values...")
# Create the FileDataset instance (initially no data elements, but file_meta
# supplied)
ds = FileDataset(filename_little_endian, {},
                 file_meta=file_meta, preamble=b"" * 128)

# Add the data elements -- not trying to set all required here. Check DICOM
# standard
ds.PatientName = "Test^Firstname"
ds.PatientID = "123456"

# Set the transfer syntax
ds.is_little_endian = True
ds.is_implicit_VR = True

# Set creation date/time
dt = datetime.datetime.now()
ds.ContentDate = dt.strftime('%Y%m%d')
timeStr = dt.strftime('%H%M%S.%f')  # long format with micro seconds
ds.ContentTime = timeStr

print("Writing test file", filename_little_endian)
ds.save_as(filename_little_endian)
print("File saved.")

# Write as a different transfer syntax XXX shouldn't need this but pydicom
# 0.9.5 bug not recognizing transfer syntax
ds.file_meta.TransferSyntaxUID = pydicom.uid.ExplicitVRBigEndian
ds.is_little_endian = False
ds.is_implicit_VR = False

print("Writing test file as Big Endian Explicit VR", filename_big_endian)
ds.save_as(filename_big_endian)

# reopen the data just for checking
for filename in (filename_little_endian, filename_big_endian):
    print('Load file {} ...'.format(filename))
    ds = pydicom.dcmread(filename)
    print(ds)

Please tell where I am going wrong?

Asked By: vithika

||

Answers:

I am not sure if there any python packages which convert jpg/png images to Dicom file. There are several system packages which do the conversion like img2dcm, gdcmimg.

Install the required system package with your system package manager.

# linux
$ sudo apt install -y dcmtk

# mac
$ brew install dcmtk

Once it is installed, you can convert it jpg to Dicom from the shell with this command.

$ img2dcm test.jpg test.dcm

Then from your python script call the command.

import os

command = 'img2dcm test.jpg test.dcm'
os.system(command)

This will convert the jpg file to Dicom file.

Answered By: Chillar Anand

i found this package that might help, just provide the jpeg file, but make sure to also check if the image is rgb or grayscale before doing so
https://pycad.co/convert-jpg-or-png-images-into-dicom/

Answered By: ahmad dana
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.