Cropping with ImageMagick always results in 1x1 images - imagemagick

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.

Related

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.

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 images with ImageMagic produces wrong size images - why?

There is a bunch of images that represent the pages, scanned from a book and already cropped with ImageMagic. They are all uniform, 629x625 pixels. I need to process them further to prepare for creation of a PDF: split them into odd and even pages. They are named canvas-00.png through canvas-53.png
When I use the following command to get the odd pages first, I get completely butchered images 275x563 pixels extracted from a wrong horizontal offset:
convert canvas-??.png -crop 314x625+0+0 ~/Pictures/odd/canvas.png
What am I doing wrong?
PS: Originally the images were larger, contained extra margins with scanner's lid visible, and I successfully cropped them to 629x625 using a very similar command, but of course with non-zero offset of +140+71. So it baffles me that a visibly more trivial command with zero offset does not work at all.
I suspect you have page offsets "remembered" in your PNG files and you need to clear them.
As an example, let's create an image 500x300 pixels:
convert xc:red xc:lime +append \
\( xc:blue xc:magenta +append \) -append -resize 500x300\! start.png
Now let's crop out a piece towards the bottom right corner, which I intentionally made magenta so you can see which piece I got:
convert start.png -crop 200x100+250+150 z.png
But if we now look at it with identify you can see it "remembers" where it was in the original picture:
identify z.png
Output
z.png PNG 200x100 500x300+250+150 16-bit sRGB 51.1KB 0.000u 0:00.000
This will then potentially affect (i.e. mess up) anything you do in future with the image. The solution is to use +repage to reset all the page geometry and layout so it forgets. You can either do this when cropping, or when using the cropped image later - depending whether you want to retain the crop information or not. (Sometimes you do want it when you are doing jigsaw puzzle processing and maybe want to reassemble the pieces later).
convert start.png -crop 200x100+250+150 +repage z.png
identify z.png
z.png PNG 200x100 200x100+0+0 16-bit sRGB 51.1KB 0.000u 0:00.000
In your case, if you already cropped and saved without repaging, you can load your images and repage prior to actual use:
convert canvas-??.png +repage -crop 817x1040+0+0 ~/Pictures/odd/canvas.png

Why does convert png->jpg blow a picture up to the original size?

I have a png whose width is 2551 pixels and whose height is 3578 pixels.
On this png, I use the -crop option of convert to cut out an image whose dimensions are 2362 x 3389 pixels:
convert original_2551x3578.png -crop 2362x3389+94+94 crop_2362x3389.png
This works as intended.
Then, I use convert to create a jpg:
convert crop_2362x3389.png -format jpg -flatten -background white out.jpg
I expected this command to produce a jpg with the same dimension (2362 x 3389). Much to my surprise, the produced jpg has the dimension 2551 x 3578 pixels (which is the same as the original image).
So it seems that somehow the original size is stored along with crop_2362x3389.png.
How can I use convert to convert a png into a jpg and have it keep the dimension of crop_2362x3389.png?
The reason you are seeing this is that when you do your initial crop, the image "remembers" it was part of a larger image and where in that image it used to be.
You can see this if you do your original crop and then run identify and look at the 4th field, just left of 8-bit.
convert original_2551x3578.png -crop 2362x3389+94+94 crop_2362x3389.png
You can also tell ImageMagick to "forget" it by using +repage like this:
# Repage after changing geometry to forget earlier geometry
convert original_2551x3578.png -crop 2362x3389+94+94 +repage crop_2362x3389.png
# Check IM has forgotten image used to be a part of a bigger one
identify crop_2362x3389.png
crop_2362x3389.png PNG 2362x3389 2362x3389+0+0 8-bit sRGB 256c 15.1KB 0.000u 0:00.000
Ok, I've found a solution. The geometry after the -crop parameter must be followed by a !:
convert original_2551x3578.png -crop 2362x3389+94+94! crop_2362x3389.png
This works as intended.

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