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

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

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

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.

Converion of pdf page to fixed size images

I want to convert pdf pages to images of same size(aspect ratio maintained) irrespective of the pdf quality. Say A4 size. I am using convert from Imagemagick for this but struggling to get the fixed size of images for different pdfs of different quality.
Can anyone help me?
Something like:
convert -density 288 document.pdf -resize 2480x3508 result.png
The density must come before the PDF to ensure the rasterisation is done at sufficient quality/resolution.
If you want that centred on a piece of A4 and padded with white to fill the page:
convert -density 288 document.pdf -resize 2480x3508 -background white -gravity center -extent 2480x3508 result.png

How to crop to at least 16:9 with imagemagick

I have images of unknown size and ratios. I wish to take a centred 16:9 crop.
If all the images were known 4:3, then it's easy: 9/16 รท 3/4 = 0.75 so I simply set the height to 75% of the original like this:
convert photo.jpg -gravity center -crop '100%x75%' +repage photo16x9.jpg
However, if the photo is already 16:9 (or even wider), I don't want to crop it, and if it is 'taller' than 16:9 I want to crop it only by the amount necessary to achieve a 16:9 crop.
This would be also easier if I wanted to scale to a known width or height (example question for that). But I'm looking to leave as much of the original image data in place as poss.
Therefore I'm looking for a way to crop the height to a factor of the image's width, with a cut-off.
I hoped this might work
convert photo.jpg -gravity center \
-crop '100%x[fx:9/16*w]' +repage photo16x9.jpg
but alas it seems the [fx:...] is not allowed in a -crop argument.
Hacking a bit I found somewhere that I could not for the life of me understand(!) also failed, but I'll list it here to show research effort!
convert photo.jpg -set option:distort:viewport \
'[fx: w]x[fx: w*9/16 ]+0+0' -filter point \
-distort SRT 0 +repage photo16x9.jpg
(I realise that neither attempts above cover the case when the image is already wider than 16:9, but if the [fx:...] thing worked I could achieve that by using the ternary operator, so I kept the examples simple.)
Maybe you can just calculate the aspect ratio and use that to make a decision. Create two test images, one 15x9 and one 17x9:
convert -size 15x9 xc:black 15x9.png
convert -size 17x9 xc:black 17x9.png
Now ask ImageMagick to tell you if the aspect ratio is wider than 16:9 or less than or equal to 16:9:
convert 15x9.png -format "%[fx:(w/h)>16/9]" info:
0
convert 17x9.png -format "%[fx:(w/h)>16/9]" info:
1

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

Resources