Shifting centered images / delete left border and retain size - image-processing

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.

Related

imagemagick Fill region outside of rectangle

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

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

Image Magick: dimmed caption with border

I am using Image Magick to overlay a dimmed caption to an image, with IM automatically choosing the best fontsize:
convert -background '#0008' -fill white -geometry +0+330 -size 370x60 caption:$title $image +swap -composite $imageOutput
My problem is that there is not enough space around the text, I would like to add some "padding". I usually do that with the -border option but if I add this to my command above, the caption is not dimmed anymore.
Do you have a solution to create a dimmed caption with enough room around the text?
I have a solution, but it's a bit of a kludge because I had trouble extending or bordering a semi-transparent background. In the end, I just constructed the caption on a black background and bordered it in black, then I tweaked the alpha channel afterwards:
convert -background black -bordercolor black -fill white \
-size 370x60 caption:"This is the title" \
-trim -border 20 -channel A -fx '(lightness/2)+.5' \
-geometry +0+200 background.gif +swap -composite result.png
The only tricky part is -channel A -fx .... The first part means that we are only affecting/modifying the alpha/opacity channel. The 0.5 means that all pixels become at least 50% opaque, and (lightness/2) means that absolutely white pixels, i.e. your lettering, (which will have a lightness of 1) become fully opaque because 0.5+(1/2) totals to one. The point of this is to preserve the anti-aliasing around the edges of the letters to some degree.

Imagemagick: After centering image, move it left by 50px

I am currently centering an image on a white background like below:
... -gravity center -background white -extent 400x400 ...
I find that after centering, I need to move the image (~250x250, can vary slightly) to the left of the center by 50px. How can I achieve this?
I tried using -geometry after -gravity center but that doesn't work.
Try
convert ... -gravity center -background white -extent 400x400+50+0 ...
Where the ImageMagick documentation talks about "geometry" in this context, it's talking about the "geometry string" associated with the "-extent" option, not about a "-geometry" option.

Use ImageMagick to place an image inside a larger canvas

Getting started with ImageMagic and trying to find a way to do this... If an image is less than 50 pixels tall or 50 pixels wide, I'd like to place it (un-scaled) in the horizontal/vertical center of a new 50x50 pixel canvas on top of a white background - and save that as the new image. Anyone know if this is possible with ImageMagick? Thanks!
I used -extent to do this:
convert input.jpg -gravity center -background white -extent 50x50 output.jpg
I wanted to do the same, except shrink the image to 70% inside. I used this:
convert input.png -resize 70%x70% -gravity center -background transparent -extent 72x72 output.png
Not exactly what was requested but hopefully it will help someone ;).
I have once used this code to place an image in the center of a new canvas with white background. hope this will help you
convert -background white -gravity center your_image.jpg -extent 50x50 new_image.jpg
See cutting and bordering for a huge number of examples. Here's one simple way you might do it:
convert input.png -bordercolor Black -border 5x5 output.png
Of course, you'll need to calculate the size of the border to add (if any) based on the dimensions of the input image. Are you using an ImageMagick API, or just the command line tools?
I tried this:
convert test.jpg -resize 100x100 -background black -gravity center -extent 100x100 output.png
You can use single composition to do this. So it would look something like this:
convert -size 50x50 xc:white null: ( my_image.png -coalesce ) -gravity Center -layers Composite -layers Optimize output.png
To modify the source image you need to use mogrify:
mogrify -gravity center -background white -extent 50x50 source.jpg
If an image is less than 50 pixels tall or 50 pixels wide
In my case, the images were much larger than the destination canvas, and weren't square. So I resize them proportionally to fit inside. Example:
convert in.png -resize 46x46 -background none -gravity center -extent 50x50 out.png
The 46x46 limit ensures a 2 pixel margin minimum. Note that the above does not distort the image, e.g. a rectangle does not become a square.
I used background none for a transparent background, but you can choose a solid color instead.

Resources