How to use the command 'jpegtran' (with examples)
- Linux
- December 25, 2023
jpegtran is a command-line tool that allows users to perform lossless transformations on JPEG files. It provides various functionalities like mirroring, rotating, transposing, converting to grayscale, and cropping images. This article will illustrate each of these use cases with examples.
Use case 1: Mirror an image horizontally or vertically
Code:
jpegtran -flip horizontal|vertical path/to/image.jpg > path/to/output.jpg
Motivation: Mirroring an image can be useful when you want to create a reflected effect or correct the orientation of a flipped image.
Explanation:
-flip
: This argument specifies the direction of the flip, either “horizontal” or “vertical”.horizontal|vertical
: Specify whether to mirror the image horizontally or vertically.path/to/image.jpg
: The input JPEG file.> path/to/output.jpg
: Specify the path and filename for the output file.
Example output: The image will be mirrored either horizontally or vertically, depending on the specified direction.
Use case 2: Rotate an image
Code:
jpegtran -rotate 90|180|270 path/to/image.jpg > path/to/output.jpg
Motivation: Rotating an image can be useful when you need to correct the orientation or align it with other elements in a design project.
Explanation:
-rotate
: This argument specifies the rotation angle, either 90, 180, or 270 degrees clockwise.90|180|270
: Specify the rotation angle for the image (clockwise).path/to/image.jpg
: The input JPEG file.> path/to/output.jpg
: Specify the path and filename for the output file.
Example output: The image will be rotated by the specified angle (in degrees) clockwise.
Use case 3: Transpose an image
Code:
jpegtran -transpose path/to/image.jpg > path/to/output.jpg
Motivation: Transposing an image can be useful when you need to mirror the image diagonally across the upper-left to lower-right axis.
Explanation:
-transpose
: This argument transposes the image across the upper-left to lower-right axis.path/to/image.jpg
: The input JPEG file.> path/to/output.jpg
: Specify the path and filename for the output file.
Example output: The image will be transposed across the upper-left to lower-right axis.
Use case 4: Transverse an image
Code:
jpegtran -transverse path/to/image.jpg > path/to/output.jpg
Motivation: Transversing an image can be useful when you need to mirror the image diagonally across the upper-right to lower-left axis.
Explanation:
-transverse
: This argument transverses the image across the upper-right to lower-left axis.path/to/image.jpg
: The input JPEG file.> path/to/output.jpg
: Specify the path and filename for the output file.
Example output: The image will be transversed across the upper-right to lower-left axis.
Use case 5: Convert the image to grayscale
Code:
jpegtran -grayscale path/to/image.jpg > path/to/output.jpg
Motivation: Converting an image to grayscale can be useful when you want to remove color information or create a more vintage or artistic effect.
Explanation:
-grayscale
: This argument converts the image to grayscale.path/to/image.jpg
: The input JPEG file.> path/to/output.jpg
: Specify the path and filename for the output file.
Example output: The image will be converted to grayscale.
Use case 6: Crop the image to a rectangular region
Code:
jpegtran -crop WxH -outfile path/to/output.jpg path/to/image.jpg
Motivation: Cropping an image can be useful when you want to focus on a particular area of the image or remove unwanted parts.
Explanation:
-crop WxH
: This argument specifies the width and height of the rectangular region to be cropped.-outfile
: This argument specifies the path and filename for the output file.WxH
: Specify the width and height of the rectangular region in pixels.path/to/output.jpg
: Specify the path and filename for the output file.path/to/image.jpg
: The input JPEG file.
Example output: The image will be cropped to the specified width and height, and the result will be saved to the specified output file.
Use case 7: Crop the image to a rectangular region starting at a specific point
Code:
jpegtran -crop WxH+X+Y path/to/image.jpg > path/to/output.jpg
Motivation: Cropping an image starting at a specific point can be useful when you need to remove unwanted parts or focus on a specific area with a given top-left coordinate.
Explanation:
-crop WxH+X+Y
: This argument specifies the width, height, and starting point of the rectangular region to be cropped.WxH
: Specify the width and height of the rectangular region in pixels.X+Y
: Specify the top-left coordinate of the starting point.path/to/image.jpg
: The input JPEG file.> path/to/output.jpg
: Specify the path and filename for the output file.
Example output: The image will be cropped to the specified width and height, starting at the specified top-left coordinate.
Conclusion:
The jpegtran command provides a range of lossless transformation capabilities for JPEG files. From mirroring and rotating to transposing and cropping, these use cases offer flexibility in manipulating and editing JPEG images while maintaining their quality. Incorporating jpegtran into your image processing workflow can contribute to achieving desired visual effects and meeting specific design requirements.