Imagemagic convert: How can we set a page "a4 Landscape"? - imagemagick-convert

Imagemagic convert:
How can we set a page "a4 Landscape"?
image Files and Folder structure:
number of jpg files: 2400.
separated into 30 folders: 80 jpgs in each folder.
Mission: create a pdf file for each folder, containing 80 images.
jpg image: 1366 x 768. (landscape)
1st trial:
convert -page A4 *.jpg pharma_reproductive.pdf
this results in a landscape image on A4 portrait. (too much white space.)
2nd trial:
convert -page A4 -rotate 90 *.jpg pharma_reproductive.pdf
this results in a 90 degree rotated landscape image on A4 portrait.
I can use this after 90 degree rotate on Adobe Reader everytime.
Given that there are 30 files and the number will increase,
there should be a way.
I googled with no luck, and I read the manual of imagemagic but
this geometry thing is too complicated...
(No offence, but Personally I think it should be more simplified...)
Thank you in advance.
Sincerely,
Doctor

I solved a similar problem (landscape US-Letter to portrait A4) by separating the size and rotation steps. For your problem, the solution could look like so:
convert -rotate 90 *.jpg rotate-%d.jpg
convert -page A4 rotate-%d.jpg rotate-%d.pdf # -density 450
convert -rotate -90 rotate-%d.pdf %d.pdf
You may want to add parameters -density 450 and -auto-orient to get the result in a good looking way.

Related

How to crop empty white space in PDF file using Preview on Mac?

There is a comic book PDF file which has a lot of white space at the bottom.
The content is almost half the length of the page size.
How to crop all pages in the PDF file?
I have tried imagemagick but the quality is poor.
convert -verbose -density 300 -interlace none -quality 100 output.pdf
In ImageMagick, try using a larger density and then resize by the inverse amount in percent. Here, I use density = 4*72 = 288 and then resize by 25% (1/4).
convert -density 288 image.pdf -resize 25% -fuzz 15% -trim +repage result.pdf

Batch Resize Photos While Maintaining the Aspect Ratio AND Fitting to a specific pixel size

I am looking to batch resize pictures to a specific pixel size while maintaining the aspect ratio. Is their a way that the picture aspect can be preserved and the rest of the space can be filled in with white? For example, I resize an image to 200 x 200 and due to preserving the aspect ratio it is changed to 200 x 194. I would like the actual image to remain 200 x 194 and white space to fill in the remaining area to create an image that is 200 x 200 px. Thank you in advance!
You can do that with ImageMagick which is included in most Linux distros and is available for macOS and Windows.
Just in Terminal, or Command Prompt on Windows:
magick input.jpg -background white -resize 200x200 -gravity center -extent 200x200 result.jpg
If you have many files to do, you may be better using ImageMagick's mogrify command, which will do them all in one go for you! So make a new directory called processed for the output files and then use this to process all PNG files in the current directory:
magick mogrify -path processed -background white -resize 200x200 -gravity center -extent 200x200 '*.png'
I don't know if you are on Windows or not, so you may or may not need the single quotes around the filenames at the end of that command. Basically, it determines whether the expansion of the filename list is done by the shell (which has limitations on the number of files), or internally by ImageMagick (which does not).
If you are running anything older than v7, the commands become:
convert input.jpg ...
or
mogrify ...

Searching for large images made from a low resolution

I have many product images on my local drive that I got from different sources and some of them are messed up a bit. I'm talking about images that are large in resolution but it is apparent that this resolution has been achieved by resizing the image from a very small source.
Is there a software or something that could find these usualy high-res but low quality images? Thanks for any ideas.
I have a few ideas, and I'll show what I am getting at with ImageMagick which is installed on most Linux distros and is available (for free) for macOS and Windows.
Just to clarify what I am talking about, it is the lack of high-frequency information (detail) in images when they upsized (up-rezzed) from smaller images. Here is an example:
The technique goes like this. Take an image, copy it, scale it down to a percentage of its original size and then scale it back up and measure how much it differs from the original. Here is an example:
magick start.jpg -set option:geom "%G" \( +clone -resize 50% -resize "%[geom]"\! \) -metric MSE -compare -format "%[distortion]" info:
0.00220709
If I now do that in a loop, I can get the MSE ("Mean Squared Error") for resizing an image down to 10% and back up, down to 20% and back up, down to 30% and back up like this:
for ((size=10;size<100;size+=10)); do
distortion=$(magick start.jpg -set option:geom "%G" \( +clone -resize "${size}%" -resize "%[geom]"\! \) -metric MSE -compare -format "%[distortion]" info: 2>&1)
echo $size $distortion
done
Sample Output
10 0.00641669
20 0.00461728
30 0.00351362
40 0.0027639
50 0.00220709
60 0.00173019
70 0.00130171
80 0.000935031
90 0.000637741
If you run that again, but redirect the output to a file called "data", you can plot it with gnuplot:
gnuplot --persist -e "set yrange [0:0.01];set title '10: MSE vs Resize Percentage';plot 'data'"
Now, we come to the actual point. If I run the plot for a file that was up-rezzed from 75% of its original size, then again for a file that was up-rezzed from 50% of its original size, and again for 25% and 15%, I can put them together in an animation like this:
Hopefully, you can see that the purple points depart from the x-axis (where the MSE error is low) immediately at the point corresponding to the percentage of the original size from which the image was up-rezzed.
So, I am suggesting that you look at your images and find a threshold for the error that would correspond to the degree of up-rezzing likely to be present and then test the error for any individual image against that threshold.
This would be just the same if you are on Windows, all the code above is only for generating the plots and numbers to make animations. You just need to get the MSE with one line:
magick YOURIMAGE -set option:geom "%G" \( +clone -resize "${size}%" -resize "%[geom]"\! \) -metric MSE -compare -format "%[distortion]" info:

