ImageMagick: How do I do a simple crop? - imagemagick

All I'm trying to do is crop images to a width of 1400 pixels from the left, but leave them at the same height.
The format should be width: 1400px, height 100% (unchanged), offset X and Y are both zero, so +0+0 (for X and Y):
convert -crop 1400x100%+0+0 inputfile outputfile
This doesn't actually do anything! Can someone help me with my syntax?

Your syntax for ImageMagick is not correct. It may work in ImageMagick 6, which is forgiving, but not in ImageMagick 7. Read the input before any operations.
What you need to do is just specify Wx+0+0, without including H. That will tell ImageMagick, that you just want the width cropped. For example:
Lena:
convert lena.png -crop 100x+0+0 +repage result.png
Include +repage for those formats that support virtual canvas, such as PNG and TIFF. JPG does not need it, but it won't hurt.
See https://imagemagick.org/Usage/crop/#crop_percent

I figured it out. The 100% isn't necessary. It should be:
convert -crop 1400x+0+0 inputfile outputfile

Related

ImageMagick crop keeping the height

I'm trying to crop image by height with this command line:
convert 1053257.png -gravity South -crop 2910x3312+0+0 -background black +repage image-cropped-top.png
The generated image is not cropped correctly, as the dimensions after running the command are 2791 x 3312.
The width is cropped as well!
Can anyone help with this?
The general form is:
convert input.jpg -crop WIDTHxHEIGHT+0+0 result.jpg
If you want to crop to a specific width, say 1024, leaving the height unaffected:
convert image.jpg -crop 1024x+0+0 result.jpg
If you want to crop to a specific height, say 768, leaving the width unaffected - note the height is after the x:
convert image.jpg -crop x768+0+0 result.jpg
If you want to crop to a maximum width and height, say 1024 wide by 768 tall without distorting the aspect ratio:
convert image.jpg -crop 1024x768+0+0 result.jpg
If you want to crop to a specific width and height, say 1024 wide by 768 tall and are happy to allow gross distortions:
convert image.jpg -crop 1024x768+0+0\! result.jpg
Think of the exclamation mark as meaning "just do it!". Note that the backslash is only needed on Linux/Unix/macOS to escape the exclamation mark, you omit the backslash on Windows.
Note, if you are saving the cropped image in PNG format, you probably want to reset the page afterwards so the image "forgets" it used to be part of a larger image:
convert input.jpg -crop 1024x768+0+0 +repage result.png
With ImageMagick a problem like that can occur if you've done a "-trim" to the image before the crop. When you "-trim" an image it can still remember the original page dimensions from before the trim, then when you crop it, it uses those page dimensions as the starting reference for the crop. You probably need to "+repage" before the crop to start with fresh paging information. Try something like this...
convert 1053257.png -gravity South +repage -crop 2910x3312+0+0 +repage image-cropped-top.png

Generating Low Resolution Black and White Text with Image Magick

I need to generate low resolution black and white images of texts in ImageMagick. These images will be plotted in a small LED matrix. The text need to have 7 pixels of height.
For now, I'm using:
convert -negate -threshold 15% -font Courier -size 80x11 caption:'hello' out.bmp
Output image:
Even with the height being more than I need, due to low resolution and anti-aliasing correction, the letters are not pretty and symmetric. Has anyone did this and can help me out?
Version: ImageMagick 6.8.9-9 Q16 x86_64
The solution I found was to use an TrueType font. Just got a free font from the internet and used it in the size it was built for.
P.S.: Switched for OpenCV as well. My Python app generates images dynamically. The cost for invoking ImageMagick several times(could get close to hundred) per minute is too high.
Posting a snippet, hope it helps.
import cv2 as cv
from PIL import ImageFont, ImageDraw, Image
# Creates a black image with 80x10 size
img = Image.new('RGBA', (80, 10), (0,0,0,0))
draw = ImageDraw.Draw(img)
# Load TrueType font of height size 8
font = ImageFont.truetype("font.ttf", 8)
# Draw text using the loaded font
draw.text((0, 0), "Hello World!", font=font)
img.save("out.bmp")
Output Image:
I would be inclined to output the letters larger than required, then to trim any extraneous spare space so as to make the most of the available resolution, then resize down to your specific needs:
convert -size 320x32 -font Courier label:'hello' -trim +repage -resize 80x8 +write out.gif
Mark, I think he wants a binary result. But you have an excellent idea.
Let's take Mark's result, threshold and then scaling down to 8 pixels tall. This ImageMagick command seems to work better than my earlier post.
Mark's Output:
convert wcwuj.gif -threshold 60% +write thresh.gif -scale x8 result.gif
Threshold Result:
Scaled Result:
Perhaps making Mark's image much larger and choosing a better threshold will produce a better result.
You have not told us what version of ImageMagick nor platform and you do not show your result for us to see what might be wrong. Also your ImageMagick syntax is not proper, though ImageMagick 6 is rather forgiving.
This is what I get using ImageMagick 6.9.10.8 Q16 Mac OSX Sierra. The first output is 8 pixels tall and the second output is scaled by 1000% (10x).
This forum does not seem to convert bmp to a usable format for display, so I am using GIF in place of BMP. But my results look the same whether BMP or GIF
convert -size x8 -font Courier label:'hello' -negate -threshold 20% +write out.gif -scale 1000% out2.gif
I have tried changing threshold, but much larger or smaller values make it worse. A range from about 10-30% produces the same results.
I have also tried using -monochrome in place of -threshold and get the following:
convert -size x8 -font Courier label:'hello' -negate -monochrome +write out3.gif -scale 1000% out4.gif
You might try a dot-matrix type font. See https://www.1001fonts.com/digital+dot-matrix-fonts.html?page=1&items=10. I have not tried any of them.
You could try some of the old X11 fonts. These were hand-drawn rather than being rendered from a set of curves, so they look good at very small sizes.
For example, if I run xfontsel I get things like this (enlarged for clarity):
Take a look in /usr/share/fonts/X11/misc.

How to crop image without knowing the height of image using ImageMagick

I have 100s of screenshots taken from website of varying height. I would like to crop header of the images by 50px from the top irrespective of the height of the image.
convert -crop autoxauto+0x50 image-input.png image-output.png
If you start with this image where the two bars are both 50 pixels tall:
You probably mean:
convert start.png -crop x50+0+0 top.png
Or:
convert start.png -crop x50+0+50 bottom.png
I agree with #Mark's answer, however I found a simpler solution:
convert image-input.png -chop 0x50 image-output.png

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.

Resize image by length with image magick

I'm trying to resize a image by its length, the width should remain. Is sounds quite easy, but somehow i don't get it. I'm using it together with PHP via the command line.
I'm using ImageMagick 6.7.3-9
By reading the docs, i would do the following:
/usr/local/bin/mogrify -resize x200! myimg.png
That gives me:
mogrify: negative or zero image size `bbb.png' # error/resize.c/ResizeImage/2570.
My goal is, that if bbb.png is w:400 h:400 i can resize it to w:400 h:200.
mogrify -resize x200 //Height with new width
mogrify -resize 200 //Width with new height
mogrify -resize 200! //New Width, old height
Can someone explain me, what I am doing wrong? Thanks
Known broken, now fixed. Fix should be in ImageMagick-6.7.4-0 Beta available 2011-12-08.
I have not found your Image Geometry fromat -resize x200! in documentation. I think you should use
xheight
or
widthxheight!
I.e.: -resize x200 or -resize 400x200!.

Resources