0
votes
    library(raster)
    r1 <- brick(system.file("external/rlogo.grd", package="raster"))
    plotRGB(r1)
    png(filename = "boy.png",width=50, height=50)
    x <- crop(r1, extent(0,50,0,50))
    plotRGB(x)
    dev.off()

Above code creates boy.png in my default working directory. The image is 50 pixels by 50 pixels.

  1. How can I make that image - 100 pixels by 100 pixels while keeping aspect ratio and DPI the same?
  2. In my png(filename = "boy.png",width=50, height=50) command I have to specify image size. Otherwise i get image of size 480 pixels by 480 pixels where cropped part of the image - x <- crop(r1, extent(0,50,0,50)) is blown in size. If in my png command i mention width as 100 and in my crop command if the image width is <100 then i get two white bands around my actual image(it seems that raster package fills extra space with white color). Is there a way to get image of exact size as mentioned in crop command but without specifying image width and height in png command?
1

1 Answers

2
votes
library(raster)
r1 <- brick(system.file("external/rlogo.grd", package="raster"))
x <- crop(r1, extent(0,50,0,50))

png(filename="boy.png", width=ncol(x), height=nrow(x))
plotRGB(x)
dev.off()

DPI (dots per inch) is not an image property. It depends on the size of the paper you print it on (and the fraction of the paper that is covered by the image).