# jpegtran — Transform and Optimize JPEG Files Losslessly

> Lossless JPEG transformations with jpegtran: rotate, flip, crop and optimize images without re-encoding or quality loss.

Source: https://www.jpkc.com/db/en/cheatsheets/images-media/jpegtran/

<!-- PROSE:intro -->
jpegtran transforms JPEG images entirely losslessly – without ever re-decoding and re-encoding them. Instead of re-rendering the picture, the tool rearranges the already-compressed DCT blocks directly: you rotate, flip, crop or optimize a file without losing a single extra bit of quality. That makes jpegtran the go-to choice when you want to slim down photos for the web or fix their orientation without introducing visible artifacts.
<!-- PROSE:intro:end -->

## Optimization

`jpegtran -optimize <input.jpg> > <output.jpg>` — Optimize Huffman coding tables (smaller file, no quality loss).

```bash
jpegtran -optimize photo.jpg > optimized.jpg
```

`jpegtran -progressive <input.jpg> > <output.jpg>` — Convert to progressive JPEG (better for web, loads gradually).

```bash
jpegtran -progressive photo.jpg > progressive.jpg
```

`jpegtran -optimize -progressive <input.jpg> > <output.jpg>` — Optimize and convert to progressive (best lossless compression).

```bash
jpegtran -optimize -progressive photo.jpg > optimized.jpg
```

`jpegtran -copy none -optimize -progressive <input.jpg> > <output.jpg>` — Optimize, make progressive, and strip all metadata.

```bash
jpegtran -copy none -optimize -progressive photo.jpg > clean.jpg
```

`jpegtran -arithmetic <input.jpg> > <output.jpg>` — Use arithmetic coding (smaller files, limited browser support).

```bash
jpegtran -arithmetic photo.jpg > arithmetic.jpg
```

## Rotation (Lossless)

`jpegtran -rotate 90 <input.jpg> > <output.jpg>` — Rotate 90 degrees clockwise (lossless).

```bash
jpegtran -rotate 90 photo.jpg > rotated.jpg
```

`jpegtran -rotate 180 <input.jpg> > <output.jpg>` — Rotate 180 degrees (lossless).

```bash
jpegtran -rotate 180 photo.jpg > rotated.jpg
```

`jpegtran -rotate 270 <input.jpg> > <output.jpg>` — Rotate 270 degrees / 90 counter-clockwise (lossless).

```bash
jpegtran -rotate 270 photo.jpg > rotated.jpg
```

`jpegtran -rotate 90 -trim <input.jpg> > <output.jpg>` — Rotate and trim partial MCU blocks at edges (avoids green edge artifacts).

```bash
jpegtran -rotate 90 -trim photo.jpg > rotated.jpg
```

`jpegtran -rotate 90 -perfect <input.jpg> > <output.jpg>` — Rotate only if it can be done perfectly (fail if dimensions not MCU-aligned).

```bash
jpegtran -rotate 90 -perfect photo.jpg > rotated.jpg
```

## Flip (Lossless)

`jpegtran -flip horizontal <input.jpg> > <output.jpg>` — Mirror horizontally (lossless).

```bash
jpegtran -flip horizontal photo.jpg > mirrored.jpg
```

`jpegtran -flip vertical <input.jpg> > <output.jpg>` — Mirror vertically (lossless).

```bash
jpegtran -flip vertical photo.jpg > flipped.jpg
```

`jpegtran -transpose <input.jpg> > <output.jpg>` — Transpose (mirror along top-left to bottom-right diagonal).

```bash
jpegtran -transpose photo.jpg > transposed.jpg
```

`jpegtran -transverse <input.jpg> > <output.jpg>` — Transverse (mirror along top-right to bottom-left diagonal).

```bash
jpegtran -transverse photo.jpg > transversed.jpg
```

## Crop (Lossless)

`jpegtran -crop <width>x<height>+<x>+<y> <input.jpg> > <output.jpg>` — Crop a region (coordinates must align to MCU boundaries, usually 8px or 16px).

```bash
jpegtran -crop 640x480+0+0 photo.jpg > cropped.jpg
```

`jpegtran -crop <width>x<height> <input.jpg> > <output.jpg>` — Crop from the top-left corner.

```bash
jpegtran -crop 800x600 photo.jpg > cropped.jpg
```

`jpegtran -crop <width>x<height>+<x>+<y> -trim <input.jpg> > <output.jpg>` — Crop with trimming to align to MCU boundaries.

```bash
jpegtran -crop 500x400+100+50 -trim photo.jpg > cropped.jpg
```

