imageMagic - converting pdf page to image of specific with - imagemagick

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

Related

Effective gravity at high density - ImageMagick

At a high density like 300 dpi other than the default density 72 dpi the following ImageMagick convert command outputs blank pages. It looks strange to me.
"convert -units PixelsPerInch -density 300 $myfiles -page A4 -gravity center test.pdf"
Anyone like to exchange idea?
gravity parameter here isn't effective appropriately
(unable to align the object)
as it can't recognize the position & resolution of A4 page at 300 dpi. This is why the questioned command is outputting blank pages at 300 dpi.
We should avoid using page parameter if density is already defined. ImageMagick has a parameter called extent that can be used to define the resolution of a page at a given density (here 300 dpi). Thus we should use extent instead of page in this case in order to achieve the desired output.
For example,
resolution of A4 size page at 300 dpi is 2480x3508, thus correct command for a set density like 300 dpi shall be:
"convert -units PixelsPerInch -density 300 $myfiles -gravity center -extent 2480x3508 test.pdf"
Here, resolution of extent parameter should be proportionately equal to the set density for any specific page size.

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

ImageMagick: reduce image size

I am using ImageMagick to reduce the image resolution, height and width.
I have noticed a few things. When I am changing resolution at "Image Size" through Photoshop (version 7) from 300dpi to 150dpi image height and width automatically change.
With ImageMagick however I am not getting such variations. For example, if image contains 878 width and 179 height at 300dpi, when changing it to 150 dpi, automatically the image width changing to 439 and height 89 respectively.
Can any one support me how to obtain such changes through ImageMagick.
The dpi setting is not really relevant in most imaging applications/areas, until the point at which you want to print an image.
Do you really need to set it? I mean, if you want to half the size of an image, just use ImageMagick and do:
convert input.jpg -resize 50x50% output.jpg
and ignore the dpi.
To resize the image keeping the rendered size the same, you can use the -resample
option, like so:
$ convert original.jpg -resample 150x150 new.jpg
Using your example, if the original is an 878x179 image at 300DPI,
the result is an 439x90 image at 150DPI:
$ file original.jpg
original.jpg: JPEG image data, JFIF standard 1.01, aspect ratio, density 300x300,
segment length 16, baseline, precision 8, 878x179, frames 3
$ file new.jpg
new.jpg: JPEG image data, JFIF standard 1.01, aspect ratio, density 150x150,
segment length 16, baseline, precision 8, 439x90, frames 3
You can alternatively use the
-density
option along with the
-resize
option to achieve the same effect:
$ convert original.jpg -density 150x150 -resize 50%x50% new.jpg
In summary:
-density
just sets the DPI metadata without changing the underlying image;
-resize
changes the image size without changing the DPI;
-resample
changes the DPI and resizes the image accordingly.
Uses
The DPI metadata is relevant when you need to print an image or convert it to a PDF.
Now you can convert both images to PDF and get files with essentially the same page size:
$ convert original.jpg original.pdf
$ convert new.jpg new.pdf
$ pdfinfo original.pdf | grep -a "Page size:"
Page size: 210.72 x 42.96 pts
$ pdfinfo new.pdf | grep -a "Page size:"
Page size: 210.72 x 43.2 pts

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

Resizing an Image using Imagemagick convert

I have a YUV420 image of size 1280x720. I am trying to resize it to 720x576 using convert (Imagemagick) using below commandline options. But the generated output file doesnot seem to be a proper resized YUV420 image(I want the resized output also to be in YUV420 format):
convert -size 1280x720 -depth 8 -sampling-factor 2x2 test_1280x720_yuv420.yuv -filter lanczos -resize 720x576 -depth 8 -sampling-factor 2x2 720x576_yuv420.yuv //Here the output file size is not what it should be of a 720x576 YUV420 file which is 720x576x1.5 bytes.
Qiestion: What is the format of this output file then?
Also tried -sample option as, but same result. Incorrect sized output file. I even tried to display the generated resized file, but it sure is not a YUV420 file, as could not view it correctly at all.
convert -size 1280x720 -depth 8 -sampling-factor 2x2 test_1280x720_yuv420.yuv -sample 720x576 -depth 8 -sampling-factor 2x2 720x576_yuv420.yuv
Question: Would convert be able to do what I am trying to get done? IF yes, what are the options?
Question: Any other tool(freeware,shareware) which could help me resize YUV files(different formats YUV420, YUV444) to YUV format output files?
Try to ignore aspect ration!
Ignore Aspect Ratio ('!' flag)
If you want you can force "-resize" to ignore the aspect ratio and distort the image so it always generates an image exactly the size specified. This is done by adding the character '!' to the size. Unfortunately this character is also sometimes used for special purposes by various UNIX command line shells. So you may have to escape the character somehow to preserve it.
Example:
convert image.gif -resize 64x64\! resized_image.gif //Resized Image with ignore ratio option

Resources