1

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: enter image description here

I also saved a 320x240 version of it:

enter image description here

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?

hpy
  • 1,989
  • 7
  • 26
  • 56
  • 2
    Probably be a bit more precise what you want. It's unclear if this is about something like DPI-stuff effecting in presentation-size or if you just want to downsample, followed by upsample. – sascha Nov 08 '17 at 14:03
  • Thank you @sascha, I meant the latter (i.e. downsample then upsample, *not* related to DPI-stuff). Will update the post. – hpy Nov 08 '17 at 14:08
  • 1
    I think you want to interpolate your downsampled image using nearest neighbor interpolation. I don't know enough about R to recommend a package. – Cris Luengo Nov 08 '17 at 14:33
  • 1
    And now you should explain what more do you need? If downsample + upsample is what you want? Why not do it? That's surely trivial with any image-lib. What's unclear now? If you wonder about the kind of interpolation, you should make clear what you really want to achieve. Also remark, that your example might be a very simple one (where sampling could be something like downsample: merge 4 pixels into 1; upsample: copy 1 pixel 4 times) – sascha Nov 08 '17 at 16:20
  • Thank you @sascha. I don't know how to use R to achieve that downsampling then upsampling, i.e. which functions or packages are available for this task? I'll update the post again. – hpy Nov 08 '17 at 17:27

1 Answers1

2

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.stack.imgur.com/xgLSp.jpg")

# downsample
img = resize(img, w=320, filter="none")

# upsample
img = resize(img, w=640, filter="none")

display(img)

enter image description here

aoles
  • 1,525
  • 10
  • 17