Image Classification :Reading and Loading Images in R



I’m getting below error while reading the images-

Error in readJPEG(x, …) : JPEG decompression error: Not a JPEG file: starts with 0x89 0x50

As I have all the images in JPEG format but when I used the image_read() function from the magick package in R, I found that some of the images having PNG format.

So, How to find all those images where there is a change in extension? Also, how to convert these type of images in pixel format?


If you are doing this on Linux/Mac, there is a file command that you can use to identify the actual file type


$ ll
total 27400
-rw-r--r--@ 1 himanshujoshi  staff   1.5M 23 Nov 19:13 this_is_a_jpg_image.jpg
-rw-r--r--@ 1 himanshujoshi  staff   6.0M 23 Nov 19:13 this_is_a_png_image.png
-rw-r--r--@ 1 himanshujoshi  staff   6.0M 23 Nov 19:14 this_is_a_png_image_with_incorrect_extension.jpg

$ file *
this_is_a_jpg_image.jpg:                          JPEG image data, Exif standard: [TIFF image data, big-endian, direntries=12, height=1080, bps=0, PhotometricIntepretation=RGB, orientation=upper-left, width=1920], baseline, precision 8, 1920x1080, frames 3
this_is_a_png_image.png:                          PNG image data, 1920 x 1080, 8-bit/color RGB, non-interlaced
this_is_a_png_image_with_incorrect_extension.jpg: PNG image data, 1920 x 1080, 8-bit/color RGB, non-

If you’re using Windows, you can probably use TrID. It is similar to Unix’s file command and can identify the file type based on its binary signature.