I know that there is the jpeg
package for R that can deal with JPEG images. In my workflow, there is a step where I would like to downsample images while retaining their original pixel resolution.
For example, the following is a 640x480 JPEG I quickly made with GIMP:
I also saved a 320x240 version of it:
As you can see the 320x240 version is smaller and appears smaller, too.
However, is there a way to programmatically use the jpeg
package or another R package to "downsample" the image so that it stays 640x480 (or whatever its original pixel dimensions are) but do it so that it looks like the 320x240 version "zoomed in" to a 640x480 size?? (and yes that means it'll probably look pixellated)
Thank you!
EDIT: To be clear, I am not specifically talking about DPI-stuff. I am talking about downsampling but upsampling back to the original resolution. Which functions and packages can I use to achieve this?
You could, for example, use the Bioconductor package EBImage to achieve the desired result. readImage
is a wrapper for the functions provided in R packages jpeg, png, and tiff which supports reading from URLs directly.
The filter
argument to resize
turns off bilinear filtering, otherwise the result would be smoothed out rather than pixelated. The transformed image can be saved to disk with writeImage
.
library(EBImage)
img = readImage("https://i.sstatic.net/xgLSp.jpg")
# downsample
img = resize(img, w=320, filter="none")
# upsample
img = resize(img, w=640, filter="none")
display(img)