Working with image files on the Linux command line


While the best way to view or manipulate image files on Linux is to open them on your desktop for viewing or manipulating with tools like Gimp, there are quite a few ways to get important details on the command line.

Identifying image type by file extension

In general, image files can be identified on the command line by listing their names. Clearly “.jpg” represents a jpeg file, “.png” a portable network graphics file, “.gif” a graphics interchange format file, “.tiff” a tagged image file and so on.

$ ls -l images 
-rw-rw-r--. 1 shs shs  256093 Jul 15  2018 mycats.jpg
-rw-r-----. 1 shs shs  784238 Jul 15  2018 mycats.png
-rw-rw-r--. 1 shs shs    6760 Jul 15  2018 arrow.jpg
-rw-r-----. 1 shs shs    8853 Jul 15  2018 arrow.png

Nearly all of the time you can rely on file extensions accurately reporting the file type, but there’s more you can do with additional commands.

Identifying images with the file command

The file command can identify image file types by extracting data from the files themselves.

$ file Emmy.jpg
Emmy.jpg: JPEG image data, progressive, precision 8, 704x584, components 3

The file below is clearly a jpg file in spite of its name. The output from the file command reports this.

$ file camper.png
camper.png: JPEG image data, JFIF standard 1.01, resolution (DPI), density 72x72, segment length 16, Exif Standard: [TIFF image data, little-endian, direntries=11, manufacturer=samsung, model=SM-G935V, orientation=upper-left, xresolution=164, yresolution=172, resolutionunit=2, software=GIMP 2.8.18, datetime=2018:04:30 07:56:54, GPS-Data], progressive, precision 8, 3465x2717, components 3

For most jpg files these days, the file command will show a lot more details as in the examples above and below. This output includes the file type, resolution details and quite a bit of information about where the image came from and when it was taken.

$ file tulip_poplar.jpg
tulip_poplar.jpg: JPEG image data, Exif standard: [TIFF image data, big-endian, direntries=11, yresolution=146, xresolution=154, model=SM-A526U, height=2448, manufacturer=samsung, software=A526USQS5CVC9, orientation=upper-left, datetime=2022:05:17 19:58:50, width=2448], baseline, precision 8, 2448x2448, components 3

As you can see from the output above, the tulip_poplar.jpg file was taken on May 17, 2022, with a Samsung cell phone,

Viewing image details using an octal dump

You can view some image details using the od -bc (octal dump) command. However, looking beyond the first couple of lines will also display portions of the image itself that won’t be helpful. The first part of the file contains “meta information” (data that describes the content rather than the image content itself). For example, the second line below identifies the file as a png file.

$ od -bc coconuts.png | head -4
0000000 211 120 116 107 015 012 032 012 000 000 000 015 111 110 104 122
        211   P   N   G  r  n 032  n        r   I   H   D   R
0000020 000 000 011 274 000 000 010 257 010 006 000 000 000 017 251 304
             t 274      b 257  b 006       017 251 304

Extracting image details using exiftool

Another tool for displaying image metadata is exiftool. In the example command below, we can see extensive details on a png file.

$ exiftool coconuts.png
ExifTool Version Number         : 12.42
File Name                       : coconuts.png
Directory                       : .
File Size                       : 5.3 MB
File Modification Date/Time     : 2023:01:19 16:18:18-05:00
File Access Date/Time           : 2023:01:19 16:18:34-05:00
File Inode Change Date/Time     : 2023:01:19 16:18:18-05:00
File Permissions                : -rw-r--r--
File Type                       : PNG
File Type Extension             : png
MIME Type                       : image/png
Image Width                     : 2492
Image Height                    : 2223
Bit Depth                       : 8
Color Type                      : RGB with Alpha
Compression                     : Deflate/Inflate
Filter                          : Adaptive
Interlace                       : Noninterlaced
Pixels Per Unit X               : 11811
Pixels Per Unit Y               : 11811
Pixel Units                     : meters
Profile Name                    : Photoshop ICC profile
Warning                         : Install Compress::Zlib to read compressed information
ICC Profile                     : (Binary data 2616 bytes, use -b option to extract)
White Point X                   : 0.31269
White Point Y                   : 0.32899
Red X                           : 0.63999
Red Y                           : 0.33001
Green X                         : 0.3
Green Y                         : 0.6
Blue X                          : 0.15
Blue Y                          : 0.05999
Image Size                      : 2492x2223
Megapixels                      : 5.5

The file below was taken in 2016. The details stored within the image file itself are incredibly extensive. It includes not only details about when the photo was taken, but also where it was taken. Note the GPS longitude and latitude measurements near the bottom.

