Adding oversized virtual canvas, puts the image in upper LHS

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
snovotill

Adding oversized virtual canvas, puts the image in upper LHS

Post by snovotill »

I'm putting variously sized images(1600x1200, 640x480) onto 4800x6300 virtual canvases, but I can't get the images to center on the canvas:

convert -monochrome -page 4800x6300 -background white -gravity center -flatten small.png centered.png
Note: I gave the "-flatten" directive above, ONLY so I can verify my results.

Do I actually have to calculate my X and Y offsets 4800x6300+X+Y for each image?
Is there a way to get ImageMagick to do this for me automagically?

Would Admin be able to add the answer to the "Canvas Creation" section of the wonderful site:
http://www.imagemagick.org/Usage/canvas/
The section seems to be missing info about adding canvases to images in this way.

P.S. My images have no alpha channel.

Thanks
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Adding oversized virtual canvas, puts the image in upper LHS

Post by fmw42 »

snovotill wrote:I'm putting variously sized images(1600x1200, 640x480) onto 4800x6300 virtual canvases, but I can't get the images to center on the canvas:

convert -monochrome -page 4800x6300 -background white -gravity center -flatten small.png centered.png
Note: I gave the "-flatten" directive above, ONLY so I can verify my results.

Do I actually have to calculate my X and Y offsets 4800x6300+X+Y for each image?
Is there a way to get ImageMagick to do this for me automagically?

Would Admin be able to add the answer to the "Canvas Creation" section of the wonderful site:
http://www.imagemagick.org/Usage/canvas/
The section seems to be missing info about adding canvases to images in this way.

P.S. My images have no alpha channel.

Thanks
I don't know the answer about centering, but the only info about virtual canvas that I know is at http://www.imagemagick.org/Usage/basics/#page
snovotill

Re: Adding oversized virtual canvas, puts the image in upper LHS

Post by snovotill »

Well, I did read through that stuff, but saw no reference to centering. At that point I came here.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Adding oversized virtual canvas, puts the image in upper LHS

Post by anthony »

snovotill wrote:I'm putting variously sized images(1600x1200, 640x480) onto 4800x6300 virtual canvases, but I can't get the images to center on the canvas

...

P.S. My images have no alpha channel.
Centering on a canvas is a real problem as centering on a virtual canvas is one function IM has not provided (yet).

Hmmm... however I can think of one way, but it would use a LOT of memory. As your image has no alpha, center it on a vary large transparent canvas then trim. Trim preverves the virtual position of the crop so it will provide a 'hack' to so the centering, regardless of the images size.

for example...

Code: Select all

convert image.jpg -alpha set -background none -gravity center -extent 4800x6300 -trim  image_virtually_centered.png
The better way however would be to extract the image size in a script or API and directly -repage the image appropriately.

As you mentioned you do calculate these you can just apply it

Code: Select all

convert  -page 4800x6300+1800+2600   image_1600x1200.jpg image_with_canvas.png
or

Code: Select all

convert   image_1600x1200.jpg -repage 4800x6300+1800+2600 image_with_canvas.png
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
snovotill

Re: Adding oversized virtual canvas, puts the image in upper LHS

Post by snovotill »

@anthony: I made small customization to your suggestion, and it centers my images fantastically well on the canvas thanks:
#convert test1.png -monochrome -alpha set -background white -gravity center -extent 4800x6300 -flatten centered.png
My scanned document appears perfectly centered on a white background of size 4800x6300 aka 8.5x11

Some notes:

1) I would love to see this technique "centering an image onto a canvas" posted to your website:
http://www.imagemagick.org/Usage/canvas/
#convert test1.png -monochrome -alpha set -background white -gravity center -extent 4800x6300 -flatten centered.png

2) Also for your website, the concept of creating a "virtual canvas" as follows:
#convert -page 9900x6300 -background white -flatten $i $i
Of course you probably wouldn't want to show "-flatten" on the website.

Thanks kindly for your help

P.S. In case you are interested: the following script is what I use to make very compact PDF manuals out of monochrome images. It compresses each image into both Deflate and Group4. Then it picks the smallest of the two, and strings them all to a pdf. Note that the script is about 6 times as long as it needs to be and uses tools other than just imagemagick, because i decided use a buggy old version of imagemagick, but it's all in the comments. No response necessary because this is just for your entertainment.

Before running the script, I pre-process my images to center them as per your advice:
for i in *.[PpBb][NnMm][GgPp]; do echo $i; convert -monochrome -alpha set -background white -gravity center -extent 4800x6300 -flatten $i $i; done

#!/bin/bash
# chmod 744 makemanual.sh
# ./makemanual.sh &> logfile &
# Makes PDF manual from many PNG TIF BMP GIF
# Files must be 600dpi or 300dpi Bitonal
# None, BZip, Fax, Group4, JPEG, JPEG2000, Lossless, LZW, RLE or Zip
# Version: ImageMagick 6.2.4 02/10/07 Q16 http://www.imagemagick.org
# Image info: identify -verbose 01_FC.tif
# apt-get install imagemagick
# apt-get install pdftk
# apt-get install libtiff-tools
# apt-get install gs-gpl
# The following command works in newer versions of imagemagick and quality 100 gives high compression
# convert $i -monochrome -compress Group4 -quality 100 -units PixelsPerInch -density 600 ${i%\.*}.pdf

