taokz / BiomedGPT

BiomedGPT: A Generalist Vision-Language Foundation Model for Diverse Biomedical Tasks
https://www.nature.com/articles/s41591-024-03185-2
Apache License 2.0
511 stars 58 forks source link

can one use dicom images for training instead of jpg #49

Open yellowblue6 opened 16 hours ago

yellowblue6 commented 16 hours ago

hello Author, thanks for the work. can one use dicom(.dcm) images for training instead of jpg ?

taokz commented 7 hours ago

Yes, you can extract the pixel data or image from the DICOM file and then encoding it in base64 format. For example,

# Function to convert DICOM to base64
def dicom_to_base64(dicom_file_path):
    dicom = pydicom.dcmread(dicom_file_path)
    pixel_array = dicom.pixel_array
    image = Image.fromarray(pixel_array)

    # Convert the image to PNG and save it into a BytesIO object
    buffered = BytesIO()
    image.save(buffered, format="PNG")

    # Get the binary content and encode it into base64
    img_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')

    return img_base64