开发者

Centering a .jpg image in PDF using Imagemagick?

I'm using convert version ImageMagick 6.6.2-6 2011-03-16, and I'd like to use it to generate an A4 pdf from an image, where the image will be non-scaled and centered.

I'm running the following (as a modification of Overlaying Images with ImageMagick):

# generate a 100x100 JPG with just red color
convert -size 100x100 xc:red red.jpg

# generate PDF from JPG
convert -page A4 xc:white red.jpg -gravity center -composite -format pdf out.pdf

... but, basically nothing shows? Same thing happens for a png image...

Note that

  • Just 'convert -page A4 red.jpg out.pdf' works - but the image is not centered; (-gravity center causes image not to show)
  • If the image is png, 'convert -page A4 -gravity center red.png out.pdf' does indeed work开发者_高级运维 fine

... however, I'd like convert to embed the contents of the JPEG stream directly - hence, I wouldn't like to convert the JPG to PNG first.

So, would it be possible to use convert to center a JPG image in an A4 PDF page directly?

Many thanks in advance for any answers,

Cheers!

 

EDIT2: @John Keyes answer works for the example above; where the image is "smaller" than the PDF size -- however if the image is bigger, e.g.:

$ convert -size 1228x1706 -background \#f44 -rotate 45 gradient:\#f00-\#fff red.jpg
$ identify red.jpg 
red.jpg JPEG 2075x2075 2075x2075+0+0 8-bit DirectClass 120KB 0.000u 0:00.000

... then it will fail. However, it turns out: "if you change -extent to 50x50, then play with -gravity, you'll see changes" - except, the question is: which extent do you change, that of the image - or that of the final PDF?

Well, it turns out - it is the extent of the final PDF... To find that size as convert sees it, check the page: Magick::Geometry - however, note that the "Postscript page size specifications" like "A4+43+43>" unfortunately, cause convert to crash in this context... But at least the respective numbers for the size (595x842) can be copied from the page; and finally this works:

convert -page A4 -gravity center -resize 595x842 -extent 595x842 red.jpg out.pdf

... and actually, the -extent part is not really needed - the -resize part is the important one to have the large image show..

However, the problem here is that the image included seems to be resampled - however, I'd just like to show it scaled so it fits the page, but would otherwise like the original JPG stream to be inserted in the file.. So I guess the question is still partially open :)

EDIT: Related:

  • ImageMagick Gravity parameter - Stack Overflow
  • ImageMagick and Geometry Issue - resizing with > - Stack Overflow
  • command line - Resizing and croping images to an aspect ratio of 6x4 with width of 1024 pixels - Unix and Linux - Stack Exchange
  • conversion - using imagemagick or ghostscript (or something) to scale PDF to fit page? - Stack Overflow


The following works perfectly for me:

convert -page A4 red.jpg  -gravity center -format pdf out.pdf

and if you change the order of the "files" it works too:

convert -page A4 red.jpg xc:white -gravity center -composite -format pdf out.pdf

I think the red.jpg is centered but the white is drawn on top of it.


Well, this is outside of imagemagick, but here is a solution in Latex, using tikz package (using How to define a figure size so that it consumes the rest of a page? #14514), which seems to reliably place images on page, and preserve them fully:

% note: need to run pdflatex twice!! First time generates blank pages!
% convert -size 1228x1706 -background \#f44 -rotate 45 gradient:\#f00-\#fff red.jpg
% convert -size 595x1400 xc:red redlong.jpg

\documentclass[a4paper]{letter}

\usepackage{graphicx}
\usepackage[hmargin=0.5cm,vmargin=0.5cm]{geometry} % sets page margins

\usepackage{tikz}
\usetikzlibrary{calc}

\newcommand{\imagepage}[1]{
\tikz[overlay,remember picture]\coordinate (image-start); \par
\vfill
\null\hfill
\begin{tikzpicture}[overlay,remember picture]
  \path let \p0 = (0,0), \p1 = (image-start) in
    node [inner sep=0pt,outer sep=0pt,anchor=center] at (current page.center) {%
      \pgfmathsetmacro\imgheight{\y1-\y0}%
      \includegraphics[height=\imgheight pt,width=\textwidth,keepaspectratio]{#1}%
  };
\end{tikzpicture}%
}



\begin{document}


% there must be a \n\n after {letter!}
\begin{letter}

  \imagepage{red.jpg}
\end{letter}


\begin{letter}

  \imagepage{redlong.jpg}
\end{letter}

% see also \resizebox{\textwidth}{!}{\includegraphics{red.jpg}}
\end{document}

Note, the letter documentclass is used to allow that each image is split on separate page..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