`jpegtran -drop +<x>+<y> <overlay.jpg> <input.jpg> > <output.jpg>` — Drop (overlay) another JPEG at a position (lossless splice).

```bash
jpegtran -drop +0+0 watermark.jpg photo.jpg > watermarked.jpg
```

## Metadata Control

`jpegtran -copy none <input.jpg> > <output.jpg>` — Strip all metadata (EXIF, comments, ICC profiles).

```bash
jpegtran -copy none photo.jpg > stripped.jpg
```

`jpegtran -copy comments <input.jpg> > <output.jpg>` — Keep only comment markers (jpegtran's default), remove EXIF and other markers.

```bash
jpegtran -copy comments photo.jpg > clean.jpg
```

`jpegtran -copy all <input.jpg> > <output.jpg>` — Preserve all extra markers, including EXIF, ICC profile and thumbnails.

```bash
jpegtran -copy all photo.jpg > output.jpg
```

`jpegtran -copy icc <input.jpg> > <output.jpg>` — Keep only ICC color profile, strip EXIF and comments.

```bash
jpegtran -copy icc photo.jpg > color-preserved.jpg
```

## Grayscale & Color

`jpegtran -grayscale <input.jpg> > <output.jpg>` — Convert to grayscale (lossless, removes chroma channels).

```bash
jpegtran -grayscale photo.jpg > grayscale.jpg
```

`jpegtran -grayscale -optimize <input.jpg> > <output.jpg>` — Convert to grayscale and optimize (significant size reduction).

```bash
jpegtran -grayscale -optimize photo.jpg > bw-optimized.jpg
```

## Common Patterns

`for f in *.jpg; do jpegtran -copy none -optimize -progressive "$f" > "opt-$f"; done` — Batch optimize all JPEGs in a directory.

```bash
for f in *.jpg; do jpegtran -copy none -optimize -progressive "$f" > "opt-$f"; done
```

`find . -name '*.jpg' -exec sh -c 'jpegtran -copy none -optimize -progressive "$1" > "$1.tmp" && mv "$1.tmp" "$1"' _ {} \;` — Recursively optimize all JPEGs in-place.

```bash
find . -name '*.jpg' -exec sh -c 'jpegtran -copy none -optimize -progressive "$1" > "$1.tmp" && mv "$1.tmp" "$1"' _ {} \;
```

`jpegtran -rotate 90 -copy all -optimize photo.jpg > rotated.jpg` — Lossless rotate while preserving metadata and optimizing.

```bash
jpegtran -rotate 90 -copy all -optimize photo.jpg > rotated.jpg
```

`jpegtran -copy none -optimize -progressive -outfile <output.jpg> <input.jpg>` — Use -outfile flag instead of stdout redirection.

```bash
jpegtran -copy none -optimize -progressive -outfile optimized.jpg photo.jpg
```

<!-- PROSE:outro -->
## Conclusion

jpegtran shines whenever every bit of quality matters: optimizing, rotating, flipping and cropping all run entirely losslessly, straight on the compressed data. Keep two things in mind, though. First, `-copy none` strips all metadata – including the EXIF orientation tag and the embedded ICC color profile; with smartphone photos in particular, the result can end up sideways or color-shifted. Use `-copy all` to keep those markers (the default `-copy comments` retains only comments). Second, lossless rotation only works cleanly when the image dimensions are divisible by the MCU size (8 or 16 pixels) – otherwise reach for `-perfect` (which aborts rather than rotating imperfectly) or `-trim` (which discards the incomplete edge blocks). And don't forget: jpegtran does not work in place but writes its result to stdout or via `-outfile` – redirect it to a different file, or you risk overwriting your original.

## Further Reading

- [libjpeg-turbo (GitHub)](https://github.com/libjpeg-turbo/libjpeg-turbo) – source and documentation of the widely used JPEG library that ships jpegtran
- [jpegtran(1) man page](https://manpages.debian.org/bookworm/libjpeg-turbo-progs/jpegtran.1.en.html) – complete reference for every command-line option
<!-- PROSE:outro:end -->

## Related Commands

- [convert](https://www.jpkc.com/db/en/cheatsheets/images-media/convert/) – convert and edit images with ImageMagick
- [optipng](https://www.jpkc.com/db/en/cheatsheets/images-media/optipng/) – losslessly optimize PNG files
- [mogrify](https://www.jpkc.com/db/en/cheatsheets/images-media/mogrify/) – batch-transform images in place (ImageMagick)

