elixir-mogrify / mogrify

Image processing in Elixir (ImageMagick command line wrapper)
MIT License
570 stars 65 forks source link

Adding caption on image #92

Open stonetwig opened 3 years ago

stonetwig commented 3 years ago

Hello, I use your library and thanks for all the great work

This "issue" is more of a question than anything else. I am trying to add a caption onto an image but I think I am failing to understand how to use the custom method or if it even is supported?

This is what I have come up with when trying to add a caption via the convert command in imagemagick:

open(image_path)
        |> custom("convert", "-background '#0008' -fill white -gravity center -size 50x30 caption:my caption")
        |> save(path: image_path)

Is it possible somehow or is it not yet supported?

Thanks in advance

bunnymatic commented 3 years ago

I'm not an owner/maintainer here but I don't believe that is supported yet. label and caption and annotate which are all ways to get text on an image require special command processing.

The following

filename |> 
  Mogrify.open |> 
  Mogrify.custom("label","the caption") |> 
  Mogrify.custom("gravity","center") |>   
  Mogrify.custom("fill","red") |> 
  Mogrify.save(path: "out.jpg")

generates a command like this:

mogrify -label "the caption" -gravity center -size 100x100 -fill red -write out.jpg inputfile.jpg

The problem here is that -label "the caption" is the wrong format for building labels. The command really needs a command that includes label:'the label' and it can't go wherever - it has to be in the right place within the command (after all the options i think). Additionally, when you use ImageMagick to create a label or caption, it is really just generating an image with that label. To get it on top of another image you need to combine them with composite.

I think to get the support out of this library, it probably needs a couple of custom command helpers that could deal with label and composite and putting the whole thing together. If you check out the code, there is a custom method to generate a histogram which is also a command in ImageMagick that needs some custom argument construction. That might be an example of how that functionality could be added to the library.

These links (ImageMagick docs and a SO post) should give some context: