Search code examples
rresizejpegimage-resizing

how to change the dimensions of a jpg image in R?


I always resized images in CorelDraw from 1219,20 x 914,40mm to 78.36x51.00 to make photo boards. But now it turns out I have a lot of images in different folders and I needed to create an auto-lop to do this for me. If I were to do this in Corel like I used to do, it would take a lot of time. I have used the the resize function from Magick package, but didn't have obtain sucess.

resize(Image,  "78.36x51.00!")

Error in resize("78,36x51,00!") : 
Not compatible with requested type: [type=character; target=integer].

So I also tried the image_scale function, in this case the dimensions changed, but the size and resolution of the image was much smaller than expected.

 image_scale(Image, "78.36x51.00!")

  
  • Demonstration with the generated image after the resize (photo) and the expected size (white square)

enter image description here

enter image description here


Solution

  • Documentation indicates that magick's scaling functions are based on pixels. Scaling based on X and Y dimensions expressed as pixels is shown below.

    I'm not sure if this directly addresses the issue because the units in the question seem to change and dots per inch (dpi) is not defined. 1219,20 x 914,40 is in mm, but the units for 78.36 x 51.00 are undefined. The first pair of numbers has an aspect ratio of 1.3 while the second is 1.5.

    Scaling by X or Y in pixels will retain the original aspect ratio. Getting the right size involves knowing the desired dpi.

    install.packages("magic")
    library(magick)
    
    Image <- image_read("https://i.sstatic.net/42fvN.png")
    print(Image)
    

    enter image description here

    # Increase 300 in X dimension
    Image_300x <- image_scale(Image, "300")
    print(Image_300x)
    

    enter image description here

    # Increase 300 in y
    Image_300y <- image_scale(Image, "x300")
    print(Image_300y)
    

    enter image description here