Add overlap or duplicate pixels on texture bounds after cropping in imagemagick - imagemagick

I have huge texture Nx1024 pixels. I split it into several 1024x1024 textures with the following command with Imagemagick:
magick image.png -quality 100 -background none -resize x1024 -crop 1024x1024 -extent 1024x1024 output%02d.png
How can I add 2 pixels overlap on left and right sides of output images or duplicate pixels with imagemagick?
PS:
I found the following case (-crop 3x1+2#) crop with equal size but it doesn't suit because I have an unknown width of the input image (it could be multiple of N for 1024)

If on Unix-based OS, then you could try my script, overlapcrop at http://www.fmwconcepts.com/imagemagick/index.php

Related

convert doesn't conserve size when scaling down and up

In order to make applying blur faster I'm first scaling my image down and then scale it back up:
convert - -scale 10% -blur 0x2.5 -resize 1000% RGB:-
This works most of the time but sometimes the output resolution is slightly different from the original input. Is there a way to force the pipeline to be size-preserving?
You should be able to access the original geometry of the image via %G, so you can do:
convert input.jpg -scale 10% -blur 0x2.5 -resize '%G!' RGB:-
If you are using Windows, you probably want "%G!" in double rather than single quotes.
If you are using v7 ImageMagick, replace convert with magick.
I think you are getting errors because if you take 10% of 72 pixels (say), you will get a whole number of pixels, i.e. 7 pixels and then when you scale back up by a factor of 10 you'll get 70 rather than your initial 72.
If you are using Imagemagick 7, you can do the following:
magick input.jpg -set option:wd "%w" -set option:ht "%h" -scale "%[fx:wd/10]x%[fx:ht/10]" -blur 0x2.5 -resize "%[fx:wd]x%[fx:ht]\!" RGB:-
This stores the input width and height. Then uses the stored values to scale by 1/10 of those dimensions, then does the blur, then resizes exactly back to the origin input size. Note the ! that forces the resize to the exact dimensions.
or simpler without storing the input width and height:
magick lena.jpg -scale "%[fx:w/10]x%[fx:h/10]" -blur 0x2.5 -resize "%[fx:w]x%[fx:h]\!" lena_x.jpg

crop image with imagemagick offset given in percentage

imagemagick's crop command supports cropping to a percentage of an image but the offset values must be specified in pixel values, e.g.:
convert image.png -crop 50%x+10+20
I want to crop with offset values x and y given in percentage of the image width, and height respectively. The pixel values can be calculated, for instance if the image size is 100x200 an offset of 10% would result in 10 and 20 respectively. Is it possible to do this calculation as part of the call to convert? Width and height are available as %w and %h at some places, but this does not work:
convert image.png -crop 50%x+(0.1*%w)+(0.1*%h)
If you're running IM v6 you can use FX expressions with "-set" to set image attributes. By setting the page geometry you can specify the offsets to a calculated percentage and do the crop like this...
convert image.png -set page -%[fx:w*0.1]-%[fx:h*0.1] -crop 50%x+0+0 result.png
That reads the image, sets the geometry for the upper left corner to a location outside the original canvas, and crops to the new top left corner specified by the geometry.
Note the offsets are negative numbers.
Also, if you're doing additional processing in the same command you'll probably want to "+repage" after the crop in order to reset the page geometry to the new WxH+0+0.
Edited to add: You can even include the width and height dimensions for the crop when using "-set page". This command would crop an output of 50% the input width and height, and starting at 10% in from the left and top...
convert image.png \
-set page %[fx:w*0.5]x%[fx:h*0.5]-%[fx:w*0.1]-%[fx:h*0.1] -crop +0+0 result.png
Notice how the crop operation is simply "-crop +0+0" since the dimensions and offsets are in the page geometry.
This method lets you use more complex calculations than just using a percent or number of pixels for the cropped output dimensions.
You cannot do that in ImageMagick 6. But you can do that in ImageMagick 7.
magick image.png -crop "50%x+%[fx:0.1*w]+%[fx:0.1*h]" +repage result.png
In ImageMagick 6, you need to do the computations ahead of the command, store them in a variable and use the variable in the crop command.
However, in ImageMagick 6, you can do the equivalent using -distort with viewport processing as follows:
convert image.png -set option:distort:viewport "%[fx:0.5*w]x%[fx:0.5*h]+%[fx:0.1*w]+%[fx:0.1*h]" -filter point -distort SRT 0 result.png
With v7 ImageMagick, make start image:
magick -size 200x100 gradient: a.jpg
Now crop using lots of calculated widths, heights, offsets:
magick a.jpg -crop "%[fx:w*0.9]x%[fx:h*0.8]+%[fx:w*0.1]+%[fx:h*0.05]" b.png
Check:
identify b.png
b.png PNG 180x80 200x100+20+5 8-bit Gray 256c 408B 0.000u 0:00.000
If you only have v6, use bash and integer arithmetic:
read w h < <(identify -format "%w %h" a.jpg)
convert a.jpg -crop $((w*80/100))x$((h*90/100))+$((w*10/100))+$((h*5/100)) result.png
Check:
identify result.png
result.png PNG 160x90 200x100+20+5 8-bit Gray 256c 412B 0.000u 0:00.000

ImageMagick convert format, crop and resize

I have a large number of research pdf figures, and I need to preform the following actions in ImageMagick:
convert all pdf to png
crop png from left/top corner x:334/y:244; from right/bottom corner x:214/y:340;
original size 2100x2100, cropped size 1552x1552 pixels
resize cropped png to 240x240 pixels
Here is how it should be cropped for point 2, the pink area is what I want to have:
I was only be able to get 1st action done with my knowledge:
mogrify -format png -density 300 -flatten *.pdf
How can I do the 2nd and 3rd actions please? And do I need to run three separated commands or could they be combined into one command?
I do not know what exact order you need for mogrify as I do not use it. I also do not know why you need flatten Try:
mogrify -format png -density 300 -crop 1552x1552+344+244 +repage -resize 240x240 *.pdf

Cropping with ImageMagick always results in 1x1 images

I'm trying to crop an image into a bunch of 256x256 tiles (with the right-most and bottom-most images less than 256 pixels due to the remainders), but ImageMagick always generates images that are 1x1.
I use this command (Windows 7 command prompt):
convert WBS.png -crop 256x256 +repage +adjoin output\WBS_%02d.jpg
After cropping the following message is displayed:
convert: geometry does not contain image `WBS.png' # warning/transform.c/CropImage/589.
After cropping, the output folder contains 1634 jpg files, all of which are 1 x 1 pixel. The source image is 7218x7650.
Suggestions? I'm sure I'm making some blatant mistake, but I don't know what it is.
This can happen if the origin of the image is not at 0,0. In that case, using +repage before processing the image should do the trick, i.e.
convert WBS.png +repage -crop 256x256 +repage +adjoin output\WBS_%02d.jpg
See also the documentation of the -crop option.

ImageMagick cropping large image into xyz tiles

i'm having a large jpg, which has a resolution of x * 256 / x * 256. I want to cut this image into 256x256 tiles with a naming convention {zoom}-{x}-{y}.jpg. In the past i've used ZoomifyExpress Converter which does the cutting and zooming. I also want 6 different zoom levels. I've started so far with this command:
convert example.jpg -crop 256x256 +gravity -set filename:tile
./tiles/%[fx:page.x/256]-%[fx:page.y/256] %[filename:tile].jpg
This produces a lot of x-y.jpg tiles.I don't know how i can add different zoomlevels. I'm relativly new to ImageMagick and this feels like a basic thing to do. Hopefully somebody can help me out. Thanks in advance.
I found the solution:
I just resize the image to the appropriate size and then crop it. The first number in the filename is the zoom level.
convert example.jpg -resize 256x256 -crop 256x256 -set filename:tile
./tiles/0-%[fx:page.x/256]-%[fx:page.y/256] %[filename:tile].jpg
convert example.jpg -resize 512x512 -crop 256x256 -set filename:tile
./tiles/1-%[fx:page.x/(256)]-%[fx:page.y/(256)] %[filename:tile].jpg
.. and so on until i reach the highest resolution.

Resources