r - 以方形马赛克打印一组图像

标签 r image plot

假设我们有一组相同的图像imgs(见下文)。请注意,设置的长度在实践中可能会有所不同。

library(magick)
library(rsvg)

img <- image_read_svg("https://image.flaticon.com/icons/svg/132/132233.svg", width = 30)
imgs <- replicate(8, img)

enter image description here

目标是打印包含 imgs 中所有图像的方形图像(即使设置的长度可能不是平方数):

enter image description here

我尝试了 magick 包中的 image_append()image_append(..., stack = TRUE) ,但没有成功 [ref] 。理想情况下,我想要一个函数(例如 printMosaic(imgs)),它以 imgs 作为输入并输出上面显示的方形图像。也许使用不同的包会更容易实现?

最佳答案

这是一个非常好的问题!

首先,让我们随机选择我们想要的图像数量,然后自动计算我们需要多少行/列。

# Number of images from 1 to 100
N <- sample(1:1e2, 1)
print(N)
[1] 84
# How many rows/columns we will need
X <- ceiling(sqrt(N))
print(X)
[1] 10

使用具有 X 行和列的 multipanelfigure 包创建空面板:

library(multipanelfigure)
figure <- multi_panel_figure(columns = X, rows = X)

# Iterate from 1 to N images and append them to figure
for(i in seq_len(N)) {
    # "./R.png" is path to image I'm using
    # With this package you don't need to worry about importing images
    figure %<>% fill_panel("./R.png", label = "", scaling = "shrink")
}

enter image description here

关于r - 以方形马赛克打印一组图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52294096/

相关文章:

arrays - matlab中如何连接两个不同维度的图像

java - 回收器 View 中的空对象引用

Python优化算法调整线越过尽可能多的点

python - 如何在 matplotlib.pyplot 中设置 X 和 Y 轴标题

python - 如何在 python 中创建 3D 高度图

r - 是否可以在 R 中默认在新窗口中绘图?

从文件中读取矩阵的索引和值并分配值

r - 多个时间点分组受试者的百分比变化 R

r - 带有ggplot2::stat_qq的Q-Q图,颜色,单个组

c# - 如何在 Windows Phone 中通过保持图像的纵横比来调整图像大小