How to use the command 'rawtopgm' (with examples)
The ‘rawtopgm’ command is used to convert a raw greyscale image to a PGM image. PGM (Portable Graymap) is a grayscale image format that stores pixel intensities from 0 (black) to a maximum value (white). This command provides various options to manipulate the conversion process and customize the output image.
Use case 1: Convert a raw greyscale image to a PGM image
Code:
rawtopgm width height path/to/image.raw > path/to/output.pgm
Motivation: This use case is used when you have a raw greyscale image and want to convert it into a PGM image.
Explanation:
- ‘rawtopgm’ is the command used for the conversion.
- ‘width’ and ‘height’ are the dimensions of the input image.
- ‘path/to/image.raw’ is the path to the raw greyscale image that needs to be converted.
- ‘path/to/output.pgm’ is the output file path where the converted PGM image will be saved.
Example output: The raw greyscale image will be converted to a PGM image and saved at the specified output file path.
Use case 2: Convert a raw greyscale image to a PGM image, assume the image to be a square
Code:
rawtopgm path/to/image.raw > path/to/output.pgm
Motivation: This use case is used when you have a raw greyscale image that is square in shape, eliminating the need to explicitly provide the width and height values.
Explanation:
- ‘rawtopgm’ is the command used for the conversion.
- ‘path/to/image.raw’ is the path to the raw greyscale image that needs to be converted.
- ‘path/to/output.pgm’ is the output file path where the converted PGM image will be saved.
Example output: The raw greyscale image, assuming it is a square, will be converted to a PGM image and saved at the specified output file path.
Use case 3: Convert a raw greyscale image in which the pixels come bottom-first instead of top-first to a PGM image
Code:
rawtopgm width height -bottomfirst path/to/image.raw > path/to/output.pgm
Motivation: This use case is used when you have a raw greyscale image where the pixels are stored in a bottom-first order instead of the usual top-first order. This option allows correcting the pixel order during the conversion.
Explanation:
- ‘rawtopgm’ is the command used for the conversion.
- ‘width’ and ‘height’ are the dimensions of the input image.
- ‘-bottomfirst’ is an option to handle the bottom-first pixel order.
- ‘path/to/image.raw’ is the path to the raw greyscale image that needs to be converted.
- ‘path/to/output.pgm’ is the output file path where the converted PGM image will be saved.
Example output: The raw greyscale image with corrected pixel order will be converted to a PGM image and saved at the specified output file path.
Use case 4: Ignore the first n bytes of the specified file
Code:
rawtopgm width height -headerskip n path/to/image.raw > path/to/output.pgm
Motivation: This use case is used when you need to ignore the first ’n’ bytes of the raw greyscale image file. This option is valuable when the image file contains metadata or irrelevant information at the beginning that should not be included in the conversion.
Explanation:
- ‘rawtopgm’ is the command used for the conversion.
- ‘width’ and ‘height’ are the dimensions of the input image.
- ‘-headerskip n’ is an option to skip the first ’n’ bytes of the file.
- ‘path/to/image.raw’ is the path to the raw greyscale image that needs to be converted.
- ‘path/to/output.pgm’ is the output file path where the converted PGM image will be saved.
Example output: The raw greyscale image with the ignored first ’n’ bytes will be converted to a PGM image and saved at the specified output file path.
Use case 5: Ignore the last m bytes of each row in the specified file
Code:
rawtopgm width height -rowskip m path/to/image.raw > path/to/output.pgm
Motivation: This use case is used when you need to ignore the last ’m’ bytes of each row in the raw greyscale image file. It can be useful if the image file contains padding or additional information at the end of each row that should be excluded from the conversion.
Explanation:
- ‘rawtopgm’ is the command used for the conversion.
- ‘width’ and ‘height’ are the dimensions of the input image.
- ‘-rowskip m’ is an option to skip the last ’m’ bytes of each row.
- ‘path/to/image.raw’ is the path to the raw greyscale image that needs to be converted.
- ‘path/to/output.pgm’ is the output file path where the converted PGM image will be saved.
Example output: The raw greyscale image with the ignored last ’m’ bytes in each row will be converted to a PGM image and saved at the specified output file path.
Use case 6: Specify the maxval for the grey values in the input to be equal to N
Code:
rawtopgm width height -maxval N path/to/image.raw > path/to/output.pgm
Motivation: This use case is used when you want to specify the maximum intensity value, ‘maxval’, for the grey values in the input image. By default, the maximum intensity value is determined based on the maximum intensity found in the input image.
Explanation:
- ‘rawtopgm’ is the command used for the conversion.
- ‘width’ and ‘height’ are the dimensions of the input image.
- ‘-maxval N’ is an option to set the ‘maxval’ for the grey values to be equal to ‘N’.
- ‘path/to/image.raw’ is the path to the raw greyscale image that needs to be converted.
- ‘path/to/output.pgm’ is the output file path where the converted PGM image will be saved.
Example output: The raw greyscale image with the specified ‘maxval’ for the grey values will be converted to a PGM image and saved at the specified output file path.
Use case 7: Specify the number of bytes that represent each sample in the input and that the byte-sequence is to be interpreted as little-endian
Code:
rawtopgm width height -bpp 1|2 -littleendian path/to/image.raw > path/to/output.pgm
Motivation: This use case is used when the raw greyscale image has a different byte representation for each sample (pixel), such as 1 byte or 2 bytes. The ‘-bpp’ option is used to specify the number of bytes per sample, and the ‘-littleendian’ option indicates that the byte sequence is to be interpreted as little-endian.
Explanation:
- ‘rawtopgm’ is the command used for the conversion.
- ‘width’ and ‘height’ are the dimensions of the input image.
- ‘-bpp 1|2’ is an option to specify the number of bytes per sample, either 1 or 2.
- ‘-littleendian’ is an option to interpret the byte sequence as little-endian.
- ‘path/to/image.raw’ is the path to the raw greyscale image that needs to be converted.
- ‘path/to/output.pgm’ is the output file path where the converted PGM image will be saved.
Example output: The raw greyscale image with the specified bytes per sample and little-endian interpretation will be converted to a PGM image and saved at the specified output file path.
Conclusion:
The ‘rawtopgm’ command is a versatile tool for converting raw greyscale images to PGM format. With its various options, users can customize the conversion process according to their specific requirements. Whether it’s manipulating the pixel order, skipping header or row bytes, setting maximum intensity values, or adjusting byte representation and interpretation, the ‘rawtopgm’ command provides flexibility and control over the conversion process.