ImageMagick: convert image to PDF with A4 page size and image fit to page - imagemagick

I want to convert different image formats (bmp,jpg,gif,png,tiff-incluging multipaged) into a PDF format with A4 page size and with images fit to page (resized if necessary). Image should be positioned at the center of the page and I'd like to define an offset.
I tried the code below but there is no offset at the top and the image quality is really poor.
convert png.png -gravity North -resize 500x500 -quality 100 -page a4x5x5 myout.pdf
Is there any way to do that?
Thanks in advance for any help,
Mariusz

If you want to keep the original resolution (lossless) you can try the following command:
convert png.png -background white -page a4 myoutput.pdf
Based on a comment posted before: https://stackoverflow.com/a/24573341/6747994
#m4tx This command only makes sense if the picture has a resolution above 500x800px, it does not zoom in, to avoid pixelated thumbnails.

You can convert to pdf using ImageMagick
convert png.png myout.pdf
but use pdfjam instead of ImageMagick to adjust the page size
pdfjam --paper a4paper --outfile myoutA4.pdf myout.pdf
pdfjam offers other options, which may fit your needs.

Found this somewhere on stackoverflow:
convert *.jpg -resize 1240x1753 \
-extent 1240x1753 -gravity center \
-units PixelsPerInch -density 150x150 multipage.pdf

Thanks to the ImageMagick support forum I was able to find a solution:
convert image.tif -resize 575x823^> -gravity center -background white -extent 595x842 image.pdf
If you get an error try:
convert image.tif -resize 595x842^\> -gravity center -background white -extent 595x842 image.pdf

You need to specify the density:
convert -density 80 -page a4 input_A.jpg input_B.jpg output.pdf
Otherwise, there can be an issue when printing with "actual size" instead of "fit". I have used a public printer where I could not choose "fit" and had to choose "actual size", in which case the printed page was only a part of the page which I wanted to print.

Related

Can I refer to the image width and height in an argument?

