draw_image在图和输出中扭曲了png 我目前正在尝试使用“ draw_image”函数显示一些PNG。更确切地说,这些是某些表情符号。 一旦我通过draw_image加载png,不幸的是它们是

问题描述 投票:0回答:1

但是,表情符号应该更像是这样:

enter image description here

enter image description here 不幸的是,我目前没有通过经过测试的解决方案实现目标。例如,我已经尝试通过降低高度或增加宽度来手动调整图片大小。不幸的是没有成功 enter image description here如果你们中的任何人在我的代码中找到一个错误(帖子结尾)或其他解决方案,我将非常感激! 谢谢你,祝你周日愉快!enter image description here

代码:

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)

您可能会在此语句中扭曲图像:

image_resize("1748x2480!")

ImageMagick示例中,请参见
Ignore纵横比('!'标志)
r ggplot2 png cowplot magick
1个回答
0
投票

当您保存图像时,您可能会再次扭曲它们:

ggsave(path = "xxx", filename = paste("randomname.png", sep = ""), dx,width = 210, height = 297, units = "mm", dpi = 100) 注意,图像高度(以像素为单位)为(dpi = 100

)x(

height = 297

) /(每英寸25.4毫米)= 1169。

相似,图像宽度(以像素为单位)为(

dpi = 100
)x(

width = 210

) /(每英寸25.4毫米)=827.

这将产生1169/827或1.4的长宽比,即高又瘦。
因此,您可以考虑调整图像的大小,或者如果必须调整图像大小,则使用一个百分比:

image_resize("150%")


然后,还要确保高度,宽度,单位和DPI将导致与原始表情符号相同的长宽比。您可以只指定高度
ggsave

宽度,而不是确定输入纵横比,然后计算高度,宽度,单位和DPI,而是可以照顾其他维度。

© www.soinside.com 2019 - 2025. All rights reserved.