dicom icon indicating copy to clipboard operation
dicom copied to clipboard

Can not load dicom

Open elviskuo07 opened this issue 2 years ago • 1 comments

I can't load this dicom, buy pydicom can with "force=True" parameter. Does there have any hotfix for this kind dicom?

err message read.go:534: error reading value unexpected EOF test-dicom.dcm.zip

elviskuo07 avatar Mar 15 '22 01:03 elviskuo07

You are getting this error because the dicom file which you are trying to parse has no preamble (128 bytes zeros) and magic bytes ('D', 'I', 'C', 'M'). If you'd like to have detailed information about the dicom format and file structure please have a look at (https://dicom.nema.org/dicom/2013/output/chtml/part10/chapter_7.html).

Regarding the problem, you can try to insert the preamble and magic at the beginning of your file and then parse it. You can apply the following snippet, but it's important to note that this is not the best solution. You may need to consider better solution in different cases.

package main

import (
	"bytes"
	"fmt"
	"log"
	"os"

	"github.com/suyashkumar/dicom"
)

func main() {
	dcmMagic := make([]byte, 128)
	dcmMagic = append(dcmMagic, []byte{'D', 'I', 'C', 'M'}...)
	buf, _ := os.ReadFile("test-dicom.dcm")
	rd := bytes.NewReader(append(dcmMagic, buf...))
	ds, err := dicom.Parse(rd, rd.Size(), nil)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(ds)
}

itp-atakan avatar Mar 21 '22 20:03 itp-atakan

Thanks for helping out the community @itp-atakan! Sounds like maybe this isn't an issue? Note that we have a compatability mode that tries to read the dataset without the magic word: https://github.com/suyashkumar/dicom/blob/961275650f24563c1d4738a20829ee4c41983d18/read.go#L149

suyashkumar avatar Aug 26 '23 20:08 suyashkumar