ropensci / magick

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

Inconsistency in text positioning with image_annotate() #329

Open Dobrokhotov1989 opened 3 years ago

Dobrokhotov1989 commented 3 years ago

Hi,

I'm trying to add annotation to my images and combine them in single composite image. I'm facing a problem with text positioning. When I use gravity = "north" the results is consistent with expectations (i.e. text centered horizontally and located at the top of the frame). However, when I'm trying to add annotation to the left using degree = -90, gravity = "west" the half of the text is cropped and it is not centered vertically. Do I misunderstand something?

This one is good: annotate_good

This one is no good: annotate_bad

Here is reprex:

  library(magick)

  ## This works perfectly
  image_read("./temp/Day_2_XY06_CH4.png") %>%
    image_border(color = "#FFFFFF", geometry = "0x25") %>%
    image_annotate(text = "EdU",
                   font = "serif",
                   weight = 700,
                   size = 20,
                   color = "magenta",
                   gravity = "north") %>%
    image_trim() 

  ## Here I have a problem
  image_read("./temp/Day_2_XY06_CH4.png") %>%
    image_border(color = "#FFFFFF", geometry = "25x0") %>%
    image_annotate(text = "EdU",
                   font = "serif",
                   weight = 700,
                   size = 20,
                   color = "magenta",
                   degrees = -90,
                   gravity = "west") %>%
    image_trim() 
dcaud commented 2 years ago

I realize you may be pointing out inconsistent behavior, but a work-around may be to adjust the location:

wizard %>%
  image_border(color = "#504050", geometry = "25x25") %>%
  image_annotate(text = "EdU",
                 font = "serif",
                 weight = 700,
                 size = 20,
                 color = "white",
                 boxcolor= "green",
                 degrees = -90,
                 location = "+12+0",
                 gravity = "west"
                 ) 

image

I guess specifying the location offset would be difficult programatically (i.e., when the font changes or the font size changes, it wouldn't be clear what good location values would be).