-1

I have some raw .IMG format files which I'm converting to .jpg using ImageMagick to apply a CNN Classifier. The converted images, however have a black vertical line splitting the image into two. The part on the left side of the line should have actually been on the right side of the right part of the image. I've posted a sample image:

I used the command magick convert input_filename.IMG output_filename.jpg

Raw .IMG File


Here is how the image is supposed to look (converted manually using numpy):

Should Look Like This


How the image is actually looking (with the vertical black line using ImageMagick):

Actually Looking Like This


Version Details:

harshitjindal@Harshits-MacBook-Pro ~ % magick identify -version
Version: ImageMagick 7.0.10-0 Q16 x86_64 2020-03-08 
https://imagemagick.org Copyright: © 1999-2020 ImageMagick Studio LLC 
License: https://imagemagick.org/script/license.php Features: Cipher DPC     
HDRI Modules OpenMP(3.1)  Delegates (built-in): bzlib freetype heic jng     
jp2 jpeg lcms ltdl lzma openexr png tiff webp xml zlib
Harshit Jindal
  • 621
  • 8
  • 26
  • 1
    What was your ImageMagick command? If the file is actually a raw image, then you typically need to specify the width, height and depth when reading the file. I tried to read it as DNG and as TIF, but both failed as does a simple conversion. – fmw42 Mar 09 '20 at 06:14
  • @fmw42 I used the command ```magick convert input_filename.IMG output_filename.jpg``` – Harshit Jindal Mar 09 '20 at 07:30
  • For ImageMagick 7, use magick, not magick convert. Nevertheless, that does not fix your issue. You should report this on the ImageMagick Discourse server in the Bugs forum at https://imagemagick.org/discourse-server/. What tool creates this type image? I do not see it listing in the supported formats via `magick -list format` – fmw42 Mar 09 '20 at 17:46
  • @fmw42 The issue is now resolved with Mark Setchell’s help – Harshit Jindal Mar 09 '20 at 17:57
  • What tool created the image? – fmw42 Mar 09 '20 at 18:01
  • @fmw42 All the .IMG files were created by NASA’s MESSENGER spacecraft – Harshit Jindal Mar 09 '20 at 18:03
  • https://en.wikipedia.org/wiki/IMG_(file_format) It lists the tools at the end of the page. Also, I'll report this bug to the discourse server this weekend. – Harshit Jindal Mar 09 '20 at 18:12
  • 1
    OK. Mark is correct. There is a document at https://www.planetary.org/blogs/emily-lakdawalla/2018/0307-image-processing-trick-raw-photoshop.html that explains the form. The .IMG file is basically a .RAW file with some header data that just needs to be removed. – fmw42 Mar 09 '20 at 18:13
  • @fmw42 Yes, I found it too just now. I included the keyword "NASA" with ".IMG File" to get the proper results. There is even an application called NASA View that opens these images conveniently, however I could not find any batch processing option. – Harshit Jindal Mar 09 '20 at 18:23

1 Answers1

2

I don't know why ImageMagick is failing to interpret the file correctly, but I can show you how to make it work.

You need to search in your file for the height, width and data type of your image, you can do that like this:

grep -E "LINES|LINE_SAMPLES|BITS" EW0220149939G.IMG 
LINES              = 1024
LINE_SAMPLES       = 1024
SAMPLE_BITS        = 8

That means your image is 1024x1024 and 8 bits/sample (1 byte). Then you need to take that number of bytes from the tail end of the file and feed them into ImageMagick. So, you need the final 1024x1024 bytes which you can get with tail or gtail (GNU tail) as you are on a Mac.

gtail -c $((1024*1024*1)) EW0220149939G.IMG | convert -depth 8 -size 1024x1024 gray:- result.jpg

enter image description here


If your image is 16-bit, like in your other question, you need to use:

gtail -c $((1024*1024*2)) 16-BIT-IMAGE.IMG | convert -depth 16 -size 1024x1024 gray:- result.jpg

If you dislike using gtail to get the last megabyte, you can alternatively specify an offset from the start of the file that tells ImageMagick where the pixel data starts. So, first you need the size of the header:

grep -E "RECORD_BYTES|LABEL_RECORDS" EW*IMG
RECORD_BYTES         = 1024
LABEL_RECORDS         = 0007

That means we need to skip 1024*7 bytes to get to the image, so the command is:

convert -size 1024x1024+$((1024*7)) -depth 8 gray:EW0220149939G.IMG result.jpg
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Thank you Mark. ```convert -size 1024x1024+$((1024*7)) -depth 8 gray:EW0220149939G.IMG result.jpg``` worked for me. I now understand how to read the header size and skip it – Harshit Jindal Mar 09 '20 at 09:35
  • 1
    This issue will be resolved in the next version of ImageMagick. Thanks for pointing us in the right direction. – dlemstra Mar 09 '20 at 22:11