ImageMagick, PDF, MediaBox and CropBox

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?".
jimc99999
Posts: 21
Joined: 2009-01-29T08:44:41-07:00

ImageMagick, PDF, MediaBox and CropBox

Post by jimc99999 »

I have a ruby program which uses rmagick to build and write image files. The production files are written in PDF format. The PDFs have been written with the CropBox, but not the MediaBox, set to the size in inches based on the current resolution. This was the case at least up through ImageMagick 6.4.9-1_0, as installed by MacPorts. However, ImageMagick 6.4.9-6_0 appears to no longer set the CropBox when writing a PDF file. Both versions have been tested with rmagick 2.9.1.

My questions are:

1. was this an intentional change?

2. is there some other way I can manually set the CropBox and/or MediaBox?

Jim Crate
Que Viva, LLC
jimc99999
Posts: 21
Joined: 2009-01-29T08:44:41-07:00

Re: ImageMagick, PDF, MediaBox and CropBox

Post by jimc99999 »

Actually, after a bit more digging, I've found that the CropBox was actually being set with the size in pixels of the image at 72 dpi. Now, I believe it is being set to the same as the MediaBox, which is the actual number of pixels of the image. e.g.

image sized 14400 x 11200, 400 dpi:

IM 6.4.9-1 : CropBox = 2592 x 2016
IM 6.4.9-6 : CropBox = 14400 x 11200

In both cases MediaBox appears to be set to the actual image size (14400 x 11200). For my concerns, since I can control whether the program that processes these files uses the CropBox or MediaBox, I just need one of them set correctly.

Jim Crate
Que Viva, LLC
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: ImageMagick, PDF, MediaBox and CropBox

Post by magick »

In both cases MediaBox appears to be set to the actual image size (14400 x 11200). For my concerns, since I can control whether the program that processes these files uses the CropBox or MediaBox, I just need one of them set correctly.
Is ImageMagick setting the Crop / Media boxes to the correct value? If not, post an example that we can reproduce. Post what the box components are and what you think they should be. Thanks.
jimc99999
Posts: 21
Joined: 2009-01-29T08:44:41-07:00

Re: ImageMagick, PDF, MediaBox and CropBox

Post by jimc99999 »

after a bit more digging, I found that I can look at the pdf data directly with vi. I generated an image at 40" x 32", 250dpi.

Code: Select all

IM 6.4.9-1
  /MediaBox [0 0 10000 8000]
  /CropBox [0 0 2880 2304]


IM 6.4.9-6
  /MediaBox [0 0 10000 8000]
  /CropBox [0 0 10000 8000]
In IM 6.4.9-1, the CropBox was set to 40" x 32" at 72 dpi. In Adobe Reader, Preview.app, and the program I wrote to process these images, this allowed the software to calculate the size in inches of the PDF correctly.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: ImageMagick, PDF, MediaBox and CropBox

Post by magick »

Try the following:
  • convert image.png -page letter image.pdf
The crop box is set to the same size as the media box as recommend by http://www.prepressure.com/pdf/basics/page_boxes. If you feel either of these boxes are not being computed properly, let us know.
jimc99999
Posts: 21
Joined: 2009-01-29T08:44:41-07:00

Re: ImageMagick, PDF, MediaBox and CropBox

Post by jimc99999 »

I'm writing the file from rmagick as a pdf, and while I realize the image is saved in the pdf in some kind of image format, the users claim the pdf is higher quality than when saved as png or jpg (the ruby program can save in any of the 3 formats.

Also, I don't need a page size of "letter". This example is 40"x32", but the size varies according to what is being produced.

If the CropBox and the MediaBox should be the same, then they should both be set to a value that allows PDF software to determine the size of the media in inches. Since PDF has no way to specify the resolution that I'm aware of, then the size needs to be converted to 72 dpi. So, 40"x32", which is 10000x8000 at 250 dpi, should be converted to 2880x2304 (40"x32" at 72 dpi). If both MediaBox and CropBox need to be the same, then they should both contain that value, not 10000x8000, because then the page size will be calculated as 138.889" x 111.111", which is obviously not 40"x32".
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: ImageMagick, PDF, MediaBox and CropBox

Post by magick »