I want to convert multiple images into square format, filling any empty space with black. Assuming the images have a width of 1000 and a height of less than 1000 (or vice versa), I can do it like this (using PowerShell):
magick.exe convert -background black -gravity center `
-resize 1000x1000 -extent 1000x1000 `
-set filename:original '%t' '.\*.jpg' './%[filename:original]-resized.jpg'
However, I want this to work for arbitrarily sized images. I need to do something like this, which is not an actually supported syntax:
#...
-resize 'max(%w,%h)xmax(%w,%h)' -extent 'max(%w,%h)xmax(%w,%h)' `
# ...
Is there an Imagemagick syntax for what I'm trying to do?
In Imagemagick 7, use magick, not magick convert and move the input parameter to the first parameter position, i.e. magick '.\*.jpg'.
Then, to do what you want change
-resize 'max(%w,%h)xmax(%w,%h)' -extent 'max(%w,%h)xmax(%w,%h)'
to
-resize "%[fx:max(w,h)]x%[fx:max(w,h)]" -extent "%[fx:max(w,h)]x%[fx:max(w,h)]"

imagemagick trim but keep canvas size and *position*

Friends,
I need to -trim some images but keep the original canvas size. Works like this:
convert in.png -fuzz 10% -trim -background white -set page "%[fx:w]x%[fx:h]" +repage out.png
But how can I position the trimmed image part at it's original position? -gravitiy center is not an option as the to-be-trimmed part usually not at the canvas center.
Any ideas?
You should be able to -trim an image, then use -flatten to lay it back onto its original canvas. Try this command...
convert logo: -background none -trim -flatten trimmed.png
#GeeMack's answer is certainly simpler and more succinct, but if you need more flexibility for dinking around, another way is to get the image height and width and the trimbox in one invocation and use them in the next - maybe with adaptation.
So, starting with this image:
# Get image width and height and the trim-box
read geom trim < <(magick start.png -format "%G %#" info:)
# Make a new white canvas same size as original and trim new image onto it
magick -size $geom xc:white \( start.png -crop $trim \) -flatten result.png
I put an artificial yellow border around it so you can see the extent of it on SO's white background.

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

There is an option in ImageMagick that automatically cut out the written part of a page?

I use ImageMagick (with Windows 10) to convert my .pdf in .png, for example:
convert -background white -alpha remove -density 150 "mydoc.pdf" -quality 100 "mydoc.png"
I'd like to know if there is an option that automatically (that is without explicitly giving the size) cut out the written part of a page.
For example, from an image like this:
get an image like this:
What you are looking for is the -trim option:
http://www.imagemagick.org/script/command-line-options.php#trim
Simply add it to your command. You might also add the +repage option to reset the canvas offset:
convert -background white -alpha remove -density 150 -trim +repage "mydoc.pdf" -quality 100 "mydoc.png"

Merge Images Side by Side (Horizontally)

I have five images of sizes: 600x30, 600x30, 600x30, 600x30, 810x30. Their names are: 0.png, 1.png, 2.png, 3.png, 4.png, respectively.
How do I merge them Horizontally to make an image of size 3210x30 with ImageMagick?
ImageMagick ships with the montage utility. Montage will append each image side-by-side allowing you to adjust spacing between each image (-geometry), and the general layout (-tile).
montage [0-4].png -tile 5x1 -geometry +0+0 out.png
Other examples can be found on Montage Usage page
ImageMagick has command line tool named 'convert' to merge images horizontally, or for other purpose. i have tried this command and working perfectly on your case:
To join images horizontally:
convert +append *.png out.png
To stack images vertically:
convert -append *.png out.png
Use -resize if the images don't have the same width/height
You can fix the height for all of them with the -resize option, e.g. to fix a 500 pixel height on two images joined horizontally:
convert +append image_1.png image_2.png -resize x500 new_image_conbined.png
Or for vertical joins, you would want to set a fixed width instead:
convert -append image_1.png image_2.png -resize 500x new_image_conbined.png
Example:
image_1.png 1067x600
image_2.png 1920x1080
new_image_conbined.png 889x500
Related:
https://askubuntu.com/questions/226054/how-do-i-join-two-images-together
https://superuser.com/questions/290656/combine-multiple-images-using-imagemagick
How to do it interactively with GIMP
If you need to crop/resize images interactively first, which is often the case, then GIMP is the perfect tool for it, here's a detailed step-by-step: https://graphicdesign.stackexchange.com/questions/83446/gimp-how-to-combine-two-images-side-by-side/145543#145543
SVGs
ImageMagick 6.9.11-60 doesn't handle them, so see:
How to concatenate SVG files lengthwise from linux command line?
https://graphicdesign.stackexchange.com/questions/137096/is-there-a-way-to-stack-two-svgs-on-top-of-each-other
https://graphicdesign.stackexchange.com/questions/90844/joining-together-multiple-svg-images
Very simple with ImageMagick (brew install imagemagick )
convert +append image_1.png image_2.png new_image_conbined.png
Anyone using the MiniMagick rails gem can use the built-in tool to merge images:
# Replace this with the path to the images you want to combine
images = [
"image1.jpg",
"image2.jpg"
]
processed_image = MiniMagick::Tool::Montage.new do |image|
image.geometry "x700+0+0"
image.tile "#{images.size}x1"
images.each {|i| image << i}
image << "output.jpg"
end
Check out the documentation for #geometry options to handle resizing and placement. The current example will resize images to a 700px height while maintaining the image's aspect ratio. +0+0 will place the image with no gaps between them.
The convert +append method described in other answers appends images horizontally, aligned to the top. If you prefer to align to the bottom or center, try:
convert input1.png input2.png -gravity South +append output.png
or
convert input1.png input2.png -gravity Center +append output.png
Source: Fred's ImageMagick Tidbits http://www.fmwconcepts.com/imagemagick/tidbits/image.php#append

Resources