magick icon indicating copy to clipboard operation
magick copied to clipboard

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

Open naikymen opened this issue 4 years ago • 3 comments

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:

  • ImageMagick 7.1.0-19 Q16-HDRI x86_64 2021-12-22 https://imagemagick.org
  • magick 2.7.3
  • R 4.1.2

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

naikymen avatar Jan 19 '22 15:01 naikymen

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

dcaud avatar Mar 11 '22 03:03 dcaud

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!

naikymen avatar Mar 18 '22 19:03 naikymen

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

dcaud avatar Mar 18 '22 19:03 dcaud