Kelly N.
Kelly N.

Reputation: 51

R Magick Package Image_Write Saves Tiff Images in Gray Instead of RGB

I have a very large number of images that need to be cropped slightly (by 1 pixel either height-wise or width-wise) in order to do further image-processing on them.

I'm trying to use the Magick package in R to do so, but running into an issue where any images that are gray are saved by Magick in grayscale instead of RGB. I see that people have asked similar questions here and here, and I have tried the solutions offered to no avail. For some reason, when doing Image_Write, setting the defines colorspace to auto-grayscale off and type to truecolor does not work and the images are still saved as grayscale instead of RGB format.

Here is the code I'm trying to use:

crop <- function(a,b) {
  image <- image_read(a)
  cut <- image_crop(image, b)
  image_write(cut, path = a, format = "tiff", defines = c('colorspace:auto-grayscale' = 'false', 'type:truecolor' = 'on'))
}

runcrop <- mapply(crop, mydat[,1], mydat[,2])

Where the input (mydat) is a table with two columns: the path to the image and the pixel size I need the image cropped to. Any image taken in black and white (CH4) is saved as a grayscale output, while all the other channels are correctly saved as RGB images after cropping.

Here is a small excerpt from the table:

> mydat
     imtocrop                            WxH            
[1,] "./01 10245 XY01_Fused_CH1.tif"     "2288x1218+0+0"
[2,] "./01 10245 XY01_Fused_CH2.tif"     "2288x1218+0+0"
[3,] "./01 10245 XY01_Fused_CH3.tif"     "2288x1218+0+0"
[4,] "./01 10245 XY01_Fused_CH4.tif"     "2288x1218+0+0"
[5,] "./01 10245 XY01_Fused_Overlay.tif" "2288x1218+0+0"

And here is an example image to recapitulate the error: https://www.dropbox.com/s/8m93vflnf6rd6ao/01%2010245%20XY01_Fused_CH4.tif?dl=0

The cropping formula I've written works perfectly, but I can't seem to get Image Magick to save these tiffs as RGB instead of grayscale. I've tried both 'false' and 'off' for auto-grayscale, I've tried 'true' and 'on' for truecolor. I've also tried using only the truecolor option, without the colorspace option (as suggested on ImageMagick's documentation site here). Nothing I do seems to convince Image_Write to output as RBG instead of grayscale. Please let me know if you have any ideas that might fix this error, thanks!

Upvotes: 0

Views: 471

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207748

I have no knowledge of R, but am trying to move my knowledge of ImageMagick towards your knowledge of R and maybe we an reach the target of sorting out your problem somewhere in the middle...

Let's make a simple 640x480 solid black image with ImageMagick in the Terminal, and pipe it into exiftool in TIFF format to check its depth and whether it is greyscale or colour:

magick -size 640x480 xc:black tif:- | exiftool - | grep -E '^Bits|^Photometric'

Bits Per Sample                 : 16
Photometric Interpretation      : BlackIsZero     <- means GREYSCALE

Ok, not surprisingly, our black 640x480 rectangle came out in greyscale with 16-bits per sample because my ImageMagick was compiled with Q16. Let's rectify the 16-bit-edness:

magick -size 640x480 xc:black -depth 8 tif:- | exiftool - | grep -E '^Bits|^Photometric'

Bits Per Sample                 : 8              <--- that's better, 8-bit
Photometric Interpretation      : BlackIsZero    <--- still greyscale

Ok, now let's force the black image to become RGB:

magick -size 640x480 xc:black -type truecolor -depth 8 tif:- | exiftool - | grep -E '^Bits|^Photometric'

Bits Per Sample                 : 8 8 8
Photometric Interpretation      : RGB

My hope is that you can find the corresponding settings/switches/options in R Studio now that you know what they look like.

Another option might be to "shell out" with system() and just use the command-line interface:

magick INPUT.TIF -crop WIDTHxHEIGHT+xoffset+yoffset -type truecolor RESULT.TIF

Note that I am forcing TIFF format and piping to exiftool for simplicity of demonstration. If you are testing in your Terminal, you can write straight to a TIFF file with:

magick -size 640x480 xc:black image.tif

You can also set lossless compression with:

magick -size 640x480 xc:black -compress LZW image.tif

Upvotes: 0

Related Questions