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..
Related
I have a PDF that I want to print and a small region of each page has a thick rainbow at the left border. It is on each page. In order to save color ressources I would like to convert only this region to grayscale - or remove it completely with a white rectangle. I have looked into imagemagick but could not find a suitable solution to keep all the other color on the pages.
I have also thought of exporting each page to a separate PDF, apply a rectangle filter to each pdf and then combine it again. But I would prefer a simpler approach as the quality of the graphs seem to decrease each time I convert a pdf.
You do not have to extract each page to do that in ImageMagick. You can process it all in one command. Here is an example.
Create PDF:
convert lena.jpg mandril3.jpg zelda1.jpg test.pdf
Create white image:
convert -size 100x100 xc:white white.png
Apply white image to every page of PDF:
convert test.pdf null: white.png -geometry +50+50 -layers composite result.pdf
I am trying to convert .pdf files to .jpg using image-magic
convert -limit -limit map 300 -flatten -density 300 -quality 100 -crop '400x400+20+20' dummy.pdf[0] test.jpg
but the problem i am facing is when i convert the file, it cropping the area but marking all the other area as white.
for example if i am converting a pdf with 1000x1000 size and cropping it to a 100x100 size, the output am getting is an image with 1000x1000 size with 100x100 area croped from the pdf and rest is white space.
sample.pdf
i cannot use trim, since my pdf may or may not have white border and trim will remove it
Your syntax is not in the proper order for Imagemagick. Most of the settings and operators need to come after reading the input PDF. Using Imagemagick 6.9.10.71 Q16 Mac OSX Sierra:
convert -limit map 300 -density 300 dummy.pdf[0] -background white -flatten -crop '400x400+20+20' -quality 100 test.jpg
How do I convert only page 2 of a pdf file to a jpg image file, using GraphicsMagick command line prompt?
What option can I use in the gm.exe convert command?
gm.exe convert testing.pdf testing.jpg
Add the page number (starting from zero) in square brackets after the PDF filename:
gm.exe convert testing.pdf[1] testing.jpg
By the way, you can use the same indexing technique for accessing specific frames of a GIF animation, or layers of multi-layer/directory TIFFs.
use the blow command, will get high quality png with white background.
magick convert -density 300 -quality 100% -background white -alpha remove -alpha off ./646.04.pdf ./x.png
How to convert PDF to PNG (and filter out the text)..
I want to render images and vector graphics (vector text included) without plain text
Below only the image is extracted.. not the whole page of the PDF
gs -sDEVICE=eps2write -dFILTERTEXT -dFirstPage=1 -dLastPage=1 -o out.eps 091.pdf
convert -density 300 -background white -alpha off out.eps -resize 2480x3508! OUT.png
from EPS
PDF
The FILTERTEST switch (as I'm pretty sure is documented) only works on text, not on image which contain a pattern of pixels which look like text, or on vector linework which looks like text.
Wihtout seeing your EPS I can't tell if the text you are complaining about is text or not, but my guess would be not.
By the way, if you want a PNG then there's no reason to convert the EPS to PDF, just render the EPS directly to a PNG file.
I have a lot of PDF documents that I want to convert to PNG, edit in Gimp, and then save back to the multipage Acrobat file. I'm filling out forms and adding scanned signature, trying to avoid printing, signing, then scanning back in, with the ability to type the information I need to enter.
I've been trying to use Imagemagick to convert to png files, which seems to work fine. I use the command convert -quality 100 -density 300x300 multipage.pdf single%d.png
(I'm not really sure if the quality parameter is right for png).
But I'm having problems with saving back to PDF. Some of the files have the wrong page size, and I've tried every command and procedure I can find, but there are always a few odd sizes. The resolution seems to vary so that it looks good at a certain zoom level, but either a few pages are specified at about 2" wide, or they are 8.5x11 but the others are about 35" wide. I've tried making sure Gimp had the canvass size and resolution correct, and to save the resolution in the file, but that doesn't seem to matter.
The command I use to save the files is convert -page letter -adjoin single*.png multipage.pdf I've tried other parameters, but none seemed to matter.
If anyone has any ideas or alternatives, I'd appreciate it.
"I'm not really sure if the quality parameter is right for PNG."
For PNG output, the -quality setting is very unlike JPEG's quality setting (which simply is an integer from 0 to 100).
For PNG it is composed by two single digits:
The first digit (tens) is (largely) the zlib compression level, and it may go from 0 to 9.
(However the setting of 0 has a special meaning: when you use it you'll get Huffman compression, not zlib compression level 0. This is often better... Weird but true.)
The second digit is the PNG data encoding filter type (before it is compressed):
0 is none,
1 is "sub",
2 is "up",
3 is "average",
4 is "Paeth", and
5 is "adaptive".
In practical terms that means:
For illustrations with solid sequences of color a "none" filter (-quality 00) is typically the most appropriate.
For photos of natural landscapes an "adaptive" filtering (-quality 05) is generally the best.
"I'm having problems with saving back to PDF. Some of the files have the wrong page size, and I've tried every command and procedure I can find [...] but either a few pages are specified at about 2" wide, or they are 8.5x11 but the others are about 35" wide."
Not having available your PNG files, I created a few simple ones with different dimensions to verify the different commands (as I wasn't sure myself any more). Indeed, the one you used:
convert -page letter -adjoin single*.png multipage.pdf
does create all PDF pages in (same) letter size, but it places my sample of (differently sized) PNGs always on the lower left corner of the PDF page. (Should a PNG exceed the PDF page size, it does scale them down to make them fit -- but it doesn't scale up smaller PNGs to fill the available page space.)
The following modification to the command will place the PNGs into the center of each PDF page:
convert \
-page letter \
-adjoin \
single*.png \
-gravity center \
multipage.pdf
If this is still not good enough for you, you can enforce a (possibly non-proportional!) scaling to almost fill the letter area by adding a -scale '590!x770!' parameter (this will leave a border of 11 pt at each edge of the page):
convert \
-page letter \
-adjoin \
single*.png \
-gravity center \
-scale '590!x770!' \
multipage.pdf
To leave away the extra border, use -scale '612!x792!'. -- Should you want only upward scaling to happen if required while keeping the aspect ratio of the PNG, use -scale '590<x770<':
convert \
-page letter \
-adjoin \
single*.png \
-gravity center \
-scale '590<x770<' \
multipage.pdf
Why not just use Xournal? That's what I use to annotate PDFs