aiwan
aiwan

Reputation: 107

how to split a dicom image into tiles?

I'm trying to use the image_slicer function to split a DICOM image into tiles, but it's not recognizing the DICOM.

I've already read the DICOM and converted them into np arrays:

dcm_files[0]

array([[-1024, -1024, -1024, ..., -1024, -1024, -1024],
       [-1024, -1024, -1024, ..., -1024, -1024, -1024],
       [-1024, -1024, -1024, ..., -1024, -1024, -1024],
       ...,
       [-1024, -1024, -1024, ..., -1024, -1024, -1024],
       [-1024, -1024, -1024, ..., -1024, -1024, -1024],
       [-1024, -1024, -1024, ..., -1024, -1024, -1024]], dtype=int16)

and am able to view the image through:

from PIL import Image
import numpy as np

img = Image.fromarray(dcm_files[0])
img.show()

and then trying to slice it:

import image_slicer
image_slicer.slice(img, 64)

Error: 'Image' object has no attribute 'read'

thanks!

Upvotes: 2

Views: 456

Answers (1)

Bartłomiej
Bartłomiej

Reputation: 1078

The module image_slicer uses filenames, not file instances. Therefore you have to save the array to some localisation. You can use tempfile module for that purpose.

from PIL import Image
import numpy as np
import image_slicer
import tempfile

array = np.random.randint(0,200, size=(128,128), dtype='int32')

img = Image.fromarray(array)

temp = tempfile.NamedTemporaryFile()
img.save(temp.name, format="png")

image_slicer.slice(temp.name, 4)
#if you want to play with the slices:
tiles = image_slicer.slice(temp.name, 4, save=False)

Upvotes: 1

Related Questions