e61-Institute / theme61

Create graphs in the e61 Institute style
https://e61-institute.github.io/theme61/
MIT License
5 stars 0 forks source link

Possible way to perfect the automatic height adjustment #66

Closed aaronw22 closed 10 months ago

aaronw22 commented 1 year ago

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.")
}
aaronw22 commented 1 year ago

Another idea is to temporarily set the panel background to a non-white colour and run the same pixel detecting algorithm.