Theoretically, we could perfectly automate the height adjustment every time by iteratively generating the image in a bitmap format, checking if the first row of pixels contains any non-transparent pixels, and then shrinking the height slightly until a pixel appears (i.e. the top of any title text).
This might be quite slow for large, complicated figures if a lot of iteration is required. We also need a way to detect if the image is starting out too narrow, perhaps we can check for the presence of non-transparent pixels on the left and right sides.
Here's some starting code ChatGPT came up with.
library(magick)
# Read the image
image <- image_read("path/to/image.png")
# Extract the pixel matrix
pixels <- image_data(image)
# Check if any pixels in the first row are not transparent
first_row <- pixels[1, , ]
has_nontransparent <- any(first_row[, 4] != 0)
if (has_nontransparent) {
print("The first row contains non-transparent pixels.")
} else {
print("The first row is fully transparent.")
}
Theoretically, we could perfectly automate the height adjustment every time by iteratively generating the image in a bitmap format, checking if the first row of pixels contains any non-transparent pixels, and then shrinking the height slightly until a pixel appears (i.e. the top of any title text).
This might be quite slow for large, complicated figures if a lot of iteration is required. We also need a way to detect if the image is starting out too narrow, perhaps we can check for the presence of non-transparent pixels on the left and right sides.
Here's some starting code ChatGPT came up with.