Reputation: 171
I am currently trying to display some PNGs using the "draw_image" function. To be more precise, these are certain emojis.
As soon as I load the PNGs via draw_image, they are unfortunately distorted, so that the emojis look like this:
But of course the emojis should look more like this:
Unfortunately, I do not currently reach my goal with my tested solutions. For example, I have already tried to manually resize the pictures by reducing the height or increasing the width. Unfortunately without success.
If anyone of you finds a bug in my code (end of post) or alternatively has another solution in mind, I would be very grateful!
Thank you and have a nice Sunday!
Code:
bg <-
image_read(paste("randomimage.png", sep = ""))%>%
image_resize("1748x2480!")
dx <- ggdraw() +
draw_image(bg) +
draw_image(Emoji3, width = 1, height = 1, scale = 0.04)+
draw_image(Emoji2, width = 1, height = 1, scale = 0.04)+
draw_image(Emoji1, width = 1, height = 1, scale = 0.04)
grid.arrange(dx)
ggsave(path = "xxx", filename = paste("randomname.png", sep = ""), dx, width = 210, height = 297, units = "mm", dpi = 100)
Upvotes: 1
Views: 226
Reputation: 335
You might be distorting the images in this statement: image_resize("1748x2480!")
See the section Ignore Aspect Ratio ('!' flag) in ImageMagick Examples -- Resize or Scaling (General Techniques)
And when you save the images, you might be distorting them again:
ggsave(path = "xxx", filename = paste("randomname.png", sep = ""), dx,width = 210, height = 297, units = "mm", dpi = 100)
Note that the image height, in pixels, will be (dpi = 100
) x (height = 297
) / ( 25.4 mm per inch ) = 1169.
Similarly, the image width, in pixels, will be (dpi = 100
) x (width = 210
) / ( 25.4 mm per inch ) = 827.
This results in an aspect ratio of 1169/827 or 1.4, i.e., tall and skinny.
So you might consider not resizing the image, or, if you must resize it, use a percentage:
image_resize("150%")
And then also make sure that the height, width, units, and dpi will result in the same aspect ratio as the original emojis. Instead of determining the input aspect ratios and then calculating height, width, units, and dpi, you can just specify height or width, and ggsave
will take care of the other dimension.
Upvotes: 0