26

I am searching for a simple way to plot a photographic JPEG image on a graphics device in R.

For example the following using the raster package appears to ignore the colour attributes in the image. I want to reproduce the photograph in its original colours:

library(raster)
library(rgdal)

myJPG <- raster("colourfulPic.jpg")
plot(myJPG)  ## Recolours JPEG;

I have discovered that the package rimage has recently been archived and appears to no longer be recommended for use (see here), if it, indeed, ever did what I need.

Similarly the EBImage for BioConductor, which may also possibly work, is not built for 64 bit Windows, and unfortunately I need this architecture.

Please tell me I missing something very obvious in base graphics?


digitalmaps
  • 2,885
  • 3
  • 22
  • 28

6 Answers6

33

Here goes an updated solution, that relies only on the jpeg package and handles color and greyscale images (packages used in other solutions are outdated and won't install with a recent R version).

The solution consists in this plot function:

plot_jpeg = function(path, add=FALSE)
{
  require('jpeg')
  jpg = readJPEG(path, native=T) # read the file
  res = dim(jpg)[2:1] # get the resolution, [x, y]
  if (!add) # initialize an empty plot area if add==FALSE
    plot(1,1,xlim=c(1,res[1]),ylim=c(1,res[2]),asp=1,type='n',xaxs='i',yaxs='i',xaxt='n',yaxt='n',xlab='',ylab='',bty='n')
  rasterImage(jpg,1,1,res[1],res[2])
}

This function can then be called with the path of the picture as argument, for example:

plot_jpeg('~/nyancat.jpg')

To add the picture to an existing plot, use the switch add=TRUE - and be wary of axis limits!

gregmacfarlane
  • 2,121
  • 3
  • 24
  • 53
Jealie
  • 6,157
  • 2
  • 33
  • 36
  • 4
    I had to invert the resolution in order to get a correct plot. Change res = dim(jpg)[1:2] to res = dim(jpg)[2:1] OR replace all res[1] with res[2] and vice versa to fix the error – Samuel Apr 22 '15 at 08:17
10

Using the imager package:

library(imager)

image <- load.image(image_filename)
plot(image)

This works for a number of image formats including jpeg, gif, etc...

James Hirschorn
  • 7,032
  • 5
  • 45
  • 53
6

Instead of rimage, perhaps use the package ReadImages:

library('ReadImages')
myjpg <- read.jpeg('E:/bigfoot.jpg')
plot(myjpg)
MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
Apprentice Queue
  • 2,036
  • 13
  • 13
  • I knew it would be simple! Sometimes finding the right package is all it takes. Thanks. – digitalmaps Mar 03 '12 at 04:25
  • 7
    FWIW If you care about performance or memory usage, using `readJPEG(..., native=TRUE)` from `jpeg` to read and then `rasterImage` from `graphics` to plot is much more efficient for large images as it avoids all the unnecessary conversions into arrays that `ReadImages` does. – Simon Urbanek Mar 03 '12 at 17:40
  • 8
    Unfortunately, the package `ReadImages` is orphaned and no longer available on CRAN. It has not been updated to work with R versions 2.14.0 and newer. – Stibu Feb 25 '16 at 20:27
4

If you are building an RMarkdown document, knitr::include_graphics() is direct and simple.

gregmacfarlane
  • 2,121
  • 3
  • 24
  • 53
0

raster Has a built-in function for this called plotRGB

library(raster)  
myJPG <- stack("colourfulPic.jpg")  
plotRGB(myJPG)  
user3357177
  • 355
  • 2
  • 9
  • 1
    While this code snippet may be the solution, [including an explanation](https://meta.stackexchange.com/questions/114762/explaining-entirely-%E2%80%8C%E2%80%8Bcode-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Narendra Jadhav Jul 10 '18 at 02:48
-2

Please note that ReadImages is now deprecated. Instead you could install the biOps package, which also contains a rich set of image manipulation methods. Use as follows:

library(jpeg)
library(biOps)

image <- readJPEG("test.jpg")

# image is of type imagedata (the red,green,blue channel images concatenated)

plot(image)

Edit: An object of type imagedata is accessed as image[Y,X,Channel], btw

pbnelson
  • 1,649
  • 16
  • 14
user562529
  • 931
  • 1
  • 11
  • 19
  • 5
    NOTE: BiOps is now deprecated also. Is there any easy way to plot a JPEG image? There seems to be a lot of data with the package 'jpeg' and takes much CPU usage to plot. – logicForPresident Sep 12 '14 at 16:56
  • @ratioVenandi: I got the same problem today, and I just proposed another solution that does not rely on `biOps` or `ReadImages` – Jealie Feb 25 '15 at 21:09