Chapter 03 - Image Processing and Acquisition using Python_Part8
The document discusses image processing in Python using the PIL and numpy libraries, detailing how to convert images to grayscale and manipulate them as numpy arrays. It also covers reading and writing DICOM images using the pyDICOM module, highlighting the similarities with standard image formats. Additionally, it explains how to save images in various formats using the cv2.imwrite function.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
2 views
Chapter 03 - Image Processing and Acquisition using Python_Part8
The document discusses image processing in Python using the PIL and numpy libraries, detailing how to convert images to grayscale and manipulate them as numpy arrays. It also covers reading and writing DICOM images using the pyDICOM module, highlighting the similarities with standard image formats. Additionally, it explains how to save images in various formats using the cv2.imwrite function.
# Converting ndarray to image for saving using PIL.
im3 = Image.fromarray(img2) In the above code, we import Image from the PIL module. We open the ’Picture.png’ image and convert a three-channel image to a single- channel grayscale by using convert(’L’) and the result is a PIL Image object. We then convert this PIL Image object to a numpy ndarray using the np.array function because most image processing modules in Python can only handle a numpy array and not a PIL Image object. After performing some image processing operation on this ndarray, we convert the ndarray back to an image using Image.fromarray, so that it can be saved or visualized.
3.5.2 Reading DICOM Images using pyDICOM
We will use pyDICOM [Mas20], a module in Python to read or write
or manipulate DICOM images. The process for reading DICOM images is similar to JPEG, PNG, etc. Instead of using cv2, the pyDICOM module is used. The pyDICOM module is not installed by default in the distributions. Please refer to the pyDICOM documentation at [Mas20]. To read a DICOM file, the DICOM module is first imported. The file is then read using the “read file” function. import dicom ds = dicom.read_file("ct_abdomen.dcm") 52 Image Processing and Acquisition using Python
3.5.3 Writing Images
Throughout this book, to write or save images we will use
cv2.imwrite. The cv2.imwrite function supports the following file for- mats:
• JPEG files: *.jpeg, *.jpg, *.jpe
• Portable Network Graphics: *.png
• Portable image format: *.pbm, *.pgm, *.ppm
• TIFF files: *.tiff, *.tif
Here is an example code snippet where we read an image and write
an image. The imwrite function takes the file name and the ndarray of an image as input. The file format is identified using the file extension in the file name.
import cv2 img = cv2.imread('image1.png')
# cv2.imwrite will take an ndarray.
cv2.write('file_name', img)
In the subsequent chapters, we will continue to use the above
approach for writing or saving images.
3.5.4 Writing DICOM Images using pyDICOM
To write a DICOM file, the DICOM module is first imported. The
file is then written using the “write file” function. The input to the function is the name of the DICOM file and also the array that needs to be stored.