How to blur/pixelate part of an image using ImageMagick?

I am using Perl and ImageMagick (Perl-API).
In a first step i would like to take a rectangle of an image and blur this part of the image. Desired result is the original image with the rectangle blured.
In a second step i need to blur a part of an image with a turned rectangle (i.e. turned by 35%).
How can i achieve this?
Best way I can think of is to leverage masks to blur against. This would allow you to "draw" a shape, and pass through what to be blurred.
Example:
# Create base image
convert rose: -sample 200x rose_large.png
# Create mask
convert -size 200x131 xc:black -fill white -draw 'circle 100 65 100 25' rose_mask.png
# Blur with mask
convert rose_large.png -mask rose_mask.png -blur 0x8 +mask rose_blur_mask.png
Other techniques and examples here. I'm not familiar with Perl API, but there should be a Mask method that accepts an image handler parameter.
Update
For rectangle, you would simply update the shape to draw on the mask. Here's an example where I'm only blur-ing what's inside a rectangle.
# Create rectangle mask
convert -size 200x131 xc:white -fill black -draw 'rectangle 50 30 150 100' rose_rectangle_mask.png
# And repeat blur apply
convert rose_large.png -mask rose_rectangle_mask.png -blur 0x8 +mask rose_blur_retangle_mask.png
As you asked for PerlMagick, I pulled my last few remaining hairs out to try and do this in Perl... the files 1.png, 2.png and 3.png are purely for debug so you can see what I am doing.
#!/usr/bin/perl
use strict;
use warnings;
use Image::Magick;
my $x;
my $image;
my $blurred;
my $mask;
# Create original fishscale image
$image=Image::Magick->new(size=>'600x300');
$image->Read('pattern:fishscales');
$image->Write(filename=>"1.png");
# Copy original image and blur
$blurred = $image->Clone();
$blurred->GaussianBlur('x2');
$blurred->Write(filename=>"2.png");
# Make mask and rotate
$mask=Image::Magick->new(size=>'600x300');
$mask->Read('xc:white');
$mask->Draw(fill=>'black',primitive=>'rectangle',points=>'100,100,200,200');
$mask->Set('virtual-pixel'=>'white');
$mask->Rotate(20);
$mask->Transparent('white');
$mask->Write(filename=>"3.png");
# Copy mask as alpha channel into blurred image
$blurred->Composite(image=>$mask,qw(compose CopyOpacity gravity center));
# Composite blurred image onto original
$image->Composite(image=>$blurred);
$image->Write(filename=>'result.png');
Here are the debug images...
1.png
2.png
3.png
result.png
There may be a much faster, simpler, more efficient way of doing this, but I don't know it, and there are precious few examples of PerlMagick out there, so I'll stick my marker in the sand and see if anyone can better it:-)
P.S. Don't feel bad about my hair - there were only three left anyway :-)

Imagemagick scale and image quality

I'm scaling an image down to 50% of its ratio with the convert command and -scale parameter. The generated image quality is pretty bad. Is there any extra option i can use to get a better result ?
-scale is quick but I belive -resize or -thumbnail is better and you can use any filters you like.
Using -sharpen 0x1.2 with -resize x% with -quality 95 produces good results for me.
I was trying to create thumbnails of PDFs and was getting poor results until I added -density 400, which is a suggestion I found here. Below is an example of the difference it can make. I got the PDF from here. Look at the bear and also the lines behind the bird.
Without -density 400
Full command: convert -resize 500x500 catalog.pdf[0] catalog-page1-resized.png
File size: 180 KB
With -density 400
Full command: convert -density 400 -resize 500x500 catalog.pdf[0] catalog-page1-resized.png
File size: 185 KB
Using -quality 80 -adaptive-resize is better for larger photos.
If you need to blur the output, use -interpolative-resize instead of -adaptive-resize

Resources