Example of converting DICOM image into JPG image by Python


Main principle: adjust the window width of DICOM to make the gray value of each pixel scale to the range of [0255].

The python library used: simpleitk

Here is a demo to convert DICOM (. DCM) images into jpg images:

import SimpleITK as sitk
import numpy as np
import cv2

def convert_from_dicom_to_jpg(img,low_window,high_window,save_path):
  lungwin = np.array([low_window*1.,high_window*1.])
  Newimg = (IMG lungwin [0]) / (lungwin [1] - lungwin [0]) normalization
  Newimg = (newimg * 255). Astype ('uint8 ') # extends the pixel value to [0255]
  cv2.imwrite(save_path, newimg, [int(cv2.IMWRITE_JPEG_QUALITY), 100])

if __name__ == '__main__':

  #The following is the corresponding DICOM format image into jpg
  dcm_ image_ path = '/DICOM_ Image / lung001. DCM '# read DICOM file
  output_jpg_path = 'JPG_image/lung001.jpg'
  Ds_ array =  sitk.ReadImage (dcm_ image_ Path) # read relevant information of DICOM file
  img_ array =  sitk.GetArrayFromImage (ds_ Array) # get array
  #The coordinate order of image data read by simpleitk is ZYX, that is, from how many slices to the width and height of a single slice. Here we read a single slice, so img_ Shape of array
  #Similar to (1, height, width)
  shape = img_array.shape
  img_ array =  np.reshape (img_ Array, (shape [1], shape [2]) # get the height and width in the array
  high = np.max(img_array)
  low = np.min(img_array)
  convert_ from_ dicom_ To_ jpg(img_ array, low, high, output_ jpg_ Call the function, convert it to JPG file and save it to the corresponding path

The above example of converting DICOM image into JPG image by Python is all the content shared by Xiaobian. I hope it can give you a reference and support developer.

Recommended Today

Summary of recent use of gin

Recently, a new project is developed by using gin. Some problems are encountered in the process. To sum up, as a note, I hope it can help you. Cross domain problems Middleware: func Cors() gin.HandlerFunc { return func(c *gin.Context) { //Here you can use * or the domain name you specify c.Header(“Access-Control-Allow-Origin”, “*”) //Allow header […]