Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tiff() resolution and compression are not reliably set in OSX

Tags:

r

image

tiff

The resolution and compression options in tiff() seem to be ignored on my system (see information below). This appears to be the same problem discussed in this SO question; I'm posting the question here so that I can be more specific about my system.

For instance the following code,

x <- rnorm(1000)
tiff("example.tiff", height=3, width=5, units="in", res=200, compression="lzw")
hist(x)
dev.off()

yields a .tiff file that superficially looks fine, but when I use Photoshop to look at the image size, I see this

enter image description here

This is consistent with tiff() creating a file with the right number of pixels, but the wrong size in inches. (This discrepancy gets me into trouble when I want to control both the font size and the image size of an image, for instance for publications that require specific physical image sizes).

Note that the file is also 2.5 MB, which seems too big for a 3"-by-5" black-and-white lzw-compressed tiff. I suspect that the compression option is also being ignored.

I get the same result with ggsave (which might rely on tiff() under the hood?) and with png(), so this seems to be a general problem with the way R and OSX deal with image files.

I'm using R Studio 0.97.449, with R 3.0.0 on OSX 10.8 as detailed below.

> sessionInfo()
R version 3.0.0 (2013-04-03)
Platform: x86_64-apple-darwin10.8.0 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] tools_3.0.0
like image 870
Drew Steen Avatar asked Jun 20 '13 15:06

Drew Steen


1 Answers

I'm on the same version of R and OS X that you are. When I run your example and then call up the image details using ImageMagick, I see the following (redacted) in my console:

$ identify -verbose /Users/schaunw/Desktop/example.tiff
Image: /Users/schaunw/Desktop/example.tiff
Format: TIFF (Tagged Image File Format)
Class: DirectClass
Geometry: 1000x600+0+0
Units: PixelsPerInch
Type: GrayscaleAlpha
Endianess: MSB
Colorspace: Gray
Depth: 8-bit
Page geometry: 1000x600+0+0
Dispose: Undefined
Iterations: 0
Compression: None
Orientation: TopLeft
Filesize: 2.402MB
Number pixels: 600K

The file size, as you point out, if quite large for an image supposedly that small, and it's showing a lack of compression. The answer to that problem revealed itself when I tried to change the type argument in the tiff function: OS X told me it no longer supports X11, and that I would need to install it manually if I wanted to use it (see http://www.macrumors.com/2012/02/17/apple-removes-x11-in-os-x-mountain-lion-shifts-support-to-open-source-xquartz/).

So I installed it, and ran this your example code again, but this time with type set to "cairo". This is what I got from ImageMagick afterwards:

$ identify -verbose /Users/schaunw/Desktop/example.tiff
Image: /Users/schaunw/Desktop/example.tiff
Format: TIFF (Tagged Image File Format)
Class: DirectClass
Geometry: 1000x600+0+0
Resolution: 200x200
Print size: 5x3
Units: PixelsPerInch
Type: Grayscale
Endianess: MSB
Colorspace: Gray
Depth: 8-bit
Page geometry: 1000x600+0+0
Dispose: Undefined
Iterations: 0
Compression: LZW
Orientation: TopLeft
Filesize: 19.8KB
Number pixels: 600K

Notice the appropriately small file size and the LZW compression, but also notice that this time ImageMagick explicitly states that the print size is 5x3. It didn't do that before.

So it looks like your problem has to do with OS X no longer shipping with X11. Installing X11 should fix your problem.

like image 114
SchaunW Avatar answered Oct 13 '22 03:10

SchaunW