How to manage image placement when converting to pdf with imagemagick? - 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

Related

imageMagic - converting pdf page to image of specific with

I am using the following command to convert the first page of a pdf to a png:
convert -density 200 "somePdf.pdf[0]" -size 500 "newImage.png"
But the output image size is not 500 pixels wide (it comes to over a 1000 pixels wide).
How do I define a specific output size?
You just use -density. In your Imagemagick command, don't use -size, and don't use -geometry. The density is what controls the output. Default PDF will use 72 dpi. So that is natural size for the dimensions converted from inches. If you want a specific size, make the density larger than needed and then use -resize.
convert -density 288 "somePdf.pdf[0]" -resize 500x "newImage.png"
You don't use size. You use geometry:
convert -density 200 "somePdf.pdf[0]" -geometry 500 "newImage.png"
This will specify the output width. You can also use -geometry 500x1000 to specify a specific width and height.
PDF does not concern itself with density or resolution just width and height. So to extract a page as image you say extract 594 wide page as 594 points 210 mm or 8.25 inches and its as perfect as the source ratios. ImageMagick does not convert PDF it passes the command on and its easier to use GS and several methods including GS support answer https://stackoverflow.com/a/10024458/10802527
If you wish to modify the returned pdf output as a different image you use IM convert to suite your taste but its not as accurate or clear as using pdftopng or better extract scanned pages as source dimensions then rescale

Imagemagick : zoom picture until fits DIN A4 dimensions

I need to process some pictures with imagemagick. Those pictures have different proportions and sizes. My goal is to 'zoom' every picture so at the end i'll have a whole DIN-A4 printable surface. Which could be the best approach ? I'm thinking of doing something like this:
A : I would expand original picture to fit 'DIN A4' height.
B : I would expand A picture to fit 'DIN A-4' width.
C : Final picture has the same aspect ratio and was 'zoomed'
A real example. Pay attention how the butterfly's image is bigger ( zoomed ) in the second one. ( using 'Canon Image Garden software' , print > option without borders' ):
LEFT IMAGE Original file.
CENTER IMAGE: how it would look printed covering whole a DIN A4 sheet.
RIGHT IMAGE: This is how we'll see the original image printed
I have no idea how could i do this using imagemagick. Any suggestions ?
To resize an image to A4 size (595 x 842) in ImageMagick, use
convert image -resize 595x842 -background white -gravity center -extent 595x843 result

Put image on top of other image, but resize to fit first

I have two pngs. One is of unknown size (but always square), the 2nd is 1024x1024 and mostly transparent. I want to put the 2nd on top of the first, but first scale it down to the size of the first.
E.g. image1.png is 100x100, overlay.png is 1024x1024. The resulting image size is 100x100 with the overlay scaled down to 100x100 and put on top of the source file.
So far I got this:
magick convert ~/Downloads/Test\ icon.png res/drawable/icon.png -gravity center -composite ~/result.png
But the resulting image is 1024x1024 and the original is tiny somewhere in the center.
This will read in both images, resize the second to fit within the dimensions of the first, then composite the second centered over the first.
magick img1.png img2.png \
-resize %[fx:u.w]x%[fx:u.h] -gravity center -composite output.png
If used in Windows, that continued line backslash "\" should be changed to a caret "^". If used in a Windows BAT script, the single percent signs "%" need to be doubles "%%".
EDITED TO ADD: The way that works is this... Two images are read into the command. The FX expressions "u.w" and "u.h" stand for the width and height of the first image. So to "-resize" first image to its own dimensions doesn't change it, of course. And the second gets resized to fit within the dimensions of the first.

ImageMagick Crop Tall image into A4 PDF pages with same width dynamic height

how can we make a tall-dynamicheight-screenshot.png into onefile.pdf with multiple pages in A4 so it could be readable when its printed?
example like this :
into A4 pages like
so when we print it would be like
not like (unreadable)
This is maybe good enough for a crude first approach - it doesn't look for good places (white space) to divide the pages along though, so it slices through the image at the bottom of your first page:
convert article.jpg -resize 2480x -crop 2480x3508 +repage result.pdf
In imagemagick you can crop to multiple pages. But it depends upon whether you want to keep your original size or resize the width to correspond to A4. The A4 file dimensions are 595x842.
If you want to keep the original dimensions and create a multipage PDF, then you will get 12 pages at your original resolution
convert V0twr.jpg -crop 595x842 +repage onefile1.pdf
If you want to resize to the width of an A4 and then crop, then you will get 2 pages at reduced resolution.
convert V0twr.jpg -resize 595x -crop 595x842 +repage onefile2.pdf
You can choose some other in-between resolution for your resize, if you want as a compromise, say twice the width of A4 = 2*595 = 1180

How to crop margin in 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"

Resources