When working with medical images (like DICOM), use encode_12bit with specific pixel formats and subsampling to ensure maximum quality. You can specify pixel_format (e.g., TJPF_GRAY) and jpeg_subsample (e.g., TJSAMP_GRAY) to match the source data.
import numpy as np
from turbojpeg import TurboJPEG, TJPF_GRAY, TJSAMP_GRAY
jpeg = TurboJPEG()
# Create a 12-bit medical image (e.g., DICOM format)
medical_img = np.random.randint(0, 4096, (512, 512, 1), dtype=np.uint16)
# Encode with the highest quality for medical applications
jpeg_medical = jpeg.encode_12bit(
medical_img,
pixel_format=TJPF_GRAY,
jpeg_subsample=TJSAMP_GRAY,
quality=100
)
# Decode for analysis
decoded_medical = jpeg.decode_12bit(jpeg_medical, pixel_format=TJPF_GRAY)