1

我正在试验 R 的 generationart 包。关于包的信息可以在这里找到。

我正在寻找一些图像以用作设计练习的背景图像。但是,我正在努力将图像导出为质量足够高或设置特定尺寸的分辨率。显然,有一些随机性,这就是目的。但是,由于我打算将其用于幻灯片和其他元素的标题,因此我希望至少对正在生成的图像质量/尺寸进行某种控制。

我正在使用示例代码 - 首先 - 没有这个概念。

  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")

在分辨率方面没有看到任何重大差异,所以我认为我做错了什么。

亲切的问候,

4

1 回答 1

3

我是包的作者。

基本上,您有两个选择:

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

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

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

  • 复制generate_plot()函数的代码
  • 更改宽度和高度的数字
  • 重新加载generate_plot()函数以使用您自己的函数,而不是包中的函数。
于 2021-05-05T20:05:24.280 回答