I am trying to add a label image to a dicom SM file using pydicom. I did """pilImage =…
- User
I am trying to add a label image to a dicom SM file using pydicom. I did """pilImage = pilImage.convert('RGB') pixels = np.array(pilImage) refds = pydicom.dcmread(anypath, stop_before_pixels=True) file_meta = pydicom.FileMetaDataset() file_meta.MediaStorageSOPClassUID = '1.2.840.10008.5.1.4.1.1.77.1.6' file_meta.MediaStorageSOPInstanceUID = uid file_meta.TransferSyntaxUID = pydicom.uid.ExplicitVRLittleEndian ds = pydicom.Dataset() ds.file_meta = file_meta ds.SOPClassUID = file_meta.MediaStorageSOPClassUID ds.SOPInstanceUID = uid ds.StudyInstanceUID = refds.StudyInstanceUID ds.PatientID = getattr(refds, 'PatientID', '') ds.PatientName = getattr(refds, 'PatientName', '') ds.StudyID = getattr(refds, 'StudyID', '') ds.StudyDate = getattr(refds, 'StudyDate', datetime.datetime.now().strftime('%Y%m%d')) ds.StudyTime = getattr(refds, 'StudyTime', datetime.datetime.now().strftime('%H%M%S')) ds.SeriesInstanceUID = refds.SeriesInstanceUID ds.SeriesNumber = seriesNum ds.SeriesDescription = 'Label Image' ds.Modality = 'SM' ds.Manufacturer = getattr(refds, 'Manufacturer', 'Custom') ds.ContentDate = ds.StudyDate ds.ContentTime = ds.StudyTime ds.ImageType = ['ORIGINAL', 'PRIMARY', imgtype, 'NONE'] ds.SamplesPerPixel = 3 ds.PhotometricInterpretation = 'RGB' ds.PlanarConfiguration = 0 ds.Rows = pilImage.height ds.Columns = pilImage.width ds.BitsAllocated = 8 ds.BitsStored = 8 ds.HighBit = 7 ds.PixelRepresentation = 0 ds.PixelData = pixels.tobytes() ds.save_as(imgpath) return imgpath""" where imgpath is the path to write, uid is my SOPInstanceUID, and pilImage is a pilImage. But, the resultant file fails to read saying """pydicom.errors.InvalidDicomError: File is missing DICOM File Meta Information header or the 'DICM' prefix is missing from the header. Use force=True to force reading."""
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.