$ exiftool myboy.jpg
ExifTool Version Number         : 12.42
File Name                       : myboy.jpg
Directory                       : .
File Size                       : 3.4 MB
File Modification Date/Time     : 2021:05:25 15:31:09-04:00
File Access Date/Time           : 2023:01:01 15:44:38-05:00
File Inode Change Date/Time     : 2021:05:25 15:31:09-04:00
File Permissions                : -rw-r--r--
File Type                       : JPEG
File Type Extension             : jpg
MIME Type                       : image/jpeg
Exif Byte Order                 : Little-endian (Intel, II)
Make                            : samsung
Camera Model Name               : SM-G935V
Orientation                     : Rotate 90 CW
X Resolution                    : 72
Y Resolution                    : 72
Resolution Unit                 : inches
Software                        : G935VVRU2APD2
Modify Date                     : 2016:06:16 13:54:56
Y Cb Cr Positioning             : Centered
Exposure Time                   : 1/245
F Number                        : 1.7
Exposure Program                : Program AE
ISO                             : 50
Exif Version                    : 0220
Date/Time Original              : 2016:06:16 13:54:56
Create Date                     : 2016:06:16 13:54:56
Components Configuration        : Y, Cb, Cr, -
Shutter Speed Value             : 1/245
Aperture Value                  : 1.7
Brightness Value                : 5.2
Exposure Compensation           : 0
Max Aperture Value              : 1.7
Metering Mode                   : Center-weighted average
Light Source                    : Unknown
Flash                           : No Flash
Focal Length                    : 4.2 mm
Maker Note Version              : 0100
Device Type                     : Cell Phone
Raw Data Byte Order             : Little-endian (Intel, II)
Raw Data CFA Pattern            : Swap
Face Detect                     : Off
User Comment                    : .
Flashpix Version                : 0100
Color Space                     : sRGB
Exif Image Width                : 4032
Exif Image Height               : 3024
Interoperability Index          : R98 - DCF basic file (sRGB)
Interoperability Version        : 0100
Sensing Method                  : One-chip color area
Scene Type                      : Directly photographed
Exposure Mode                   : Auto
White Balance                   : Auto
Focal Length In 35mm Format     : 26 mm
Scene Capture Type              : Standard
Image Unique ID                 : C12QSJB01SB
GPS Latitude Ref                : North
GPS Longitude Ref               : West
GPS Altitude Ref                : Unknown (1.7)
GPS Time Stamp                  : 17:54:32
GPS Date Stamp                  : 2016:06:16
Compression                     : JPEG (old-style)
Thumbnail Offset                : 6142
Thumbnail Length                : 15400
Image Width                     : 4032
Image Height                    : 3024
Encoding Process                : Baseline DCT, Huffman coding
Bits Per Sample                 : 8
Color Components                : 3
Y Cb Cr Sub Sampling            : YCbCr4:2:0 (2 2)
Time Stamp                      : 2016:06:16 13:54:56-04:00
Aperture                        : 1.7
Image Size                      : 4032x3024
Megapixels                      : 12.2
Scale Factor To 35 mm Equivalent: 6.2
Shutter Speed                   : 1/245
Thumbnail Image                 : (Binary data 15400 bytes, use -b option to extract)
GPS Altitude                    : 0 m Above Sea Level
GPS Date/Time                   : 2016:06:16 17:54:32Z
GPS Latitude                    : 39 deg 3' 8.04" N
GPS Longitude                   : 78 deg 22' 13.80" W
Circle Of Confusion             : 0.005 mm
Field Of View                   : 69.4 deg
Focal Length                    : 4.2 mm (35 mm equivalent: 26.0 mm)
GPS Position                    : 39 deg 3' 8.04" N, 78 deg 22' 13.80" W
Hyperfocal Distance             : 2.14 m
Light Value                     : 10.5

Of course, I’m now very curious about that “Circle Of Confusion” field! More information on the exiftool output is available in an earlier column, What do your photos know about you?

Viewing images in ASCII

Another way to look at images on the command line is to use a tool that displays image content in character format. In others words, the image is displayed as characters and, for some tools, both characters and colors to replicate the image. How well this works will depend on the image itself and the tool you use. One such tool is called img2txt. Had I included the colors in the image below, you would likely be able to recognize the penguin that is waving at you.

$ img2txt penguin.png
                                                 @
                                               S8X
                                              S888
       8888@88XX8                           %@888@
    ;88%88@@X888tS8                        S8888@X
   @SS%tS:8 8S8t8@:;                     ;@8888@8
  t8;.;%SXX@888@@88                    8S8888888@
  88:.;%888@88X: @@ %                 X88888888@X
 %X8;;88S8%X88XS:.S  8              88@8888888@X
 X8S%t8 ;:t8.St% 8%.;@             88@8888888@@
 St888X8888@ 8888;888X8          X8X888888888X
;t:X8888888888:888888S@8        X@X88@88@8@@
  .S8888888@8@8@888tX% 888     8@88888888@
   %@X888@%88SX8X8%@:S88@888  8888888@88
 ;tSS@8 88.8S888:8@%XS%:88888@@8888@8
 SSS%t%S%X8 @888SX@X8%tt88@X8@@8% .%
 8XXS%t%tX8:  8XX@88:@;;;tS8%S;%S; :% 8
XX ;%%t%%SXS@8; XX  ;X;;;tt%%%%XS .:.%X.S         8
SX8@S%8X%8XS%S%S8X8t;;;8%%%%%%tt;8%.:;;.%@.      888t
 X8SX@@%XX%%@Xt%t;;;;8%%%%%%%%tt;;%8 ::.:.:%      8888

Wrap-up

While viewing image files on the command line is not likely to generate deep feelings of nostalgia, you can extract a lot of interesting image details that tell you about the types of images, when and where photos were taken, and what kind of device was used to take the photos.

Copyright © 2023 IDG Communications, Inc.



Source link