imagemagick Fill region outside of rectangle - imagemagick

I have an image and I want to fill with some color (e.g. blue) parts of that image which are outside of a rectangle. Position of left upper corner and width/height of this rectangle relative to the left upper corner of the original image are known.
Here borders of the original image are in red and borders of the rectangle are in black. Blue area should be filled.
How can I do this? I tried to create a tmp image and use a mask but it seems to also fill transparent regions inside the rectangle which is not what I want.
Also, there is probably a way to do it without creating intermediate images? I'd be interested in both options though, because I'm not sure how requirements will change over time -- maybe in the future I'd have to apply some other shape (not a rectangle), so having a way to apply an arbitrary mask would be better in that case.
Example:
image:
coordinates of the rectangle (assuming x axis runs from left to right, y axis from top to bottom, with (0,0) being left-upper corner of the original image):
left-upper corner (117,-24)
right-bottom corner (1117,1676)

Find a colour that doesn't exist in the original image. Hint: use -unique-colors
Fill/draw the black image with that colour
Make everything that is not that colour blue. Hint: use -fill blue +opaque THATCOLOUR
The blue pixels are your mask
Not sure if I have understood you correctly, but here is what I get for the first part:
magick 7uZfM.png -alpha deactivate -fill yellow -draw "rectangle 117,-24 1117,1676" result1.png
And then the second part:
magick 7uZfM.png -alpha deactivate -fill yellow -draw "rectangle 117,-24 1117,1676" -fill blue +opaque yellow -transparent yellow result2.png
Ok, now with the original image showing through:
magick 7uZfM.png \( +clone -alpha deactivate -fill yellow -draw "rectangle 117,-24 1117,1676" -fill blue +opaque yellow -transparent yellow \) -flatten -background magenta result3.png

Related

ImageMagick Colorize GIF and Preserve Transparency

I have a small icon GIF that I would like to change the color. All opaque GIF pixels should be replaced by this new color, but preserve transparency for the gif. Then I need to place that new colored GIF over a background map, all in one command line. Here is what I have, but it's not changing all gif opaque pixels. Attached is the GIF icon. (Edit: I know im only targeting the white here, but just can't figure out how to target ALL opaque pixels in the GIF)
exec("{$convert} {$map_image} \( {$map_icon} -fill orange -opaque white -geometry +1700+600 \) -composite map2.jpg ");
Use +opaque none to fill all non-transparent colors in ImageMagick. The +opaque means everything "but" that color.
convert grid.gif -fill orange +opaque none x.png

Imagemagick - Inverse

I've a png image containing transparent pixels and colored pixels (mainly white).
I'd like to transform all transparent pixels to white pixels and all white pixels to transparent pixels within a given rectangle.
My idea would be to
convert the white pixels to red
the transparent colors to white
and the red colors to transparent
. Here' s my code:
1) convert ldl_0.png -fuzz 10% -fill red -opaque white lx.png
2) convert lx.png -background white -alpha remove -alpha off lx2.png
However I can' t figure out how do I transform red colors to transparent. How do I do that?
Also how can I force to do this only within a given rectangle?
Thank You.
-----
Try this in ImageMagick. Negate the alpha channel and turn the whole RGB channels to white.
convert in.png -channel a -negate +channel -fill white -colorize 100 out.png

remove white background with ImageMagick but not the white inside picture?

im trying to delete the background with a batch process in pictures of products for an e-commerce site.
The problem is that the script also remove the white color in the inside of the product, leaving the product transparent in some areas..
For example:
Command:
convert *.jpg -set filename: %t -fuzz 5% -transparent white %[filename:].png
this is the best I can get..im ok with this result around the product, but I need that the white inside the product remains white and not transparent.
The problem with your current approach is that it doesn't respect boundaries, it is just applied globally, making all white pixels transparent regardless of their connectivity to the background.
Instead, you will get on better using a "floodfill" that only floods into areas that are within the fuzz distance of the top-left corner pixel.
So, I chose an unused colour of magenta so you can see what is happening:
convert product.jpg -fuzz 5% -fill magenta -draw 'color 0,0 floodfill' result.png
You would then follow that with the command to make magenta transparent like this:
convert product.jpg -fuzz 5% -fill magenta -draw 'color 0,0 floodfill' -transparent magenta result.png

Imagemagick: how do I an irregular region of color

When I have an image like the following, is it possible to convert the outer (!) orange to transparency? It's like trim, but I need to remove the outer region of orange color and convert that color to transparent. However, the inner orange must not be touched.
You can try flood filling the outer color.
convert Zx5HL.png -fill transparent -fuzz 50% -draw 'color 0,0 floodfill' output.png
... but that'll introduce clipping artifacts.

Shifting centered images / delete left border and retain size

I have about 400 images that are the same size, but the actual image content is centered against a white border. This looks odd when I display some of them in a list because the left hand border size varies against the image content.
I'd like to basically remove the left hand border but keep the image size, and fill the right hand side with white. Thanks
I would add a white border to your whole image and use -trim which will remove the border and the white L/H border. Then use an -extent with a -gravity west and a -background white to add the border to the RH side.
Something like:
convert input.jpg -bordercolor White -border 2x2 -trim +repage -background white -gravity west -extent 500x800 output.png
Where 500x800 is the final size of your image.

Resources