Blur part of an image only - imagemagick

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:

Related

How can I specify a background in Imagemagick mogrify based on the colour of an existing pixel?

I want to use mogrify to set the background colour of a large number of images to be whatever colour is at a specific pixel, to make them square.
The vast majority have a single colour in the image background, or are photos in front of a single colour (so with only slight variations from shadows, etc.).
(The specific purpose in this case is to make the images all the same size and square for StyleGAN2-ADA training, so I want to avoid big "letterbox" rectangles where possible as it would be seen by the discriminator as relevant to the image, where a more faded-in background that approximately matches would be more likely to be ignored. Specifically, I have thousands of pictures of single dolls and action figures from various sources, some of which are "trimmed out" to have a truly solid colour background, others of which are against solid colour tables/walls/etc, for instance, from eBay images and such.)
Since they do not all have the same colour in the image background (the colour in the image, as opposed to the 'background colour' setting as per ImageMagick's jargon), I need to sample a pixel and set the background, but I can't figure out how. I tried things based on methods used to set the whole image to one colour, to no avail.
I have tried:
mogrify -verbose -resize 1024x1024 -gravity center -background 'p{10,10}' -extent 1024x1024 -resize 256x256 *.jpg
and
mogrify -verbose -resize 1024x1024 -gravity center -background "%[pixel:p{10,10}]" -extent 1024x1024 -resize 256x256 *.jpg
and neither works. I can't find any other possibilities in the documentation.
EDITED TO ADD: While testing various commands I came across a way to modify your original command to make it work on ImageMagick versions as far back as IMv6.8.
mogrify -resize 1024x1024 -set background "%[pixel:p{10,10}]" \
-gravity center -extent 1024x1024 -resize 256x256 *.jpg
The significant difference is setting the background color in an unusual way. Instead of the normal option -background <color>, this command uses -set background <color>. Then it behaves as expected using that +10+10 color as the background in the "mogrify" command.
For ImageMagick v7 use magick mogrify instead of just mogrify.
The following was my original answer. The suggestion for IMv6 "convert" may be quite useful for some workflows, but the answer above seems to be the simplest, most direct route.
PREVIOUS ANSWER:
ImageMagick v6 won't do that inline parsing of the color, but there are ways to get the same result, usually with IM's "convert" in a "for" loop in your shell. I don't know which shell you're using so I don't know how you'd write a "for" loop, but running this command inside the loop on each image should give you the results you described...
convert $image -resize 1024x1024 ( +clone -crop 1x1+10+10 ) +swap \
-resize 1024x1024 -gravity center -composite -resize 256x256 $image
That reads in the image, resizes it, makes a clone inside the parentheses, and extracts that pixel at +10+10. After the parentheses that single pixel get resized to a 1024x1024 square. Then setting the gravity to "center" and compositing the input image over that colored square gives you the result you described.

Rotate and crop an image, while keeping its original size

I have a JPG image with a known size of 3072x2048. Now I want to rotate that image by any degrees (e.g. 45), while keeping its original size. Thus - using ImageMagick on the command line - I first want to rotate, then crop the image, like this:
convert -rotate 45 -gravity center -crop 3072x2048 +repage original.jpg rotated-45.jpg
By using -gravity center I specify to crop the center part of the image, which is what I want. This operation produces four output images:
rotated-45-0.jpg
rotated-45-1.jpg
rotated-45-2.jpg
rotated-45-3.jpg
The first image rotated-45-0.jpg is exactly the final image I want to get. The other three I don't need. I could delete them, but I think it would be nicer to not generate these "extra" images in the first place. So I thought I could do it with this command instead:
convert -rotate 45 -gravity center -crop 3072x2048+0+0 +repage original.jpg rotated-45.jpg
This only produces one output image, however, now the top-left corner of the image is being cropped. So apparently the -gravity center is not used any longer.
Any ideas what I am missing here?
Using ImageMagick you can rotate an image any number of degrees while keeping the original canvas dimensions using "-distort SRT"...
convert original.jpg -virtual-pixel black -distort SRT 45 rotated-45.jpg
Use "-virtual-pixel" to specify how you want to handle the parts that were outside the canvas before the rotation. In this example I used "black". You can use black, white, background, tile, mirror, or none.

Compose image with mask image using ImageMagick

Suppose I have some image a.jpg and some other image b.jpg.
The desired output out.jpg should be obtained by copying all the pixels from b.jpg that are not black onto a.jpg, all other pixels shall remain untouched.
I tried using composite but had no success whatsoever.
EDITED TO ADD: A solution here can be quite simple and generic, but going forward, please remember to always include your version of ImageMagick and which OS or platform you're working on. There are some syntax differences that can make that important.
At the very simplest, using ImageMagick v6, you should be able to do something like this...
convert b.jpg -background none -transparent black a.jpg +swap -composite out.jpg
That reads in the B image, changes all the pure black pixels to transparent, then reads in the A image, swaps the images so they're in the right order, then composites the modified B image over the A image and writes the output.
You can add a fuzz value like "-fuzz 5%" ahead of the "-transparent" operation to expand the selection to include near-black pixels, also.
To use with IMv7 change "convert" to "magick".

crop / swap parts of image with magick mogrify

I am trying to crop and swap different parts of a big 800x800 image and re-create 800x800 image using imagemagick with this command.
magick mogrify titli.gif -crop 2x4# +repage -reverse -append -path converted titli.gif
my problem is "-append" creates tall image (400x1600) & "+append" creates wide image (3200x200)
How can I get a large image of original size 800x800 but with cropped and swapped (reversed) parts set in "mosaic or tiled" style...
If I understand the question, you shouldn't need "mogrify" to do that. Just "magick" should accomplish that task.
It looks like you'll have to crop the image into 8 pieces, reverse them, and "-append" them vertically as you've done.
Then after that, and in the same command, you'll need to crop that result in half vertically and "+append" those two pieces horizontally to get the 800x800 output.
This example command shows how it works...
magick in.png -crop 2x4# -reverse -append -crop 1x2# +append out.png
If you're doing any more operations within the same command you'll probably want to use "+repage" after the "+append" to reset the image geometry back to WxH+0+0.

preventing resized images from becoming blurred

im trying to resize image from 72x72 to 512x512 with following command
convert input.png -resize 512x512 output.png
but the output image (output.png) become blur
how to prevent resized images from becoming blurred
how to prevent resized images from becoming blurred
If you want a pixelated image of the original use -sample
# Create small image.
convert -size 72x72 plasma: 72x72.png
# Magnify the image with pixel subsampling.
convert 72x72.png -sample 512 512x512_sample.png
It's true that you can't restore missing data when upscaling images, but there's a lot of various algorithms to calculate what may be missing.
Try using the -filter option in addition to -resize, and checkout the wonderful usage examples here.
Probably the best you can do is use a sharper -filter such as catrom and then do post processing using -unsharp.
convert input.png -filter catrom -resize 512x512 -unsharp 0xSigma output.png
where sigma is the sharpening value, try sigma=1 or 2 (or as desired)
But it will not maintain the same quality as the input as others have mentioned above.
See -unsharp at http://www.imagemagick.org/script/command-line-options.php#unsharp
You can't. For an equal sharpness, you would need more data in the bigger image. Since you have only the data from a small image the result is blurred.
Look at it the other way, if what you asked was possible, instead of compressing the 512x512 image, we would first scale it down to 72x72, compress that (much smaller file) and sent it over with instructions to scale it up to 512x512.

Resources