Imagemagick : zoom picture until fits DIN A4 dimensions - imagemagick

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

Related

Splitting a region of an image into tiles

I have a set of images that are 4500px x 3000px in size.
Out of each of these images I need to extract a set of tiles in a 5 columns by 3 rows combination with each tile being 700px wide by 500px high.
What complicates the above is that the starting point for this needs to be at X:650 and Y:450 position from the top left corner of the image.
I have gotten as far as convert image.jpg -gravity NorthWest -chop 650x450 -crop 700x500 tile-%d.jpg
This gets me to a correct start position for creating the tile set but includes the rest of the blank area in the right and bottom edge of the image.
How do I go about solving this?
Within a single ImageMagick command you can crop the initial rectangle that contains all the tiles first, then crop that into the 15 output images. Try something like this...
convert image.jpg -crop 3500x1500+650+450 -crop 5x3# tile-%02d.jpg

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 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

GraphicsMagick crop: shows what will be cropped

I'm working on a photo application and I need some advice how should I solve the following with Graphics/ImageMagick.
Given a photo with resolution: 2048x1536
Given a specified resolution: 1864x1228
Resize the image and fill the specified resolution with the image (now it's 1864x1398)
Highlight the area of the original image will be cropped (to 1864x1228)
I have a working solution which resizes and crops the image properly:
IMOperation resizeOp = new IMOperation();
resizeOp.addImage();
resizeOp.resize(MAX_WIDTH, MAX_HEIGHT, "^");
resizeOp.gravity("center"); //centered image with crop the top/bottom parts
resizeOp.crop(MAX_WIDTH, MAX_HEIGHT, 0, 0);
resizeOp.addImage();
ConvertCmd cmd = new ConvertCmd(true);
cmd.run(resizeOp, fileName, outputFileName); //cropped, center filled image (1864x1228)
The question is how should I do the following: show the full image instead of the cropped version and highlight the area of the image will be cropped. I'd prefer with red border around the cropped image and show with the cropped parts with alpha layer.
I have an idea which I don't like very much: generate an image from the original with alpha layer and put the cropped image on it with red border. It doesn't seem to be the optimal solution :) My other idea is to do this with javafx imageviews, but it seems suboptimal as well.
Notes:
I'm using im4java with GM. I accept a command line solution too (and I'll figure out and post it in im4java ;)
We can restrict the conversation about horizontal images only, I can figure out the vertical operations
Any comments would be highly appreciated.
The oneliner imagemagick convert (remove the line breaks):
convert ( in.jpg -resize 1864x1228^ -fill white -colorize 50% )
( in.jpg -resize 1864x1228^ -gravity center -crop 1864x1228+0+0 )
-gravity center -composite out.jpg
In bash, you have to escape the () characters with \!
I solved it in im4java with sub operations:
IMOperation op = new IMOperation();
op.openOperation();
op.addImage(); //input image
op.resize(MAX_WIDTH, MAX_HEIGHT, "^");
op.fill("white");
op.colorize(50);
op.closeOperation();
op.openOperation();
op.addImage(); //input image
op.resize(MAX_WIDTH, MAX_HEIGHT, "^");
op.gravity(GRAVITY_OPT_CENTER); //centered image with crop the top/bottom parts
op.crop(MAX_WIDTH, MAX_HEIGHT, 0, 0);
op.closeOperation();
op.gravity(GRAVITY_OPT_CENTER);
op.composite();
op.addImage(); // output image
Notes:
It still doesn't contain red border (I can't add border only to the second image).
I decided to use "fade to white" effect, instead of playing with alpha channel.
Example:
input: http://i.stack.imgur.com/lhMgT.jpg
output: http://i.stack.imgur.com/EYO7c.jpg

Resources