Stand by. We're checking with our PDF expert on the correct interpretation of MediaBox. We checked a few programs and they return the same MediaBox as ImageMagick does but we'll let the expert let us know which method is correct.
jimc99999
Posts: 21
Joined: 2009-01-29T08:44:41-07:00

Re: ImageMagick, PDF, MediaBox and CropBox

Post by jimc99999 »

I looked at some files created by Photoshop, and only the MediaBox parameter was set. The MediaBox parameter was set to the size of the image at 72dpi. So for example, creating a PDF using Photoshop from an image 14400 x 11200 at 400 dpi, the MediaBox parameter is set to 2592 x 2016, and the CropBox parameter is not even used.

Jim
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: ImageMagick, PDF, MediaBox and CropBox

Post by magick »

Look for a patch for this problem by sometime tomorrow. Thanks.
jimc99999
Posts: 21
Joined: 2009-01-29T08:44:41-07:00

Re: ImageMagick, PDF, MediaBox and CropBox

Post by jimc99999 »

Thanks!
allenmiglore

Re: ImageMagick, PDF, MediaBox and CropBox

Post by allenmiglore »

I've also run into this cropbox/mediabox problem when converting an image to pdf format. I haven't tried 6.4.9 or later, but at 6.4.2 I saw the mediabox in pixels, the cropbox in points. My reading of PDF docs is that both these values should be specified in "default user space units", which is points (i.e. 72 dpi).

This text is from the PDF specification manual:

MediaBox
rectangle
(Required; inheritable) A rectangle (see Section 3.8.4, “Rectangles”), expressed in default user space units, defining the boundaries of the physical medium on which the page is intended to be displayed or printed (see Section 10.10.1, “Page Boundaries”).

CropBox
rectangle
(Optional; inheritable) A rectangle, expressed in default user space units, defining the visible region of default user space. When the page is displayed or printed, its contents are to be clipped (cropped) to this rectangle and then imposed on the output medium in some implementation-defined manner (see Section 10.10.1, “Page Boundaries”). Default value: the value of MediaBox.

-Allen
jimc99999
Posts: 21
Joined: 2009-01-29T08:44:41-07:00

Re: ImageMagick, PDF, MediaBox and CropBox

Post by jimc99999 »

Can you tell me what version has this patch?
allenmiglore

Re: ImageMagick, PDF, MediaBox and CropBox

Post by allenmiglore »

I just downloaded the 6.5.0-6 release (convert.exe --version indicates 6.5.0-4). Apparently this includes a patch related to this problem as values have changed, but there is still an error. When I convert a 2552x3300, 300 dpi png image to pdf, the scale of the image data and of the page is wrong. This image should generate a 8.5x11 inch pdf page, but does not. The image is drawn at about 118 dpi rather than using standard PDF unit of points (72 dpi), resulting in a page about 13.9x18 inches.

Here's what I see:

In the /Page object, the media and cropboxes, which should be [0 0 612 792], are this:

/MediaBox [0 0 1004.72 1299.21]
/CropBox [0 0 1004.72 1299.21]

In the page content is a stream object that does a "cm" command to set the scaling of the image when drawn. It also should use 612x792, but does this:

1004.72 0 0 1299.21 0 0 cm

The image Xobject is correct, with a /Width 2552 and /Height 3300.

The formula for calculating the mediabox and cropbox and cm matrix should be: pixels*72/dpi.

-Allen
jimc99999
Posts: 21
Joined: 2009-01-29T08:44:41-07:00

Re: ImageMagick, PDF, MediaBox and CropBox

Post by jimc99999 »

I also had noticed that the mediabox and cropbox were not being calculated correctly. However, the image I was using for testing had resolution specified in PixelsPerCentimeterResolution, and I had already run into a different problem so I didn't test further at that time to see if an image with resolution specified in PixelsPerInchResolution would calculate correctly.

Jim
jimc99999
Posts: 21
Joined: 2009-01-29T08:44:41-07:00

Re: ImageMagick, PDF, MediaBox and CropBox

Post by jimc99999 »

I just tested with 6.5.0-8, and when creating a pdf, the mediabox and cropbox are being set to the size of the image in pixels, no conversions are being done at all now. These values need to be set to the size of the image in points, or pixels/dpi*72.

Jim
Post Reply