Read a bitmap image stored in the JPEG format

Share:

Description

Reads an image from a JPEG file/content into a raster array.

Usage

1
readJPEG(source, native = FALSE)

Arguments

source

Either name of the file to read from or a raw vector representing the JPEG file content.

native

determines the image representation - if FALSE (the default) then the result is an array, if TRUE then the result is a native raster representation.

Value

If native is FALSE then an array of the dimensions height x width x channels. If there is only one channel the result is a matrix. The values are reals between 0 and 1. If native is TRUE then an object of the class nativeRaster is returned instead. The latter cannot be easily computed on but is the most efficient way to draw using rasterImage.

Most common files decompress into RGB (3 channels) or Grayscale (1 channel). Note that Grayscale images cannot be directly used in rasterImage unless native is set to TRUE because rasterImage requires RGB or RGBA format (nativeRaster is always 8-bit RGBA).

JPEG doesn't support alpha channel, you may want to use PNG instead in such situations.

Note

CMYK JPEG images saved by Adobe Photoshop may have inverted ink values due to a bug in Photoshop. Unfortunately this includes some sample CMYK images that are floating around, so beware of the source when converting the result to other color spaces. readJPEG will preserve values exactly as they are encoded in the file.

See Also

rasterImage, writeJPEG

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# read a sample file (R logo)
img <- readJPEG(system.file("img", "Rlogo.jpg", package="jpeg"))

# read it also in native format
img.n <- readJPEG(system.file("img", "Rlogo.jpg", package="jpeg"), TRUE)

# if your R supports it, we'll plot it
if (exists("rasterImage")) { # can plot only in R 2.11.0 and higher
  plot(1:2, type='n')

  rasterImage(img, 1.2, 1.27, 1.8, 1.73)
  rasterImage(img.n, 1.5, 1.5, 1.9, 1.8)
}

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.