Reputation: 397
I'm experimenting with the generativeart package for R. Information on the package can be found here.
I'm looking to generate some images to use as a background image for a design exercise. However, I'm struggling to get the images exported into a resolution that is of high enough quality or set specific dimensions. Obviously there's some part of randomness, which is the purpose. However, since I'm planning to use this for headers on slides and other elements, I'd like to have at least some sort of control over the image quality / dimensions that are being generated.
I'm using the example code - to start with - an there's no notion of this.
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")
This section isn't documented and I'm not sure IF it is even possible. Would appreciate any feedback on how to make this work.
Kind regards,
Edit - After the feedback of the creator of the package I modified the code. However, I'm not getting the desired result so I assume I'm doing something wrong.
Code update below:
# 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")
Not seeing any major difference when it comes to resolution so I assume I'm doing something wrong.
Kind regards,
Upvotes: 1
Views: 278
Reputation: 1339
I am the author of the package.
Basically, you have 2 options:
You save the image as svg
by setting the filetype
parameter: generativeart::generate_img(formula = my_formula, nr_of_img = 5, polar = TRUE, filetype = "svg", color = "black", background_color = "white")
. You can then scale the svg
file as you wish.
When you want to use png
you have to modify the generate_plot()
function I set the width and height to 6 (inch), see: https://github.com/cutterkom/generativeart/blob/master/R/generate_plot.R (line 43)
What you should do when choosing option 2:
generate_plot()
functiongenerate_plot()
function to use your own instead of the one from the package.Upvotes: 3