LinuxCommandLibrary

jpegtran

Transform and optimize JPEG images losslessly

TLDR

Mirror an image horizontally or vertically

$ jpegtran [[-f|-flip]] [horizontal|vertical] [path/to/image.jpg] > [path/to/output.jpg]
copy

Rotate an image 90, 180 or 270 degrees clockwise
$ jpegtran [[-ro|-rotate]] [90|180|270] [path/to/image.jpg] > [path/to/output.jpg]
copy

Transpose the image across the upper-left to lower right axis
$ jpegtran -transpose [path/to/image.jpg] > [path/to/output.jpg]
copy

Transverse the image across the upper right to lower left axis
$ jpegtran -transverse [path/to/image.jpg] > [path/to/output.jpg]
copy

Convert the image to grayscale
$ jpegtran [[-g|-grayscale]] [path/to/image.jpg] > [path/to/output.jpg]
copy

Crop the image to a rectangular region of width W and height H from the upper-left corner, saving the output to a specific file
$ jpegtran -crop [W]x[H] -outfile [path/to/output.jpg] [path/to/image.jpg]
copy

Crop the image to a rectangular region of width W and height H, starting at point X and Y from the upper-left corner
$ jpegtran -crop [W]x[H]+[X]+[Y] [path/to/image.jpg] > [path/to/output.jpg]
copy

SYNOPSIS

jpegtran [options] [input_file] [output_file]
input_file can be omitted to read from standard input. If output_file is omitted, output is written to standard output.

PARAMETERS

-copy
    Specifies which markers (metadata) to copy from the input file to the output. none copies no markers, comments copies only comment markers, and all copies all markers.

-crop
    Crops the image to the specified width (W) and height (H) starting at X,Y coordinates. Dimensions are in pixels.

-flip
    Flips the image horizontally or vertically.

-perfect
    Causes jpegtran to abort if the transformation cannot be performed losslessly (e.g., due to dimensions not being multiples of MCU size).

-optimize
    Optimizes the Huffman coding tables for smaller file size without changing image quality. Also known as -optim.

-progressive
    Creates a progressive JPEG file, which loads in multiple passes, typically preferred for web display.

-outfile
    Specifies the output file name. If not used, output goes to standard output.

-rotate <90|180|270>
    Rotates the image clockwise by the specified degrees (90, 180, or 270).

-trim
    Trims off any partial MCU (Minimum Coded Unit) blocks from the edges when cropping or rotating, ensuring transformations remain on whole blocks.

-transpose
    Performs a transpose operation (flips along top-left to bottom-right diagonal).

-transverse
    Performs a transverse transpose operation (flips along top-right to bottom-left diagonal).

-grayscale
    Converts the image to grayscale. Note that this operation is lossy as it discards color information.

-verbose
    Enables verbose output, showing details about the transformation process.

DESCRIPTION

jpegtran is a powerful command-line utility for performing lossless transformations on JPEG image files. Unlike typical image editors that decompress and recompress an image (which introduces generation loss), jpegtran manipulates the JPEG data directly at the Minimum Coded Unit (MCU) block level. This enables operations such as rotating images by 90, 180, or 270 degrees, flipping them horizontally or vertically, and cropping them without any degradation in visual quality.
Beyond geometrical transformations, jpegtran can also be used to optimize Huffman tables for smaller file sizes (via the -optimize or -progressive options), remove or selectively copy metadata (-copy), and convert an image to a progressive JPEG format for faster web loading. Its primary advantage lies in preserving the original image fidelity while making common adjustments, making it an indispensable tool for photographers, web developers, and anyone managing large collections of JPEG images.

CAVEATS

jpegtran operates on JPEG's internal block structure (MCUs). While most transformations are lossless for the data, certain operations like cropping or rotation of images with dimensions not perfectly divisible by the MCU block size (typically 8x8 or 16x16 pixels) might result in minor edge artifacts or require the use of -trim to preserve block alignment. The -grayscale option, despite being available in jpegtran, is inherently lossy as it converts color information to shades of gray.

COMMON USE CASES

jpegtran is frequently used in scenarios where image quality is paramount and file size optimization is desired, such as rotating photos without recompression, preparing images for web display, or bulk processing of images to remove sensitive metadata.
For example, to rotate an image 90 degrees clockwise and optimize its size:
jpegtran -rotate 90 -optimize input.jpg > output.jpg

HISTORY

jpegtran is an integral part of the widely used libjpeg library distribution, which is maintained by the Independent JPEG Group (IJG). It emerged as a dedicated utility for performing transformations on JPEG data without requiring a full decode-encode cycle, thereby preserving image quality. Its development has closely tracked the evolution of the libjpeg library itself, making it a foundational tool for JPEG manipulation on Linux and Unix-like systems. It's also included in modern forks like mozjpeg.

SEE ALSO

cjpeg(1), djpeg(1), rdjpgcom(1), wrjpgcom(1), convert(1) (ImageMagick), exiftool(1)

Copied to clipboard