Center a .jpg image in a PDF using Imagemagick?

I am using the convert version of ImageMagick 6.6.2-6 2011-03-16, and I would like to use it to create an A4 PDF with an image where the image will be unscaled and centered,

I run 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 shows nothing? The same thing happens for the png image ...

note that

  • Only " convert -page A4 red.jpg out.pdf " works, but the image is not centered; ( -gravity center makes the image not show)
  • If the image png, ' convert -page A4 -gravity center red.png out.pdf ' really works fine

... however, I would like convert embed the contents of the JPEG stream directly - hence, I would not want to convert JPG to PNG first.

So, is it possible to use convert to center a JPG image on a PDF PDF page directly?

Thanks a lot in advance for any answers,
Hurrah!

EDIT2: @John Keyes is responsible for the example above; where the image is "smaller" than the size of the PDF, however, if the image is larger, for example:

 $ 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, and then play with -gravity, you will see the changes " - with the exception of the question: to what extent will you change the image of the image or the final PDF file?

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

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

... and in fact, the -extent part -extent not needed - the -resize part is important for displaying a large image.

However, the problem is that the included image seems to be oversampled - however, I just wanted to show it scaled to fit the page, but otherwise I would like the original JPG stream to be inserted into the file .. Therefore, I think that the question is still partially open :)

EDIT: Related:

+6
source share
2 answers

The following works fine for me:

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

and if you change the order of the β€œfiles”, it will work too:

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

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

+4
source

Well, this is outside of imagemagick, but here is the solution in Latex using tikz package (using How to determine the size of the shape so that it consumes the rest of the page? # 14514 ), which reliably puts the images on the page and saves them completely:

 % 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 that the letter documentclass class is used to allow the separation of each image on a separate page.

0
source

Source: https://habr.com/ru/post/898249/


All Articles