elixir-image / image

Image processing for Elixir

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error with Image.Text.add_background_padding!

tielur opened this issue · comments

I'm getting this error when trying to use Image.Text.add_background_padding!

iex(1)> image = Image.open!(Path.expand("~/Downloads/cropped.jpg"))
%Vix.Vips.Image{ref: #Reference<0.2240260505.480641047.260946>}
iex(2)> image |> Image.Text.add_background_padding!(padding: 2)
** (Image.Error) operation build: linear: vector must have 1 or 3 elements
    (image 0.38.4) lib/image/text.ex:748: Image.Text.add_background_padding!/2
    iex:2: (file)
    ```

While on this topic I'm looking for a way to add a border to an image expanding outward. Meaning that if I have an image with text that is right on the border, I want to make the image bigger by adding a border. Is Image.Text.add_background_padding the correct function to be using for that?

I noticed that Image.Text.add_background_border does the opposite of what I want, it creates a border of the existing image moving inward which covers parts of my image

For example:

jose = Image.open!(Path.expand("~/Downloads/jose.jpeg")) |> Image.p()

iTerm2 nvxZRQ

jose
|> Image.Text.add_background_border!(background_stroke_color: :white, background_stroke_opacity: 1.0, background_stroke_width: 50) 
|> Image.p()

iTerm2 o0IYxO

But I'm actually trying to produce an image like this:
output-onlinejpgtools (1)

Thanks for the report @tielur.

I believe the error comes from your base image having an alpha band. If you do Image.flatten/2 like:

image |> Image.flatten() |> Image.Text.add_background_padding!(padding: 2)

I don't think you'll get an error - and I don't believe you'll get the expected result either. Image.Text.add_background_padding/2 isn't the right tool for this job - its uses padding in the text management sense which is what you are seeing.

What you're after will require a couple of steps - although I'm certainly open to adding a function to make this easier and more explicit

  1. Create a new image with dimensions the size of the base image plus the border you want
  2. Compose the base image over the background image, in the central location.

Example

iex> text = Image.Text.text! "This is some text"
%Vix.Vips.Image{ref: #Reference<0.700115826.751960085.157529>}
iex> border = Image.new!(Image.width(text) + 50, Image.height(text) + 50, color: :blue)
%Vix.Vips.Image{ref: #Reference<0.700115826.751960085.157534>}
iex> composed = Image.compose! border, text, x: :center, y: :center
%Vix.Vips.Image{ref: #Reference<0.700115826.751960085.157535>}
image

Wow, a young José :-)

Wow, a young José :-)

I'm hoping one day he'll see this and we'll get the famous multi-colored hearts

image |> Image.flatten!() |> Image.Text.add_background_padding!(padding: 2)
** (Image.Error) operation build: linear: vector must have 1 or 3 elements
    (image 0.38.4) lib/image/text.ex:748: Image.Text.add_background_padding!/2
    iex:42: (file)

Looks like it still errors but I'll pursue the compose option you mentioned for my expected output

iex> jose = Image.open!("/Users/kip/Desktop/jose.png")
%Vix.Vips.Image{ref: #Reference<0.700115826.751960090.157163>}
iex> border = Image.new!(Image.width(jose) + 50, Image.height(jose) + 50, color: :gray)
%Vix.Vips.Image{ref: #Reference<0.700115826.751960090.157168>}
iex> composed = Image.compose! border, jose, x: :center, y: :center
%Vix.Vips.Image{ref: #Reference<0.700115826.751960090.157169>}
image

Looks like it still errors but I'll pursue the compose option you mentioned for my expected output

I'll check it out.

The compose method is exactly what I was looking for, thank you. Let me know if you need anything to help reproduce the add_background_padding error

I've also fixed Image.embed/4 which is probably a better solution to your requirement than composition - probably slightly more efficient.

iex> jose = Image.open!("./test/support/images/jose.png")
%Vix.Vips.Image{ref: #Reference<0.1922390019.1770913817.240448>}
iex> Image.embed! jose, Image.width(jose) + 50, Image.height(jose) + 50, background_color: :blue
%Vix.Vips.Image{ref: #Reference<0.1922390019.1770913817.240463>}
Rex> jose = Image.open!("./test/support/images/jose.png")
%Vix.Vips.Image{ref: #Reference<0.1922390019.1770913815.239334>}
iex> border = Image.embed! jose, Image.width(jose) + 50, Image.height(jose) + 50, background_color: :blue
%Vix.Vips.Image{ref: #Reference<0.1922390019.1770913817.240464>}
iex> Image.preview border
image