R生成艺术包图像分辨率



我正在试验R的generativeart包。有关该包的信息可以在这里找到。

我希望生成一些图像作为设计练习的背景图像。然而,我很难将图像导出到足够高质量的分辨率或设置特定的尺寸。很明显,这是随机性的一部分,这就是目的。然而,由于我计划将其用于幻灯片和其他元素的标题,我希望至少对生成的图像质量/尺寸有某种控制。

首先,我使用的是示例代码,但没有这方面的概念。

x = quote(runif(1, -1, 1) * x_i^2 - sin(y_i^2)),
y = quote(runif(1, -1, 1) * y_i^3 - cos(x_i^2))
)
generativeart::generate_img(formula = my_formula, nr_of_img = 5, polar = TRUE, color = "black", background_color = "white")

这一部分没有记录,我甚至不确定这是否可能。如有任何关于如何使之发挥作用的反馈,我们将不胜感激。

问候,

编辑-在收到包创建者的反馈后,我修改了代码。然而,我没有得到想要的结果,所以我认为我做错了什么。

代码更新如下:

# modify generate_plot function and store it in custom function name
generate_plot1 <- function(df, file_name, polar, filetype, color = "black", background_color = "white") {
print("generate plot")
if (polar == TRUE) {
plot <- df %>%
ggplot2::ggplot(ggplot2::aes(x = x, y = y)) +
ggplot2::geom_point(alpha = 0.1, size = 0, shape = 20, color = color) +
ggplot2::theme_void() +
ggplot2::coord_fixed() +
ggplot2::coord_polar() +
ggplot2::theme(
panel.background = element_rect(fill = background_color),
plot.background = element_rect(fill = background_color)
)
} else {
plot <- df %>%
ggplot2::ggplot(ggplot2::aes(x = x, y = y)) +
ggplot2::geom_point(alpha = 0.1, size = 0, shape = 20, color = color) +
ggplot2::theme_void() +
ggplot2::coord_fixed() +
ggplot2::theme(
panel.background = element_rect(fill = background_color),
plot.background = element_rect(fill = background_color)
)
}
ggplot2::ggsave(plot, filename = paste0(IMG_PATH, file_name), width = 100, height = 100, device = filetype)
print("image saved...")
}
# reload generate_img function and add generate_plot1 function
generate_img <- function(formula, nr_of_img, polar = FALSE, filetype = "png", ...) {
seeds <- generate_seeds(nr_of_img)
purrr::map(seeds, function(seed){
set.seed(seed)
file_name <- generate_filename(seed, filetype)
logfile <- check_logfile_existence()
logfile <- generate_logfile_entry(logfile, formula, seed, file_name)
df <- generate_data(formula)
plot <- generate_plot1(df, file_name, polar, filetype, ...)
})
}
my_formula <- list(
x = quote(runif(1, -1, 1) * x_i^2 + sin(y_i^2)),
y = quote(runif(1, -1, 1) * y_i^3 + cos(x_i^4))
)
# call the main function to create five images with a polar coordinate system
generativeart::generate_img(formula = my_formula, nr_of_img = 5, polar = FALSE, 
filetype = "png", color = "black", background_color = "aquamarine3")

在解决问题时没有看到任何重大差异,所以我认为我做错了什么。

问候,

我是这个包的作者。

基本上,你有两个选项:

  1. 通过设置filetype参数generativeart::generate_img(formula = my_formula, nr_of_img = 5, polar = TRUE, filetype = "svg", color = "black", background_color = "white")将图像另存为svg。然后,您可以根据需要缩放svg文件。

  2. 当您想要使用png时,您必须修改generate_plot()功能。我将宽度和高度设置为6(英寸(,请参阅:https://github.com/cutterkom/generativeart/blob/master/R/generate_plot.R(第43行(

选择选项2:时应该做什么

  • 复制generate_plot()函数的代码
  • 更改宽度和高度的数字
  • 重新加载generate_plot()函数以使用您自己的函数,而不是包中的函数

最新更新