Home > Enterprise >  Converting NRRD file to JPG
Converting NRRD file to JPG

Time:05-16

i am trying to convert nrrd file into jpg by reading that nrrd image using pynrrd and then using the pixel i am trying to form an image but the out i am getting is terrible contrast image. Below is waht i have tried

import numpy as np
import nrrd
from PIL import Image
import numpy as np
filename = "/content/drive/MyDrive/CT.nrrd"
readdata, header = nrrd.read(filename)
print(readdata.shape) # (512, 512, 504)
for i in range(504):
  if i == 200:
    img = np.array(readdata[:,:,i])
    print(img.shape)
    print(np.amax(img))
    img = (np.maximum(img, 0) / img.max()) * 255.0
    img = Image.fromarray(np.uint8(img), mode = "L")
    img.save(f'/content/testrgb{i}.png')
    break

The output i am getting is this enter image description here

Can someone please help me this

CodePudding user response:

from PIL import Image import numpy as np import nrrd

def manipulating_nrrd_contrast(img, level):
    img_c = img.astype(int).copy()
    factor = (8 * (level 255)) / (255 * (259-level)) #This 8 here is the value that i changes manually by try and test and found out that this works best 
    img_c = factor * (img_c - 128)   128
    img_c = np.clip(img_c, 0, 255)
    return img_c.astype(np.uint8)

filename = "/content/drive/MyDrive/CT.nrrd"
readdata, header = nrrd.read(filename)
tag_info = get_meta_data(header)
print(tag_info)
print(readdata.shape)
for i in range(readdata.shape[2]):
  b = np.asarray(readdata[:,:,i]).astype(int)
  final = Image.fromarray(manipulating_nrrd_contrast(b, 128))
  final.save(f'/content/png/testrgb{i}.png')

And, The Result that i got is below enter image description here

  • Related