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
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
- Create a new image with dimensions the size of the base image plus the border you want
- 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](https://private-user-images.githubusercontent.com/15895/288282438-0c9c6ca7-57c5-4b7a-b29d-dfb60f5be05a.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjA2NjUyODUsIm5iZiI6MTcyMDY2NDk4NSwicGF0aCI6Ii8xNTg5NS8yODgyODI0MzgtMGM5YzZjYTctNTdjNS00YjdhLWIyOWQtZGZiNjBmNWJlMDVhLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MTElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzExVDAyMjk0NVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWJiOGIzYzdmOGRlNGQyZjY5NDQzNDE4ODBlYmYxOTliYTc0NzdhMzQyNDNkMjYzMGU4ZjI3MTQ2Njk2NzJiNTImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.j6cpqBus1QuGreIiU7GQdvMKBGWP2_hGzLK25d4X2as)
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](https://private-user-images.githubusercontent.com/15895/288283698-72e702a5-712f-42a5-b590-eba5c2fbbd87.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjA2NjUyODUsIm5iZiI6MTcyMDY2NDk4NSwicGF0aCI6Ii8xNTg5NS8yODgyODM2OTgtNzJlNzAyYTUtNzEyZi00MmE1LWI1OTAtZWJhNWMyZmJiZDg3LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MTElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzExVDAyMjk0NVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWIxMmJiZWU2ZTc5MDZiMzNjYTk3ZjllY2U0ZDljYWM0ZDM0OWQzNmU4MTcyZTcxMjI5MjZlOThhMmYxOTU0MWQmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.Q35JDCOAeTIG42SFDFx0Eo2fWAkqrM3-J2P_P5tRmzw)
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](https://private-user-images.githubusercontent.com/15895/288620085-74c9e534-a320-4f24-93af-78b40de1af1a.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjA2NjUyODUsIm5iZiI6MTcyMDY2NDk4NSwicGF0aCI6Ii8xNTg5NS8yODg2MjAwODUtNzRjOWU1MzQtYTMyMC00ZjI0LTkzYWYtNzhiNDBkZTFhZjFhLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MTElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzExVDAyMjk0NVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTY0MzI1NzhlNmI5OTY4Mjg1YzY5ZTRjZWU3YmY1ZWM1NGZiOWMxNDc0NWQ0YmQyZTZlMTc4MjA5MGQ5YTgyMzYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.y8U7fkndhchOZyfneXnvHPoGbSZZvV_X15h4n3-fYQo)