Hello Friends,

i am trying to convert image files (ie. jpg, bmp, png) in DICOM images. but i dont get any help of article to convert it.

i try such articles but still not find any help.


if you have any idea please suggest us.

Thanks

Dani AI

Generated

Practical approach and a minimal, working recipe to convert JPG/PNG/BMP into a valid DICOM file (thread starter asked for a how‑to; pointed to the DICOM standard and flagged that image bytes belong in the PixelData element).

The easiest, most compatible route is: decode the source image to raw pixels, create a File Meta and a File Dataset, fill a small set of required tags, set pixel attributes correctly, write PixelData. Minimal tags to set: MediaStorage SOP Class/Instance UIDs (use the Secondary Capture SOP Class for non‑medical captures), Transfer Syntax UID, Implementation UID; PatientName/PatientID, StudyInstanceUID, SeriesInstanceUID, SOPInstanceUID/SOPClassUID, Modality (OT), Rows, Columns, SamplesPerPixel, PhotometricInterpretation, BitsAllocated/BitsStored/HighBit/PixelRepresentation, and PixelData. For RGB images include PlanarConfiguration (usually 0). Prefer Explicit VR Little Endian and uncompressed PixelData for broad viewer compatibility.

A compact Python example (pydicom + Pillow + numpy) that implements the above:

from pydicom.dataset import FileDataset, FileMetaDataset
import pydicom.uid
from PIL import Image
import numpy as np
import datetime

def image_to_dicom(in_path, out_path, patient_name="Anon", patient_id="1"):
    img = Image.open(in_path)
    arr = np.asarray(img)

    file_meta = FileMetaDataset()
    file_meta.MediaStorageSOPClassUID = pydicom.uid.SecondaryCaptureImageStorage
    file_meta.MediaStorageSOPInstanceUID = pydicom.uid.generate_uid()
    file_meta.TransferSyntaxUID = pydicom.uid.ExplicitVRLittleEndian
    file_meta.ImplementationClassUID = pydicom.uid.PYDICOM_IMPLEMENTATION_UID

    ds = FileDataset(out_path, {}, file_meta=file_meta, preamble=b"\0"*128)
    now = datetime.datetime.now()
    ds.ContentDate = now.strftime('%Y%m%d')
    ds.ContentTime = now.strftime('%H%M%S.%f')
    ds.PatientName = patient_name
    ds.PatientID = patient_id
    ds.Modality = "OT"
    ds.StudyInstanceUID = pydicom.uid.generate_uid()
    ds.SeriesInstanceUID = pydicom.uid.generate_uid()
    ds.SOPInstanceUID = file_meta.MediaStorageSOPInstanceUID
    ds.SOPClassUID = file_meta.MediaStorageSOPClassUID

    if arr.ndim == 2:
        ds.SamplesPerPixel = 1
        ds.PhotometricInterpretation = "MONOCHROME2"
    else:
        if arr.shape[2] == 4:
            arr = arr[:, :, :3]
        ds.SamplesPerPixel = arr.shape[2]
        ds.PhotometricInterpretation = "RGB"
        ds.PlanarConfiguration = 0

    ds.Rows, ds.Columns = arr.shape[0], arr.shape[1]
    ds.BitsAllocated = 8
    ds.BitsStored = 8
    ds.HighBit = 7
    ds.PixelRepresentation = 0

    if arr.dtype != np.uint8:
        arr = (arr.astype('float32') / arr.max() * 255).astype(np.uint8)

    ds.PixelData = arr.tobytes()
    ds.is_little_endian = True
    ds.is_implicit_VR = False
    ds.save_as(out_path)

Troubleshooting/cautions: drop alpha channels before embedding; ensure Rows/Columns and SamplesPerPixel match PixelData length; if aiming to keep JPEG compression inside DICOM, use a library that supports encapsulation (more complex). If real patient data is involved, remove or anonymize PHI and follow applicable rules. For production use, consider mature toolkits (pydicom, DCMTK/GDCM, fo‑dicom, commercial SDKs) rather than hand‑rolling everything.

Recommended Answers

All 2 Replies

DICOM datasets have many elements one of them is called "Pixel data" (NOT all datasets have this element) which contains the image(s).

If your requirement is to just extract/convert the DICOM images to other formats, you can probably find a small code to help you extract the images or you can use a free library to do that such as:
http://opendicom.sourceforge.net/

If you want to do more with the DICOM dataset, you will probably need to read more into DICOM and do some research. For example, in our case we did the research for transferring and displaying the DICOM datasets while being under still time frame so we used a library to help facilitate those things called leadtools and it helped us.
It all comes to what your requirements are.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.