I have a png image.
How can I cut a small rect from this image using imagemagick? (make a transparent hole in image)
For example:
Source:
Destination:
It seems I figure it out.
convert u1.png -alpha set -region 40x30+15+5 -alpha transparent u3.png
It cuts a rect (40x30) from u1.png
Related
I have an image that all four margins are transparent alpha channels and I want to crop all margins and only keep the non-alpha channel parts. Is there any way to do it with ImageMagick? Take this image as an example but consider its transparent margins!
You can do that in Imagemagick using -trim to automatically remove the constant transparent regions.
convert img.png -trim +repage result.png
I would like to blur only a part of an image. The part to blur is always rectangular, so that I can easily use the following command:
magick source.jpg -region 1000x1000+0+500 -blur 0x20 result.jpg
This works, but is pretty slow for large images. Since I have to process thousands of files again and again, this will simply take too long.
Therefore, I decided to do the blurring by downscaling and upscaling the image back to the original size. However, since this will blur the full image, I have tried to accomplish the task using the following steps:
take the original image as background
create a copy of the original image
blur the copy using down-/upscaling
crop the desired region from the blurred copy
compose the original and the blurred&cropped copy
I am already pretty close (I hope), but when composing the two images, the cropped image will always be positioned in the top-left corner of the original image - instead of the original position from the source image. This is my current command:
magick source.jpg ( -clone 0 -resize 5% -resize 2000% -crop 1000x1000+0+1000 ) -composite result.jpg
I have read in the documentation that the original canvas size will be retained when using the -crop operation, and that this size and position will be used when using -composite. However, this doesn't seem to work in my case. Does anyone have an idea why?
I have tried to use -repage, -extent and other options to define the size and position of the cropped image, but to no avail so far.
I would try -flatten in your command as that is used for layers.
You can do it with a mask image (of any shape) in ImageMagick. Though I am not sure if that will be faster than your scaling method.
Input:
Mask:
(note: blurring occurs where mask is black)
magick lena.jpg -write-mask mask.png -blur 0x3 +write-mask lena_blurred.png
Result:
I have an image of 4500x5400, I want to resize it for the height 4200 keeping the width ratio, however the width of the image needs to be 4050, leaving the sides transparent.
I have this ImageMagick command:
convert file.png -resize 4500x5400 -gravity center -background transparent -extent 4050x4200 out.png
However it's cutting the top and the bottom, while it needs to appear.
Do you guys have any idea of how I can make it work?
Appreciate your time!
Try this...
convert input.png -resize 4050x4200 -background none -gravity center -extent 4050x4200 output.png
The "-resize" fits your input image within a container of that size. The "-extent" makes sure the total canvas is that size. The "-background" and "-gravity" make sure extra space is filled with transparent and that the input image is located in the center of the output canvas.
I want to blur an image with transparent background using ImageMagick. I've trimmed this image to its content, so it doesn't have any surrounding whitespace/transparency.
The above part works fine, but:
When I blur the trimmed image, it will (obviously) overflow the boundaries. So the question is: How can I blur a trimmed image, while extending its boundaries to the right size before?
Extend the background with transparent pixels, blur and then re-trim:
convert original.png -background none -gravity center -extent 200x200% -blur ... -trim result.png
I'm working on a photo application and I need some advice how should I solve the following with Graphics/ImageMagick.
Given a photo with resolution: 2048x1536
Given a specified resolution: 1864x1228
Resize the image and fill the specified resolution with the image (now it's 1864x1398)
Highlight the area of the original image will be cropped (to 1864x1228)
I have a working solution which resizes and crops the image properly:
IMOperation resizeOp = new IMOperation();
resizeOp.addImage();
resizeOp.resize(MAX_WIDTH, MAX_HEIGHT, "^");
resizeOp.gravity("center"); //centered image with crop the top/bottom parts
resizeOp.crop(MAX_WIDTH, MAX_HEIGHT, 0, 0);
resizeOp.addImage();
ConvertCmd cmd = new ConvertCmd(true);
cmd.run(resizeOp, fileName, outputFileName); //cropped, center filled image (1864x1228)
The question is how should I do the following: show the full image instead of the cropped version and highlight the area of the image will be cropped. I'd prefer with red border around the cropped image and show with the cropped parts with alpha layer.
I have an idea which I don't like very much: generate an image from the original with alpha layer and put the cropped image on it with red border. It doesn't seem to be the optimal solution :) My other idea is to do this with javafx imageviews, but it seems suboptimal as well.
Notes:
I'm using im4java with GM. I accept a command line solution too (and I'll figure out and post it in im4java ;)
We can restrict the conversation about horizontal images only, I can figure out the vertical operations
Any comments would be highly appreciated.
The oneliner imagemagick convert (remove the line breaks):
convert ( in.jpg -resize 1864x1228^ -fill white -colorize 50% )
( in.jpg -resize 1864x1228^ -gravity center -crop 1864x1228+0+0 )
-gravity center -composite out.jpg
In bash, you have to escape the () characters with \!
I solved it in im4java with sub operations:
IMOperation op = new IMOperation();
op.openOperation();
op.addImage(); //input image
op.resize(MAX_WIDTH, MAX_HEIGHT, "^");
op.fill("white");
op.colorize(50);
op.closeOperation();
op.openOperation();
op.addImage(); //input image
op.resize(MAX_WIDTH, MAX_HEIGHT, "^");
op.gravity(GRAVITY_OPT_CENTER); //centered image with crop the top/bottom parts
op.crop(MAX_WIDTH, MAX_HEIGHT, 0, 0);
op.closeOperation();
op.gravity(GRAVITY_OPT_CENTER);
op.composite();
op.addImage(); // output image
Notes:
It still doesn't contain red border (I can't add border only to the second image).
I decided to use "fade to white" effect, instead of playing with alpha channel.
Example:
input: http://i.stack.imgur.com/lhMgT.jpg
output: http://i.stack.imgur.com/EYO7c.jpg