Reputation: 2525
I have hexagon image with parts from other close hexagons. Because the image by default is square, but hexagon is, well, hexagon.
It looks like this:
But I'd like to remove those triangles outside the border to keep the hexagon with transparent background.
I've tried with autotrim script and tried different combinations of imagemagick commands, which have the goal to extend white border and replace white color to transparent.
convert base.png -fuzz 10% -trim +repage out.png
magick base.png \( -clone 0 -fuzz 15% -fill white +opaque white -draw "color $centx,$centy floodfill" \) -alpha off -compose Src -composite out.png
composite -compose Src -gravity Center base.png -alpha set out.png
And I've used command, based on this answer
magick base.png \( -clone 0 -fuzz 15% -fill black +opaque "rgb(255,255,255)" -fill white +opaque black -fill white -draw "color $centx,$centy floodfill" \) -alpha off -compose copy_opacity -composite out.png
But nothing worked.
So, my question is, how to extend border of image and replace background color or how to crop image inside border but preserve hexagon form?
Upvotes: 0
Views: 1087
Reputation: 53164
Using Imagemagick 6.9.10.78 on Mac OSX, I get the following:
Input:
Make corners transparent (using floodfill at each corner):
convert DB8m5.png -fill none -fuzz 95% \
-floodfill +0+0 black \
-rotate 90 -floodfill +0+0 black \
-rotate 90 -floodfill +0+0 black \
-rotate 90 -floodfill +0+0 black \
-rotate 90 \
result.png
Crop to Inner Rectangle:
convert DB8m5.png -bordercolor white -border 10 \
-define trim:percent-background=0% \
-define trim:background-color=white \
-trim +repage \
result2.png
ADDITION:
If you need to remove the white as well as the corners, then do the following:
convert DB8m5.png -fill white -fuzz 95% \
-floodfill +0+0 black \
-rotate 90 -floodfill +0+0 black \
-rotate 90 -floodfill +0+0 black \
-rotate 90 -floodfill +0+0 black \
-rotate 90 \
-bordercolor white -border 1 \
-fuzz 0 -fill none \
-floodfill +0+0 white \
-shave 1 \
result.png
For IM 7, use magick in place of convert.
Upvotes: 1
Reputation: 2525
The solution I came up with was to make a chain of calls to change the image. This is modified and extended solution provided here
CENT_X="$(magick $filename -format "%[fx:w/2]" info:)"
CENT_Y="$(magick $filename -format "%[fx:h/2]" info:)"
magick $filename \( -clone 0 -fuzz 1% -fill black +opaque "rgb(255,255,255)" -fill white +opaque black -fill white -draw "color $CENT_X,$CENT_Y floodfill" \) -alpha off -compose copy_opacity -composite $filename_prep.png
convert $filename_prep.png -fuzz 10% -transparent white $filename
As I have a bunch of images, I wrote bash loop script, which process each image one by one redeclaring CENT_X/CENT_Y
values on each loop.
Upvotes: 0
Reputation: 5395
With ImageMagick v6 or v7 you can crop a pretty accurate hexagon from any input image with a command like this...
convert -size 1000x1155 xc:white \
-background none -virtual-pixel none \
-distort SRT 60 -distort SRT 60 \
wizard: +distort SRT "%[fx:min(t?1:v.w/u.w,t?1:v.h/u.h)] 0" \
-gravity center -compose srcin -composite -shave 1 hexout.png
That begins by creating a mask that starts with a white rectangle the dimensions of a vertical hexagon. It rotates the rectangle 60 degrees a couple times on a transparent background to clip off the corners and make a hexagon mask.
Then it reads the input image. In this example I used the IM built-in "wizard:". Replace that with the file name of your input image.
Next it uses "+distort SRT" to re-scale the mask to fit the maximum possible size within the input image. The FX expression isn't as complicated as it looks.
It sets the gravity to "center" and the compose method to "srcin", so the result of the composite will be the center hexagon punched out from the input image and placed on a transparent background .
The "+distort" operation adds a row of pixels to every side, so the command finishes by shaving off one pixel all the way around and writing the output file.
The command above works in a bash shell with IM 6.8.9-9. To use it with IMv7 change the "convert" command to "magick". To run it on Windows command line change the continued-line backslashes "\" to carets "^".
Upvotes: 0