
cjpeg Command in Linux
cjpeg is a Linux command that allows you to compress an image file to a JPEG format. You can use the cjpeg command to convert various input formats, such as BMP, RLE, PGM, PPM, and Targa to a JPEG file. You can also adjust the quality setting, trading off file against image quality. Lower quality settings will result in generating smaller files, while higher settings yield better visual fidelity.
Table of Contents
Here is a comprehensive guide to the options available with the cjpeg command −
- How to Install cjpeg Command in Linux?
- Syntax of cjpeg Command
- cjpeg Command Options
- Examples of cjpeg Command in Linux
How to Install cjpeg Command in Linux?
The cjpeg command is not pre-installed on your Linux system, however, you can install it easily from your systems package manager. To use the cjpeg command, you must install a package called libjpeg-turbo-progs. Once you install this package, you will be able to use the cjpeg command on your Linux system.
In Linux distributions like Ubuntu, Debian that use the apt package manager, you can use the following command to install the libjpeg-turbo-progs package −
sudo apt install libjpeg-turbo-progs

In other Linux distributions like CentOS and Fedora that use the yum package manager, you can run the below-given command to install the libjpeg-turbo-progs package −
sudo yum install libjpeg-turbo-progs
Once, you are done with the installation of the libjpeg-turbo-progs package, simply run the following command to confirm the cjpeg installation −
cjpeg -version

Syntax of cjpeg Command
The basic syntax to use the cjpeg command in Linux is provided below −
cjpeg [options] [filename]
Here, the options in the above cjpeg command allow you to customize the compression process.
cjpeg Command Options
With cjpeg command, you can use different options to change the behavior of the command. These options are provided in the table given below −
Option | Description |
---|---|
-quality N | Adjusts image quality (0 to 100, default is 75). |
-grayscale | Creates a monochrome JPEG from color input. |
-rgb | Creates an RGB JPEG without converting to YCbCr colorspace. |
-optimize | Optimizes entropy encoding parameters (usually makes the file smaller). |
-progressive | Creates a progressive JPEG. |
-targa | Specifies Targa format input. |
-dct int | Specifies the DCT method (0 to 2, default is 1). |
-dct fast | Uses a faster DCT method (less accurate). |
-dct float | Uses a floating-point DCT method (more accurate but slower). |
-icc file | Embeds an ICC profile from the specified file. |
-memdst | Writes output directly to memory instead of file (useful for scripting). |
-report | Generates a report on compression progress. |
-strict | Treats all warnings as errors. |
-arithmetic | Uses arithmetic coding. |
-baseline | Generates a baseline JPEG (no progressive mode). |
-scans file | Specifies custom scan parameters from a file. |
-qtables file | Specifies custom quantization tables from a file. |
-qslots N | Sets the number of quantization table slots (1 to 4, default is 2). |
-sample HxV | Sets the horizontal and vertical sampling factors (e.g., -sample 2x1). |
-restart N | Specifies the restart interval (0 to 65535, default is 0). |
-smooth N | Specifies the smoothing factor (0 to 100, default is 0). |
-maxmemory N | Limits memory usage (in KB). |
-outfile filename | Specifies the output file name (useful for scripting). |
-verbose | Enables verbose output (useful for debugging). |
-help | Opens help section and exists. |
-version | Displays the version information and exists. |
For more details about these options, you can open cjpeg manual on the terminal using the below-given command −
man cjpeg
Examples of cjpeg Command in Linux
Lets explore some basic examples of cjpeg command in Linux −
- Compress PPM File with Quality 60
- Create Monochrome JPEG from PPM
- Optimize JPEG Parameters
- Convert PPM to RGB JPEG
- Covert Targa Format to JPEG
Compress PPM File with Quality 60
Image compression plays a crucial role in various scenarios, including web development, storage, and transmission. You can compress an image file with your desired quality by using the cjpeg command with the -quality option.
For example, we have a PPM image file named sample.ppm, and we want to compress it with a quality factor of 60. Wecan achieve this using the cjpeg command as follows −
cjpeg -quality 60 sample.ppm > sample.jpg

Create Monochrome JPEG from PPM
When you want to convert a color image to a smaller monochrome (grayscale) JPEG, you can use the cjpeg command with the -grayscale option. For instance, the following command will convert the PPM image named sample.ppm to grayscale JPEG image −
cjpeg -grayscale sample.ppm > output.jpg

Optimize JPEG Parameters
You can also optimize the entropy encoding parameters of your image file by using the -optimize option with the cjpeg command. For example, the below-provided command optimizes the parameters of a PPM file and output the results in the form of a JPEG file −
cjpeg -optimize sample.ppm > opt.jpg

Convert PPM to RGB JPEG
If you want to create an RGB JPEG without converting to the YCbCr colorspace, you can use the -rgb option. The command for an example to do such conversion is provided below −
cjpeg -rgb sample.ppm > rgb.jpg

Convert TARGA Format to JPEG
If you have an image file in TARGA format i.e, .tga, you can use the -targa option to convert that image to a JPEG image format. For instance, the following command will convert the TARGA image called sample.tga into the JPEG image file named output.jpg −
cjpeg -targa sample.tga > output.jpg

Thats how you can use the cjpeg command with different options and apply compression operation on your desired image file.
Conclusion
The cjpeg is a powerful command used in Linux for image compression and manipulation. Whether you need to adjust image quality, create monochrome versions, optimize parameters, or perform other tasks, cjpeg provides a versatile set of options.
We have provided details about the cjpeg command including its syntax, different options with their description and examples for better understanding. You should explore these capabilities and apply them as needed.