Search code examples
rimagejpegdocxofficer

body_add_img with custom image size


I want to add images to a serie of docx.files. But I do not want to change their size - each image has different width and height. How can I specify this for every imagine in:

 sample_doc %>% body_add_img(paste0(data_base$Skizze[[x]],".JPG"),width = 5.5, height = 5.5)

I would need to get for every image separately height and width to overwrite it. How could I do this?

I know the number of pixels

But I don't know how Microsoft Word computes the cm width and height for the default insertion - I want that for every document.


Solution

  • You could read each file with readJPEG from jpeg and use dim:

    library(jpeg)
    img <- readJPEG("pic.jpg")
    
    dim(img)
    

    The first two elements are the pixels, the third is always 3 (R, B, G). Now you divide the pixels by 96 and you have the width and height in inch and you can loop through your list of pictures with these values.