Page sizes inside PDF are wrong when putting images as separate pages - imagick

I'm using Image Magic 7.1
$ convert -version
Version: ImageMagick 7.1.0-40 beta Q16-HDRI x86_64 21a5642bc:20220620 https://imagemagick.org
Copyright: (C) 1999 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI OpenMP(4.5)
Delegates (built-in): fontconfig freetype jng jpeg pangocairo png x zlib
Compiler: gcc (10.2)
When i attempt to place two image files into a PDF a very strange thing happens: Image sizes are correct, but page sizes of the resulting PDF are wrong, and the images are stretched to fit the page (However, if you were to extract those images, you'd see that the sizes are identical).
I am trying to do so in two different ways:
convert file.jpeg -resize 1795x1287^ -gravity center -extent 1795x1287 /tmp/1.jpeg
convert back.png /tmp/2.jpeg
convert /tmp/1.jpeg /tmp/2.jpeg output-test.pdf
back.png is already in the correct resoluion. I can see that temporary files are of correct resolution, but the PDF is wrong.
I am also trying to do this in a single command:
convert \
file.jpeg -resize 1795x1287^ -gravity center -extent 1795x1287 \
back.png \
-quality 100 \
output.pdf
The resulting file is displayed thusly

I have managed to generate the file the way i wanted, but i had to abandon using Image Magic for the last step. I have tried -density and -page, but to no avail. What worked in the end:
convert file.jpeg -resize 1795x1287^ -gravity center -extent 1795x1287 -density 300 -quality 100 /tmp/1.jpeg
convert back.png -density 300 -quality 100 /tmp/2.jpeg
img2pdf /tmp/1.jpeg /tmp/2.jpeg --pagesize 1795x1287 -o output-test.pdf

Related

Converting very long images (800px x 12,000px) into PDF with img2pdf and image magick leaves large white spaces between images

I have 10 images that range from 800x8500 to 800x11500 with one of them being header/footer images that are 1100x875 that are intended to be stacked vertically, end to end (its a web comic meant to be read from top to bottom) but every time I try to convert the images using either image magick or img2pdf there are very large dead spaces (approx. 1000px) in between the images.
Is there any way that I can remove this dead space or convert the sequence of images without all of the extra space in between?
img2pdf --output name.pdf --fit exact 800x12000 --pillow-limit-break $(\ls -v *.jpg)
img2pdf --output name.pdf -f shrink --viewer-fullscreen --pillow-limit-break $(\ls -v *.jpg)
convert $(\ls -v *.jpg) -quality 100 out.pdf
convert -define pdf:use-cropbox=true $(\ls -v *.jpg) -quality 100 out.pdf
convert -density 300 -background white $(\ls -v *.jpg) -quality 100 out.pdf
convert $(\ls -v *.jpg) -gravity center -append out.png && imgpdf out.png -o out.pdf
(corrupted the file even with --pillow-limit-break)
I'm using the terminal + bash to do this so far but I'm open to alternative solutions.

Vips + mozjpeg vs Imagick

I'm looking for a solution to get the best jpeg compression when down scaling an image. I'm comparing Vips + Mozjpeg and Imagick (convert).
My original file (pic.jpg) is 6.5 MB.
I run:
vipsthumbnail pic.jpg --size=1920x1280 --delete -o pic-vips-q96.jpg[Q=96,optimize_coding,strip,intelace]
and I my output file is 1.7 MB
I run:
convert pic.jpg -resize 1920x1280 -quality 96 -interlace plane -strip pic-imagick-q96.jpg
and my output file is 1.2 MB
Am I doing fair comparison here? Is Imagick that much better in compressing?
The link to the original image (from unsplash):
https://images.unsplash.com/photo-1545278068-cdca78378350
I'm comparing these two libraries because they both have Go bindings, which I need in my project.
Grateful for any advice!
libvips automatically disables chroma subsampling for Q > 90, so your two compression settings are not quite the same. Try this:
$ vipsthumbnail pic.jpg --size=1920x1280 -o pic-vips-q90.jpg[Q=90,optimize_coding,strip,interlace]
$ ls -l pic-vips-q90.jpg
-rw-r--r-- 1 john john 495764 Dec 20 17:17 pic-vips-q90.jpg
$ convert pic.jpg -resize 1920x1280 -quality 90 -interlace plane -strip pic-imagick-q90.jpg
$ ls -l pic-imagick-q90.jpg
-rw-r--r-- 1 john john 492029 Dec 20 17:17 pic-imagick-q90.jpg
So they are very close. The remaining difference is perhaps just in the downsize algorithm -- maybe libvips is making a very slightly sharper image.
libvips will probably be using libjpeg-turbo by default. If you want to compress with mozjpeg, you'll need to build everything from source.

Convert entire folder to greyscale using image magick?

I am trying to convert an entire folder to grayscale, using image magick.
convert *.jpg -colorspace Gray -separate -average
is met with this error :
convert: `-average' # error/convert.c/ConvertImageCommand/3290.
What is the correct command for this?
If you have lots of files to process, use mogrify:
magick mogrify -colorspace gray *.jpg
If you have tens of thousands of images and a multi-core CPU, you can get them all done in parallel with GNU Parallel:
parallel -X magick mogrify -colorspace gray ::: *.jpg
Also, the following can be used in a script - for the context menu of file managers like Dolphin, Nautilus, Nemo, Thunar etc:
for filename in "${#}"; do
name="${filename%.*}"
ext="${filename##*.}"
cp "$filename" "$name"-grayscale."$ext"
mogrify -colorspace gray "$name"-grayscale."$ext"
rm "$name"-grayscale."$ext"~
done

Difference of multipage tiff

I have to calculate the difference of two images. Currently I do this with imagemagick. The following code works well for jpg or single page tiffs:
convert fileA.jpg fileB.jpg -compose -Difference -composite -colorspace gray -format '%[fx:mean]' info:
When I edit a multipage tiff on the second page and use command above with modified and unmodified tiff, I get the same result like compare fileA with itself, so I think that imagemagick compares page 1 with page 2 of fileA.
Actual I split the multipage images into single image files and compare page by page, but this is slow!
Windows:
...>magick -version
Version: ImageMagick 7.0.8-11 Q16 x64 2018-08-29 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2018 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Visual C++: 180040629
Features: Cipher DPC Modules OpenMP
Delegates (built-in): bzlib cairo flif freetype gslib heic jng jp2 jpeg lcms lqr lzma openexr pangocairo png ps raw rsvg tiff webp xml zlib
Ubuntu 14.04
....# convert -version
Version: ImageMagick 6.7.7-10 2018-07-10 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC
Features: OpenMP
How can I get a percentage difference of the two compared tiffs fast? Maybe there is a way with imagemagick or another with bash/java/...
You should be able to do that by appending all the pages of each TIFF image together before doing the composite in Imagemagick. For example with a 4 page test tiff that I made, where the second page has been blurred:
convert \( test1.tif +adjoin -append \) \( test2.tif +adjoin -append \) -compose difference -composite -colorspace gray -format '%[fx:mean]\n' info:
0.00847632
You can also do that with -layers composite and get the difference for each page.
convert test1.tif +adjoin null: test2.tif +adjoin -compose difference -layers composite -colorspace gray -format '%[fx:mean]\n' info:
0
0.0324311
0
0

Merge multiple images into different combinations

In my folder i have 4 pics every time in a 4X6 pic size.
In next step i want to show all the 4 pic's into different cobinations to user like, {pics: 1 2 3 4}
image 1: 1+2;
image2: 1+2+3;
image3: 2;
image4: 1+2+3+4;
how do i can achive this...in .net/any technology please suggest.
You can do it like this with ImageMagick which is installed on most Linux distros and is available for OSX, and Windows. At the command-line, in Terminal, side by side:
convert a.png b.png +append result.png
Or, above and below:
convert a.png b.png -append result.png
Or, 4-up:
convert a.png b.png +append \( c.png d.png +append \) -append result.png
Or 4-across:
convert a.png b.png c.png d.png +append result.png
If your images are JPEG, just change the extensions. If you are on Windows, you may need to put carets (^) in front of the \ or ( to escape them.

Resources