ropensci / magick

Magic, madness, heaven, sin
https://docs.ropensci.org/magick
Other
456 stars 63 forks source link

image_annotate - The background box is offset from the annotation's text #341

Open naikymen opened 2 years ago

naikymen commented 2 years ago

Description

Hi! I am trying to annotate some images with a string that has newline characters, and boxcolor = "white".

But something seems off: while the annotation renders, the box seems to be offset from the text (see pic below).

On the other hand, single line text renders fine.

Is there a way to prevent this? Thank you for all your work so far :)

I am using:

Code

Multiline

mystring <- "# /tmp/rcell.test.dir/parameters_53216ffa8960.txt\nmax_split_over_minor 0.8\nmax_dist_over_waist 4\nmax_pixels_per_cell 3000\nmin_pixels_per_cell 50\nbackground_reject_factor 0.4\ntracking_comparison 0.2\nimage_type brightfield\nbf_fl_mapping list\n"

cat(mystring)

image_blank(width = 500, height = 500, color = "black") %>% 
  image_annotate(mystring, boxcolor = "white", color = "black", size = 10)

Output:

# /tmp/rcell.test.dir/parameters_53216ffa8960.txt
max_split_over_minor 0.8
max_dist_over_waist 4
max_pixels_per_cell 3000
min_pixels_per_cell 50
background_reject_factor 0.4
tracking_comparison 0.2
image_type brightfield
bf_fl_mapping list

image

Single line

mystring <- "# /tmp/rcell.test.dir/parameters_53216ffa8960.txt"

cat(mystring)

image_blank(width = 500, height = 500, color = "black") %>% 
  image_annotate(mystring, boxcolor = "white", color = "black", size = 10)

Output:

# /tmp/rcell.test.dir/parameters_53216ffa8960.txt

image

dcaud commented 2 years ago

Just in case you want to know one way NOT to solve this problem:

mystring <- "# /tmp/rcell.test.dir/parameters_53216ffa8960.txt\nmax_split_over_minor 0.8\nmax_dist_over_waist 4\nmax_pixels_per_cell 3000\nmin_pixels_per_cell 50\nbackground_reject_factor 0.4\ntracking_comparison 0.2\nimage_type brightfield\nbf_fl_mapping list\n"

st <- strsplit(mystring, "\\n")[[1]]
mx <- max(nchar(st))
library(stringr)

my.string.padded <- str_pad(st, width = mx, side = "right")
my.string.padded <- paste(my.string.padded, collapse = "\n")

image_blank(width = 500, height = 500, color = "black") %>% 
  image_annotate(my.string.padded, boxcolor = "white", color = "black", size = 10)

image

naikymen commented 2 years ago

Thanks a lot dcaud, I'll stick to your idea for now.

Using a monospace font (Hack) makes it pretty too.

image

I'm happy but I'll let someone else close this when it's fixed.

Best!

dcaud commented 2 years ago

@naikymen Nice hack on top of my hack. I too look forward to a better fix.