compress
Writes a file's pixel data in a compressed transfer syntax: RLE, JPEG-LS, JPEG 2000, HTJ2K, lossless or lossy JPEG, JPEG XL, or Deflated Image Frame Compression. Everything else in the file is kept.
When to use it
Use compress to make files smaller for storage or transfer, to produce a
file in the syntax a peer or viewer needs, or to make test files. Compressed
pixel data is decoded first, so a file moves from one syntax to another
directly.
The lossless syntaxes return every sample as it was. A lossy syntax is used
only when you give its parameter (-quality, -near, -ratio or -psnr):
what to lose is never chosen for you. To send files compressed without
writing them first, storescu takes the same -syntax names.
The closest dcmtk tools are dcmcrle, dcmcjpeg, dcmcjpls and
dcmcjp2k; in pydicom, ds.compress().
Examples
Lossless
go-dicom compress -syntax rle ct.dcm ct-rle.dcm
go-dicom compress -syntax jpeg-ls ct.dcm ct-jls.dcm
go-dicom compress -syntax htj2k ct.dcm ct-htj2k.dcm
go-dicom compress -syntax jpeg2000 mr.dcm mr-j2k.dcmWrote ct-rle.dcm: RLE Lossless (1.2.840.10008.1.2.5)
Wrote ct-jls.dcm: JPEG-LS Lossless (1.2.840.10008.1.2.4.80)
Wrote ct-htj2k.dcm: HTJ2K Lossless (1.2.840.10008.1.2.4.201)
Wrote mr-j2k.dcm: JPEG 2000 Lossless (1.2.840.10008.1.2.4.90)On CT_small.dcm (39206 bytes) RLE gives 27506 bytes, JPEG-LS 20628, HTJ2K
20964. The SOP Instance UID is kept. -syntax also takes the UID itself:
-syntax 1.2.840.10008.1.2.4.90.
JPEG Baseline, lossy
go-dicom compress -syntax jpeg -quality 90 rgb.dcm rgb-jpeg.dcmWrote rgb-jpeg.dcm: JPEG Baseline (1.2.840.10008.1.2.4.50)
lossy: Lossy Image Compression is 01, and the instance has a new SOP Instance UID, 2.25.115494908300885828514153027756370068781Without -quality it is refused:
Error: compress: JPEG Baseline is lossy; give -quality from 1 to 100 to accept that.
A color image is written as YBR_FULL; -chroma 422 halves the color
resolution across (YBR_FULL_422) for a smaller file.
Warning
A lossy syntax loses detail at every setting. The output records the loss
(Lossy Image Compression 01, its ratio and method) and is given a new SOP
Instance UID, since a lossy copy is a different instance (PS3.3
C.7.6.1.1.5). Keep the original if it may be needed for diagnosis.
JPEG 2000 to a ratio
go-dicom compress -syntax jpeg2000-lossy -ratio 10 mr.dcm mr-lossy.dcmWrote mr-lossy.dcm: JPEG 2000 (1.2.840.10008.1.2.4.91)
lossy: Lossy Image Compression is 01, and the instance has a new SOP Instance UID, 2.25.209038795032225044382019226582099810620-ratio r keeps the codestream no larger than the samples over r; -psnr
asks for a minimum PSNR in decibels instead. The same applies to
jpeg2000-part2-lossy and htj2k-lossy.
Segmentation or any image, deflated
go-dicom compress -syntax deflated-frame mr.dcm mr-dfl.dcmWrote mr-dfl.dcm: Deflated Image Frame Compression (1.2.840.10008.1.2.8.1)From one compressed syntax to another
go-dicom compress -syntax rle j2k.dcm j2k-rle.dcmWrote j2k-rle.dcm: RLE Lossless (1.2.840.10008.1.2.5)The JPEG 2000 data is decoded and written as RLE. A loss made before is kept on record.
Notes
- What a syntax takes: JPEG Baseline takes 8-bit unsigned grayscale and
color; JPEG Extended adds unsigned grayscale up to 12 bits; JPEG-LS with
-nearabove 0 takes unsigned images only (Error: compress: ct.dcm: a signed image is coded as JPEG-LS only losslessly, at NEAR 0: …); lossy JPEG 2000 and HTJ2K take signed images too. - An RGB image in lossless JPEG 2000 and HTJ2K is written as
YBR_RCT, in their lossy forms asYBR_ICT; lossless JPEG and JPEG-LS keepRGB. Planar Configuration becomes 0. -in-placewrites over the input through a new file renamed over it. Naming the input as output without it is refused.- Exit status is 0 when the file was written, 1 otherwise.
- See CONFORMANCE.md, Transfer syntaxes supported for files and Pixel data.
See also
Options
Write a file's pixel data in a compressed transfer syntax
USAGE:
go-dicom compress -syntax <name|uid> [options] <input> <output>
go-dicom compress -syntax <name|uid> [options] -in-place <file>
SYNTAXES:
rle RLE Lossless 1.2.840.10008.1.2.5
jpeg-ls (jpegls, jls) JPEG-LS Lossless 1.2.840.10008.1.2.4.80
jpeg-ls-near (jls-near) JPEG-LS Near-Lossless, -near 1.2.840.10008.1.2.4.81
jpeg2000 (j2k) JPEG 2000 Lossless 1.2.840.10008.1.2.4.90
jpeg2000-lossy JPEG 2000, lossy, -ratio 1.2.840.10008.1.2.4.91
(j2k-lossy) or -psnr
jpeg2000-part2 JPEG 2000 Part 2 Lossless 1.2.840.10008.1.2.4.92
(j2k-part2)
jpeg2000-part2-lossy JPEG 2000 Part 2, lossy, 1.2.840.10008.1.2.4.93
(j2k-part2-lossy) -ratio or -psnr
htj2k HTJ2K Lossless 1.2.840.10008.1.2.4.201
htj2k-rpcl HTJ2K Lossless, RPCL order 1.2.840.10008.1.2.4.202
htj2k-lossy HTJ2K, lossy, -ratio or 1.2.840.10008.1.2.4.203
-psnr
jpeg-lossless JPEG Lossless, Process 14 1.2.840.10008.1.2.4.57
jpeg-lossless-sv1 JPEG Lossless, predictor 1 1.2.840.10008.1.2.4.70
deflated-frame Deflated Image Frame 1.2.840.10008.1.2.8.1
(deflated-image-frame) Compression, any image,
1-bit segmentations too
jpeg-xl (jpegxl, jxl) JPEG XL Lossless 1.2.840.10008.1.2.4.110
jpeg (jpeg-baseline) JPEG Baseline, lossy, 1.2.840.10008.1.2.4.50
-quality
jpeg-extended JPEG Extended (12-bit), 1.2.840.10008.1.2.4.51
(jpeg-12bit) lossy, -quality
The lossless ones return every sample as it was. Compressed pixel data is
decoded first, so a file moves from one syntax to another. A lossy one is
used only when its parameter is given: what to lose is never chosen for you.
OPTIONS:
-syntax <name|uid> The syntax to write (required).
-quality <1-100> For jpeg and jpeg-extended, and required with them. They
lose detail at every quality.
-chroma <422|444> For jpeg: 422 codes color with its chroma halved across,
YBR_FULL_422, smaller and losing color detail finer than
two pixels; 444, the default, keeps every pixel's
chroma, YBR_FULL.
-near <0-255> For jpeg-ls-near, and required with it: every sample
comes back within this much of its value. 0 is lossless.
-ratio <r> For jpeg2000-lossy, jpeg2000-part2-lossy and
htj2k-lossy: the compression ratio to meet, the
codestream no larger than the samples over r.
-psnr <dB> For the same, instead of -ratio: the PSNR the decoded
image is to have at least.
-in-place Write over the input, through a new file renamed over it.
-h, -help Show this help message
WHAT CHANGES:
The transfer syntax and Pixel Data. A color image says Planar Configuration
0; an RGB image in lossless JPEG 2000 (Part 1 or 2) and HTJ2K is YBR_RCT, in
lossy JPEG 2000 (Part 1 or 2) and HTJ2K YBR_ICT, and in JPEG Baseline and Extended YBR_FULL; lossless JPEG and JPEG-LS keep
RGB. JPEG Baseline takes 8-bit unsigned grayscale and color; JPEG Extended
takes those and unsigned grayscale of up to 12 bits stored; JPEG-LS with
-near above 0 takes unsigned images. Lossy JPEG 2000 and HTJ2K take signed
images too.
Lossless: the SOP Instance UID is kept, and so is any record of a loss
made before.
Lossy: Lossy Image Compression becomes 01, its ratio and method
(ISO_10918_1, ISO_14495_1, ISO_15444_1 or ISO_15444_15) are added to any recorded, and the
instance is given a new SOP Instance UID, since a lossy copy is a different
instance (PS3.3 C.7.6.1.1.5).
EXAMPLES:
go-dicom compress -syntax rle ct.dcm ct-rle.dcm
go-dicom compress -syntax jpeg-ls -in-place mr.dcm
go-dicom compress -syntax jpeg-lossless-sv1 ct.dcm ct-jpll.dcm
go-dicom compress -syntax deflated-frame seg.dcm seg-deflated.dcm
go-dicom compress -syntax 1.2.840.10008.1.2.4.90 us.dcm us-j2k.dcm
go-dicom compress -syntax jpeg -quality 90 sc.dcm sc-jpeg.dcm
go-dicom compress -syntax jpeg -quality 90 -chroma 422 us.dcm us-jpeg.dcm
go-dicom compress -syntax jpeg-extended -quality 90 cr.dcm cr-jpeg12.dcm
go-dicom compress -syntax jpeg-ls-near -near 2 us.dcm us-jls.dcm
go-dicom compress -syntax jpeg2000-lossy -ratio 10 mr.dcm mr-j2k.dcm