echo Recompressing image files and making PDFs

for i in *.[PpBbTtGg][NnMmIiIi][GgPpFfFf]
do i_size=`wc -c < "$i"`
# The following intermediate conversion to gif bypasses an imagemagick 6.2 bug
convert -monochrome -compress None -units PixelsPerInch -density 600 $i temporary.gif

echo -n $i" SIZE="$i_size
convert -monochrome -compress Zip -quality 100 -units PixelsPerInch -density 600 temporary.gif temporary.png
png_size=`wc -c < "temporary.png"`
echo -n " PNG="$png_size
convert -monochrome -compress Group4 -quality 100 -units PixelsPerInch -density 600 temporary.gif temporary.tif
tif_size=`wc -c < "temporary.tif"`
echo -n " TIF="$tif_size
rm temporary.gif

if [ $png_size -lt $tif_size ]
then
rm temporary.tif
convert -compress Zip -quality 100 -units PixelsPerInch -density 600 temporary.png ${i%\.*}.pdf
o=${i%\.*}.png
rm $i
mv temporary.png $o


else
rm temporary.png
# The following imagemagick command would work in versions of imagemagick newer than 6.2:
# convert -compress Group4 -quality 100 -units PixelsPerInch -density 600 temporary.tif ${i%\.*}.pdf

# But we are using buggy OLD version imagemagick 6.2 so we have to do instead:
tiff2ps -2eaz temporary.tif > ${i%\.*}.ps
ps2pdf13 ${i%\.*}.ps
rm ${i%\.*}.ps
o=${i%\.*}.tif
rm $i
mv temporary.tif $o

fi
pdf_size=`wc -c < "${i%\.*}.pdf"`
echo " PDF="$pdf_size
done

echo Concatenating individual PDF files onto one big file
# The following imagemagick command would work but for bugs in imagemagick 6.2:
# convert *.pdf pdffile.pdf
# mv pdffile.pdf pdffile.fdp
# rm *.pdf
# mv pdffile.fdp pdffile.pdf

# But imagemagick OLD version 6.2 can't do this, so we must do the following instead.
pdftk *.pdf cat output temporary.fdp
rm *.pdf
mv temporary.fdp pdffile.pdf
out_size=`wc -c < "pdffile.pdf"`
echo "Finished compressing files and made pdffile.pdf $out_size"
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Adding oversized virtual canvas, puts the image in upper LHS

Post by anthony »

snovotill wrote:@anthony: I made small customization to your suggestion, and it centers my images fantastically well on the canvas thanks:
#convert test1.png -monochrome -alpha set -background white -gravity center -extent 4800x6300 -flatten centered.png
My scanned document appears perfectly centered on a white background of size 4800x6300 aka 8.5x11
NOTES:
  • The -flatten in this case is not needed if the image contains no transparency.
  • I thought you wanted a virtual canvas. What you wanted was centered on a larger image, whcih is covered in IM examples by -extent, and thumbnail padding.
I would love to see this technique "centering an image onto a canvas" posted to your website:
http://www.imagemagick.org/Usage/canvas/
The Canvas Section is on generating images and background canvases, not for enlarging images, which is the 'Cutting and Bordering' section where -extent is. But thank you for the comment.
Also for your website, the concept of creating a "virtual canvas" as follows:
#convert -page 9900x6300 -background white -flatten $i $i
Of course you probably wouldn't want to show "-flatten" on the website.
the is using image layering to make the actual image larger, and is covered in 'Image Layering'. It is a hack method typically used in IM versions before the addition of -extent.

However you arguments are not in order (you are using IM v5 legacy style) and you could get problems with this ordering later.
The command should have been...

Code: Select all

  convert -page 9900x6300 $i   \
                 -background white -flatten \
                 $i
whcih are three distinct operations.
  • Read a image giving it a virtual canvas size, without any offset to create a 'layered image'.
  • -flatten the layer image 'filling out' the virtual canvas with a white background. turning the image back into a normal 'non-layered image'.
  • Write it back into the file the image was read.
P.S. In case you are interested: the following script is what I use to make very compact PDF manuals out of monochrome images. It compresses each image into both Deflate and Group4. Then it picks the smallest of the two, and strings them all to a pdf. Note that the script is about 6 times as long as it needs to be and uses tools other than just imagemagick, because i decided use a buggy old version of imagemagick, but it's all in the comments. No response necessary because this is just for your entertainment.
Thank you. I'll go though the script and see what you do. I may add parts to the PDF section of 'Command Image Formats' later, If so I will note the source.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
snovotill

Re: Adding oversized virtual canvas, puts the image in upper LHS

Post by snovotill »

Thanks again kindly. No need to give me credit for any of my bad scripting even if you decide to use some ideas. That would just be unneeded noise. Back to churning out thousands of pages of PDF's. Debian dRules. Signing out.
Post Reply