[R] determine size (width and height) of a graphics file via R - how?

Mark Heckmann mark.heckmann at gmx.de
Wed Jun 20 10:02:27 CEST 2012


Thanks Ethan, this is helpful!

Just for documentation purposes: 
Daniel Adler (creator of the rdyncall package) send me another useful approach and code snippet.
The rdyncall package allows to connect to libraries not included in R (DLLs). 
To get the image size one may install the SDL library (http://www.libsdl.org/) to a place found by the Path (on Windows).
To connect to the SDL library and retrieve the image information is straightforward then:

require(rdyncall)
dynport(SDL)
#SDL_Init(SDL_INIT_VIDEO)
dynport(SDL_image)
#IMG_Init(IMG_INIT_JPG + IMG_INIT_PNG + IMG_INIT_TIF)

getsize <- function(file) {
  	s <- IMG_Load(file)
 	o <- list(width=s$w,height=s$h,file=file)
 	SDL_FreeSurface(s)
 	s <- NULL
 	return(o)
}

x <- getsize("/tmp/logo")
print(x)

--Mark


Am 14.05.2012 um 23:21 schrieb Ethan Brown:

> Hi Mark,
> 
> You can do this easily with the "identify" command in ImageMagick
> <http://www.imagemagick.org>. Install it, and then from within an R
> session:
> 
> system2("identify", "yourimagename.jpg")
> 
> ...and it should give you something like this:
> 
> yourimagename.jpg JPEG 800x533 800x533+0+0 8-bit DirectClass 378KB
> 0.000u 0:00.019
> 
> ...which is overkill but does include the dimensions.
> 
> If you're on Windows you need an extra argument:
> 
> system2("identify", "yourimagename.jpg", invisible = FALSE)
> 
> to make sure it actually shows you the result.
> 
> EBImage is an R interface to imagemagick but is probably more trouble
> than it's worth for the simple task you're trying to do.
> 
> Hope this helps,
> Ethan
> 
> On Sun, May 13, 2012 at 6:57 AM, Mark Heckmann <mark.heckmann at gmx.de> wrote:
>> Hi,
>> 
>> is there a way to determine the size (width, height) of a graphics file saved on my hard disk, e.g. a .bmp, via R.
>> What I want is basically the same information on the dimensions of the graphic file that I get from my file browser.
>> 
>> Thanks
>> Mark
>> 
>> PS. Why: I use the R2PPT and I need to determine the size of the original graphic before adding it to a slide.
>> 
>> 末末末末末末末末末末末末末末末末末末
>> Mark Heckmann
>> Blog: www.markheckmann.de
>> R-Blog: http://ryouready.wordpress.com
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>>        [[alternative HTML version deleted]]
>> 
>> 
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>> 

––––––––––––––––––––––––––––––––––––
Mark Heckmann
Blog: www.markheckmann.de
R-Blog: http://ryouready.wordpress.com



More information about the R-help mailing list