Christopher Graham
Christopher Graham

Reputation: 239

Resizing image in R

I am trying to work with some image data in R, and cannot figure out how to resize the images I have to ensure they are all the same size.

In Python, I approached this problem as follows:

from PIL import Image
import numpy as np

size = (100, 100)
img = Image.open(filename)
img = img.resize(size)
img = np.array(img.getdata())

In R, I've been unable to locate a library that would accomplish the same thing. The farthest I've been able to get is:

library(jpeg)

img <- readJPEG(filename)
# Need something here to resize
img <- as.matrix(img)

The easiest approach would be a library like Pillow that I could call on, but as I said, I can't seem to find anything.

Thanks,

Upvotes: 19

Views: 43742

Answers (6)

Benoit Lamarsaude
Benoit Lamarsaude

Reputation: 405

With package imager and a single parameter

library(imager)

im <- load.image(pict)

thmb <- im %>% imresize(0.1)

plot(im)
plot(thmb,main="Thumbnail")

Upvotes: 0

Simon Ji
Simon Ji

Reputation: 759

Inspired by Seily, to resize grey level image.

resize = function(img, new_width, new_height) {
  new_img = apply(img, 2, function(y){return (spline(y, n = new_height)$y)})
  new_img = t(apply(new_img, 1, function(y){return (spline(y, n = new_width)$y)}))

  new_img[new_img < 0] = 0
  new_img = round(new_img)

  return (new_img)
}

Upvotes: 2

RUser4512
RUser4512

Reputation: 1074

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.

Upvotes: 11

aoles
aoles

Reputation: 1545

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 image
dim(x)[1:2]

# scale to a specific width and height
y <- resize(x, w = 200, h = 100)

# scale by 50%; the height is determined automatically so that
# the aspect ratio is preserved
y <- resize(x, dim(x)[1]/2)

# show the scaled image
display(y)

# extract the pixel array
z <- imageData(y)

# or
z <- as.array(y)

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

Upvotes: 25

Seily
Seily

Reputation: 496

I use the following code to resample matrices. If you have a jpeg object, you might do that for each color channel individual.

The strategy is as follows:

Given a matrix m with the dimensions a and b and the new dimensions a.new and b.new

  1. Define your new grid
x.new <- seq(1,a,length.out=a.new)
y.new <- seq(1,a,length.out=b.new)
  1. resample the original matrix twice in x and in y direction
V <- apply(V,2,FUN=function(y,x,xout) return(spline(x,y,xout=xout)$y),x,x.new)
V <- t(apply(V,1,FUN=function(y,x,xout) return(spline(x,y,xout=xout)$y),d,y.new))

Here I choose the spline-interpolation but you can also use a linear one with apporx(). You will gain additionally an x- and y-axis for plotting with the image(x = x.new, y = y.new, z = V) function.

Best.

Upvotes: 5

eipi10
eipi10

Reputation: 93811

Do these options cover what you need:

library(jpeg)

img <- readJPEG(system.file("img", "Rlogo.jpg", package="jpeg"))

# Set image size in pixels
for (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.

Upvotes: 9

Related Questions