ImageMagick is splitting the NASA's [.IMG] fil

2020-08-01 06:51发布

问题:

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):


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


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

回答1:

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


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