convert -trim enlarges image width?

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
mike1
Posts: 2
Joined: 2011-01-13T01:01:43-07:00
Authentication code: 8675308

convert -trim enlarges image width?

Post by mike1 »

Hi all,

I have a postscript file with an image with a width of 5cm against a colour background. To extract the image and convert it to PNG I use:
convert -density 400 -trim p.ps p.png

However the width of the resultant p.png image is probably 15cm across (I view it with eye-of-gnome at 100% scale). Is there a way to create a PNG which preserves the original image size?.

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

Re: convert -trim enlarges image width?

Post by fmw42 »

the pdf file has no image size as it is vector (or contains an image), the pdf size in inches depends upon what you specify for the density. the density and original size in inches will determine the PNG pixel size. If you want the PNG to print to a different set of inches, then you need to change the density.

Also -trim will need +repage after it to remove the original canvas size so that its size represents the size you trimmed it to.

Perhaps you should post a link to your pdf and explain what size you want the PNG to be.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: convert -trim enlarges image width?

Post by anthony »

Most image viewers ignore the density (resolution) settings of a PNG, and just use the native resolution of the monitor (direct pixel to pixel display).

Change the resolution for the PDF read to your monitors resolution in pixels per inch.
(typically 90 to 120 ppi)

Some X window programs report the monitors resolution using the command

Code: Select all

xdpyinfo | grep resolution:
resolution: 96x96 dots per inch
BUT many also make a bad guess (as it was in my case).

Getting a ruler I measured 30.5 cm per 1200 pixels... whcih with a little math makes it 11.91 inches and thus close to 100 ppi

IM by default (for historical reasons) uses a resolution of 72 ppi.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
mike1
Posts: 2
Joined: 2011-01-13T01:01:43-07:00
Authentication code: 8675308

Re: convert -trim enlarges image width?

Post by mike1 »

Thanks for replying.

I'll try to clarify the issue I'm facing:

I have DVI files generated by LaTeX. The elements (text/images) occupy a small portion of the page. I want to import them into another program where the relevant supported formats seem to be EPS, PNG, SVG.

If the Latex document does not have a colour background I can convert the DVI to an EPS with a correct (tight) bounding box:
dvips f1.dvi && ps2eps f1.ps
generates f1.eps and gives a BB: 133 585 478 714
f1.eps is available at: http://ge.tt/6E9Kjpl

Now if I change the background to anything but white the BB is wrong (encloses the full page):
as above for f2.ps gives a BB: 0 1 596 843
(f2.eps is also in the link above).

So I was thinking of using convert with the -trim option to remove the empty regions.
xpdyinfo|grep resolution gives: resolution: 90x90 dots per inch
However, convert -density 90 -trim +repage f2.ps f2.eps
gives an EPS with poor quality. I can improve the quality by increasing the density but then the dimension of the image (when imported into the other program) is too large.

So, I'd appreciate if you can assist with these two questions:
1. Can GS identify the correct BB with a non-white background? (this is not IM-related).
2. If not, how I can generate a tight EPS or PNG with good quality which preserves the dimension of the original latex elements? that is, if the latex element is say 4cmx7cm then the resultant EPS, when imported into another program, would also occupy 4cmx7cm on the program's page.

Thanks,
Mike.
Post Reply