How to crop margin in imagemagick? - imagemagick

I have many images (*.pdf) that originally do not have margin. The files are in various sizes but usually from 8000x4000 to 20000x14000 px.
I use this command to convert them to jpeg:
for /r %x in (*.pdf) do convert -density 400 -quality 100% "%x" "%x.jpg"
But it produces big margins. Margins take 30% of the image size. So I need to prevent them or to crop them. I tried to look in IM manual to find how to obtain width and height and to substract some value and to add it to -crop. But this his too hard to me, I am tring to solve it about 1,5 hours without result. And I am new to IM. Can somebody tell me how to solve this and remove the margin?
Also I would like to repair name of file from e.g. map.pdf to map.jpg Now it result in map.pdf.jpg

I used this code to crop the image after conversion to raster:
for /r %x in (*.pdf) do convert -density 400 -shave 1600x1600 -quality 34% "%x" "%x.jpg"

Related

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.

Simple ImageMagick real world usable

I've read the ImageMagick documentation here and here and been unable to achieve a couple of simple tasks. Would appreciate any simple pointers or direction, or even commands I can execute that will work on Linux.
I want to convert any image-type (JPG, GIF, PNG, TIFF) to save to a PNG, losslessly, and as compressed as possible without any loss of quality. Ideally in 96 DPI so they look good in Retina screens.
To then take the above generated PNG and also resize it in specific sizes, with height and width specified.
What am I missing with the convert command?
If you want to convert a TIF, GIF or JPEG to PNG, the command is:
convert image.tif result.png
or
convert image.jpg result.png
In answer to your questions...
Question 1
PNG is lossless by definition, so that is not an issue. To get better compression of a PNG, you generally need to reduce the colours, i.e.
convert image.jpg -colors 64 result.png # or try 255 which allows a palettised image
The dpi is pretty irrelevant until you print on paper. More pixels are needed if you want more quality on screen.
Question 2
If you want to resize an image without destroying its aspect ratio, use
convert image.jpg -resize 200x100 result.png # retain aspect ratio
If you don't care if that makes the image look stretched or distorted, and you want exactly 200x100, tell ImageMagick that you really mean it by shouting:
convert image.jpg -resize 200x100! result.png # ignore aspect ratio

How to manage image placement when converting to pdf with imagemagick?

I have a bunch of png's ripped from github, that look kind of like this:
All have a width of 912, but the heights vary.
I'm trying to convert them all to a nice readable A4 pdf, using:
convert -page A4 *.png -format pdf file.pdf
I can get them all into the pdf, but can't get the results quite right:
It puts them on the bottom instead of the top of the page.
Also, most of them fit on one page, but for the ones with lots of pixels going down, I'd like to split and crop it so it spans multiple pages while keeping a consistent size
How do I get them on the top of the page (centered, or left-justified with a bit of margin), and bonus points for splitting them?
ImageMagick adds pictures to a PDF at the bottom left corner. It is possible to set an offset to push it up to the top of the page. Here is your code modified to work with your example png:
convert -page A4+0+593 example.png -format pdf file.pdf
However, the vertical offset (593px here) needs to be in relation to the height of each png. You can find the height of the file (using identify xyz.png) and subtract it from 842 (the height of the A4 PDF). That will give you the necessary vertical offset for each file.
The complication for you is that your pngs are 912px wide and the A4 pdf is only 595px wide, so your png is being proportionally reduced in order to fit. You can use ImageMagick to reduce the image proportionately to 595px wide (using convert example.png -resize 595x595 reducedExample.png). Then find the new height of the reduced png, subtract that from 842, and use that number as the offset.
This helps you solve your second problem. Once you've established the height of your reduced png, you'll know if it is too tall for a page. Anything taller than 842px (or less if you include margins) will need to be chopped to fit the page using the -chop operator (see ImageMagick Usage manual for details).
Instead of calculating you could use the -gravity parameter. I would suggest "-gravity north" to stick the images to the top margin or "-gravity center" to center them. Results vary depending on if the image fits properly into the page. If they are all about the same size it should work.
So if you are in the folder where all your png:s are i would do the following:
convert -gravity north -page A4 *.png -format pdf output.pdf

How to deal with padding in convert?

I am using convert from PDF to PNG.
$ convert -density 203.294113 -resize 6000x3300\> src.pdf[0] dst.png
But when the image is created, it has a white margin added around the origin pdf page and I don't know how to eliminate it (it doesn't matter whether "density" and "resize" are used). Thanks a lot for the help.
you can use -shave to remove border with specific size or -trim to auto-trim image

How can I reduce the dimensions of gif images with ImageMagick?

What flag in magick.exe convert <flags> will reduce the dimensions of my image?
This is the command I'm using: magick.exe input.png[0] -depth 8 -type Grayscale -dresize 400x300.
The [0] after the source-image filename is meant to strip the first frame of any animated gifs.
How can I do reduce my output gif sizes? My file sizes are too large: my outputs need to be less than 100k. Any methods other than reducing the dimensions are also welcome.
convert test.gif -fuzz 10% -layers Optimize result.gif
Adding a -fuzz 2% produced a better optimization, but still not very good. At -fuzz 15% It isolated the differences for frame optimization to just the visible color band changes I noted before.
At 25% the differences were almost to just the text changes.
Finally at a massive 30% fuzz factor (ignore color changes below that figure, did it optimize to just the text changes.
You can try gifsicle:
gifsicle -O3 old.gif -o new.gif
If it's an animation, you could try skipping frames (see how it works).
magick.exe convert -resize 100x100 .\step1.jpg .\step2.jpg -delay 100 -loop 0 animation.gif
Thanks to Nate.
//IMPORTANT: -resize should come first.
//BY DEFAULT: the aspect ratio will be locked with the longest dimension being set to 100px.
//GENERALIZATION: this order `magick convert 1st<input_file(s)> 2nd<switch(es)> 3rd<output_file>`.
//IMPORTANT-RELATED: `-delay` will not work on second brush! Use `-set delay` for existing files.
Please, also be sure to refresh your folder view to ensure that you're not viewing old output files: Windows Explorer (F5).
Try to use the option:
-type Palette
It might help to reduce your gif file sizes smaller, oh and I believe -depth 8 can only be used for png images.

Resources