Resizing image in R Resizing image in R r r

Resizing image in R


You can easily accomplish this with the help of the Bioconductor package EBImage, an image processing and analysis toolbox for R. To install the package use:

source("http://bioconductor.org/biocLite.R")biocLite("EBImage")

You can then use the functionality provided by EBImage to load and scale the image, as in the following example.

library("EBImage")x <- readImage(system.file("images", "sample-color.png", package="EBImage"))# width and height of the original imagedim(x)[1:2]# scale to a specific width and heighty <- resize(x, w = 200, h = 100)# scale by 50%; the height is determined automatically so that# the aspect ratio is preservedy <- resize(x, dim(x)[1]/2)# show the scaled imagedisplay(y)# extract the pixel arrayz <- imageData(y)# orz <- as.array(y)

For more examples on the functionality provided by EBImage see the the package vignette .


The package imager is a nice fit and hides all the details about splines, interpolations and simply stores the images in a 4 dimensional array (the fourth dimension being used in the case of videos)

library(imager)im <- load.image(my_file)thmb <- resize(im,round(width(im)/10),round(height(im)/10))plot(im)plot(thmb,main="Thumbnail")

More informations can be found here: on the official introduction.


Do these options cover what you need:

library(jpeg)img <- readJPEG(system.file("img", "Rlogo.jpg", package="jpeg"))# Set image size in pixelsfor (i in 3:6) {  jpeg(paste0("Pixels",i,".jpeg"), width=200*i, height=200*i)  plot(as.raster(img))  dev.off()}# Set image size in inches (also need to set resolution in this case)for (i in 3:6) {  jpeg(paste0("Inches",i,".jpeg"), width=i, height=i, unit="in", res=600)  plot(as.raster(img))  dev.off()}

You can also save in other formats; png, bmp, tiff, pdf. ?jpeg will display help for saving in bitmap formats. ?pdf for help on saving as pdf.