[SOLVED] Imagemagick transparent background and white image outlines
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Imagemagick transparent background and white image outlines
Since it did not fail for me, that would be one indication that the problem is not likely with ImageMagick or libpng as I had questioned. However, one can never tell. But my recommendation is to see what the package manager say first. If they cannot verify the issue, then you might report the issue here. But the IM developers would have to reproduce the issue and I do not know if they have access to your same package and hardware.
Re: Imagemagick transparent background and white image outlines
So far this is what I have.
1st, I remove all white on the outside border. (I decided not to mess with the white inside the image)
2, get the alpha mask
3, enlarge the border (to create room for extra working space), dilate the edges (so I can create a white outline in the final result), and trim off the extra
4, composite the final
Do my commands look good? And I also want to antialias the edges of tmp3, I haven't figured out how to smooth them yet
1st, I remove all white on the outside border. (I decided not to mess with the white inside the image)
2, get the alpha mask
3, enlarge the border (to create room for extra working space), dilate the edges (so I can create a white outline in the final result), and trim off the extra
4, composite the final
Do my commands look good? And I also want to antialias the edges of tmp3, I haven't figured out how to smooth them yet
Code: Select all
# remove all outside borders
magick image.png -bordercolor white -border 1x1 \
-fill transparent -floodfill +0+0 white \
-shave 1x1 tmp.png
# get mask image
magick tmp.png -alpha extract tmp2.png
# expand border, create white edges along outside, re-trim image
magick tmp2.png -bordercolor black -border 20x20 \
-morphology Dilate Octagon:4 \
-trim tmp3.png
magick image.png tmp3.png -alpha off -compose copy_opacity -composite result.png
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Imagemagick transparent background and white image outlines
I think this might be what you want.
Input:
line 1-2: read the input and flood fill to make white into transparent
line3: create an all white image
line4: create a ring of white outside the area of the transparent part of the alpha channel, but save the alpha channel to mpr
line5: composite the input, white and ring image
line6: dilate the alpha channel and blur to antialias
line7: put that image into the alpha channel of the result of line5
line8: write the output
Note I have left in tmp image so you can see the steps. You can remove the +write tmpX.png when satisfied.
Input:
line 1-2: read the input and flood fill to make white into transparent
line3: create an all white image
line4: create a ring of white outside the area of the transparent part of the alpha channel, but save the alpha channel to mpr
line5: composite the input, white and ring image
line6: dilate the alpha channel and blur to antialias
line7: put that image into the alpha channel of the result of line5
line8: write the output
Note I have left in tmp image so you can see the steps. You can remove the +write tmpX.png when satisfied.
Code: Select all
magick \( image.png +repage -bordercolor white -border 1x1 \
-fill transparent -floodfill +0+0 white -shave 1x1 +write tmp0.png \) \
\( -clone 0 -fill white -colorize 100% \) \
\( -clone 0 -alpha extract +write tmp1.png -write mpr:alpha -morphology edgeout octagon:10 +write tmp2.png \) \
-alpha off -compose over -composite +write tmp3.png \
\( mpr:alpha -morphology dilate octagon:10 -blur 0x1 -level 50x100% +write tmp4.png \) \
-alpha off -compose copy_opacity -composite \
result1.png
Re: Imagemagick transparent background and white image outlines
That's exactly my intended result, thank you very much! I will study the steps carefully
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: [SOLVED] Imagemagick transparent background and white image outlines
If on Unix-like system, see my script, contour, at my link below.
Re: [SOLVED] Imagemagick transparent background and white image outlines
Is there a way to create some extra working space and trim it afterwards so that the edgeout and dilation don't get clipped if they go beyond the previous image bounds? (I'm trying to figure out how to do that right now)
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: [SOLVED] Imagemagick transparent background and white image outlines
Are you asking about my script or about the code above? In either case, can you post an example of the issue with an input and output.
Re: [SOLVED] Imagemagick transparent background and white image outlines
I mean the code above. (Your script is really nice by the way)
This is an example of the edges clipped (not enough room for expansion of edgeout and dilation)
And here I increased the image size a little to account for expansion (unfortunately, I wasn't able to figure out how to trim the result yet)
---
And lastly, after doing the edgeout and dilation, there are sometimes some gaps inside left over. I figure I can use your previous code somewhere to fill those in. (The number is so high because any and all gaps should be removed)
Image example. You can see that the morphology has caused there too be a closed gap now.
Thank you for your patience, I really appreciate it. Although I'm new to imagemagick, I'm finding it to be quite fun reading the documentation. I'm learning a lot! This is also turning out quite nicely
This is an example of the edges clipped (not enough room for expansion of edgeout and dilation)
And here I increased the image size a little to account for expansion (unfortunately, I wasn't able to figure out how to trim the result yet)
Code: Select all
magick \( image.png +repage -bordercolor white -border 1x1 \
-fill transparent -floodfill +0+0 white -shave 1x1 \
-bordercolor transparent -border 30x30 +write tmp0.png \) \
---
And lastly, after doing the edgeout and dilation, there are sometimes some gaps inside left over. I figure I can use your previous code somewhere to fill those in. (The number is so high because any and all gaps should be removed)
Code: Select all
-define connected-components:mean-color=true \
-define connected-components:area-threshold=500 \
-connected-components 4 \
Thank you for your patience, I really appreciate it. Although I'm new to imagemagick, I'm finding it to be quite fun reading the documentation. I'm learning a lot! This is also turning out quite nicely
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: [SOLVED] Imagemagick transparent background and white image outlines
Your last image seems to be the same as the second. Using connected-components is what I would have suggested to fill the gaps.
To add more room use -bordercolor and -border with a large enough border to account for the room you need. You can remove that at the end by using -shave with the same value as you used for -border.
To add more room use -bordercolor and -border with a large enough border to account for the room you need. You can remove that at the end by using -shave with the same value as you used for -border.
Re: [SOLVED] Imagemagick transparent background and white image outlines
Here is my final code. Removes all the gaps inside, and trims the final image. Works great, thank you very much!
Edit: gah, actually, trim isn't working! I can't figure it why
Edit: gah, actually, trim isn't working! I can't figure it why
Code: Select all
magick \( image.png +repage -bordercolor white -border 1x1 \
-fuzz 10% -fill transparent -floodfill +0+0 white -shave 1x1 \
-bordercolor transparent -border 25x25 \) \
\( -clone 0 -fill white -colorize 100% \) \
\( -clone 0 -alpha extract -write mpr:alpha -morphology edgeout octagon:20 \
-define connected-components:mean-color=true \
-define connected-components:area-threshold=500 \
-connected-components 4 \) \
-alpha off -compose over -composite \
\( mpr:alpha -morphology dilate octagon:20 \
-define connected-components:mean-color=true \
-define connected-components:area-threshold=500 \
-connected-components 4 -blur 2x20 -level 50x100% \) \
-alpha off -compose copy_opacity -composite -trim \
result.png