jpegtran Command in Linux



jpegtran is a Linux command that allows you to perform lossless transformation on JPEG images. This means that you can modify your images without losing any quality. The jpeg command helps you rotate, flip, and transpose images or convert them to grayscale. The command is pretty useful in case you want to optimize images by reducing their size or improving compression. Apart from that, the command can also help crop images and remove unnecessary padding.

Table of Contents

Here is a comprehensive guide to the options available with the jpegtran command −

Installation of jpegtran Command

By default, the jpegtran command isn’t preinstalled on your Linux systems. However, it can be installed directly from the default system’s package manager.

1. Ubuntu/Debian

sudo apt install libjpeg-progs

2. CentOS/RHEL

sudo yum install libjpeg-turbo-utils

3: Fedora

sudo dnf install libjpeg-turbo-utils

4: Arch Linux

sudo pacman -S libjpeg-turbo

5: OpenSUSE

sudo zypper install libjpeg8

Syntax of jpegtran Command

The general syntax for jpegtran on Linux is as follows −

jpegtran [switches/options] [inputfile]

Where −

  • [switches/options] are the various options you can use to specify the type of transformation you want to perform on the JPEG file.
  • [inputfile] is the path to the input JPEG file that you want to transform.

jpegtran Command Options

The following are some options or switches that can be used with the jpegtran command in Linux −

OptionDescription
-arithmeticUses arithmetic coding instead of Huffman coding for potentially smaller files.
-flip horizontalMirrors the image horizontally (left to right).
-flip verticalMirrors the image vertically (top to bottom).
-grayscaleTurns the image to black and white, removing all colors.
-optimizeOptimizes the entropy encoding parameters to reduce file size without quality loss.
-outfileSpecifies the output file’s name.
-progressiveCreates a progressive JPEG, which loads in stages for better viewing on slow connections.
-restart NAdds a restart marker after every N MCU rows or blocks to enhance error recovery.
-rotate 180Rotates the image 180 degrees.
-rotate 270Rotates the image 270 degrees clockwise (or 90 degrees counterclockwise).
-rotate 90Rotates the image 90 degrees clockwise.
-scans fileUses a custom scan script from a specified file to control encoding order.
-transposeFlips the image along its diagonal from the top-left to the bottom-right corner.

Examples of jpegtran Command in Linux

Let’s explore a few examples of jpegtran command in Linux −

1. Horizontal Flip

If you want to create a mirror image of your photo along the vertical axis, you can use the -flip horizontal option with jpegtran. This command will take your original image and produce a new one that is flipped horizontally. For example −

jpegtran -flip horizontal path/to/image.jpg > path/to/output.jpg

2. 90-degree Rotation

To rotate an image by 90 degrees clockwise, you can use the -rotate 90 option. This is handy when you need to change the orientation of a photo, such as when a picture is taken in landscape mode but you want it in portrait mode. For example −

jpegtran -rotate 90 path/to/image.jpg > path/to/output.jpg

This command will rotate the image and save the result in a new file.

3. Grayscale Conversion

If you need to convert a color image to grayscale, the -grayscale option will remove all color information, leaving you with a black-and-white image. This can be useful for artistic purposes or to reduce file size. For example −

jpegtran -grayscale path/to/image.jpg > path/to/output.jpg

The resulting image will only contain shades of gray, which can highlight textures and contrasts.

4. Image Cropping

To crop an image to a specific rectangular region, use the -crop WxH+X+Y option. Here, W denotes the width, H is the height, while X and Y are the coordinates of the top-left corner of the cropping area. Here’s an example −

jpegtran -crop 100x100+10+10 path/to/image.jpg > path/to/output.jpg

This command will extract a 100x100 pixel area starting 10 pixels from the left and 10 pixels from the top of the original image.

5. Diagonal Transpose

For a diagonal flip along the upper-left to lower-right axis, the -transpose option is used. This can create interesting effects by flipping the image diagonally. For example −

jpegtran -transpose path/to/image.jpg > path/to/output.jpg

This command mirrors the image along the diagonal axis, which can be useful for certain types of image corrections or artistic transformations.

6. Diagonal Transverse

To flip an image diagonally along the upper-right to lower-left axis, you can use the -transverse option. This is another way to create a diagonal mirror effect. Here’s an example −

jpegtran -transverse path/to/image.jpg > path/to/output.jpg

This command will produce a new image that is mirrored diagonally in the opposite direction compared to the -transpose option.

Conclusion

The jpegtran is a powerful tool for performing lossless transformations on JPEG files. It offers a range of functionalities such as mirroring, rotating, transposing, and converting images to grayscale, all while maintaining the original image quality.

In this tutorial, we have provided details on the basic syntax, options, and practical examples of the command. Whether you need to flip an image, correct its orientation, or convert it to grayscale, jpegtran is a straightforward and efficient way to handle such tasks.