Reducing File Size without Reducing Page Size

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
mr_hunter
Posts: 16
Joined: 2015-12-09T08:45:47-07:00
Authentication code: 1151

Reducing File Size without Reducing Page Size

Post by mr_hunter »

Hi there,

The command below adds a transparent image on top of each page on a PDF. I'm trying to do this whilst creating as small a file as possible, but without changing the dimensions of the pages. The command below gets a small file but it sadly changes the dimensions of the pages.

convert.exe -units PixelsPerInch -density 300 226811.pdf -resample 150 null: stampOnDoc.png -gravity northwest -geometry +3+3 -define compose:args=70,100 -compose dissolve -layers composite -depth 8 -compress zip -resample 200 StampedPDF.pdf

Bonus question - is there a way of outputting the size of the pages in the PDF, and page count as an output of the command above or another command?

Any help appreciated :D
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Reducing File Size without Reducing Page Size

Post by snibgo »

"Dimensions" can mean the width and height measured in pixels, or measured in inches (centimetres, whatever). Which do you want to change?

EDIT: correction below.
snibgo's IM pages: im.snibgo.com
mr_hunter
Posts: 16
Joined: 2015-12-09T08:45:47-07:00
Authentication code: 1151

Re: Reducing File Size without Reducing Page Size

Post by mr_hunter »

Hi there... I think I'm confused.

The following link says that resample's purpose "is to adjust the number of pixels in an image so that when displayed at the given Resolution or Density the image will still look the same size in real world terms. That is, the given image is enlarged or shrunk, in terms of the number of pixels, while the image size in real world units will remain the same."
http://www.imagemagick.org/Usage/resize/#resample

Yet my image shrinks when I open it and look at the dimensions in Adobe Acrobat or pdfinfo.exe.

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

Re: Reducing File Size without Reducing Page Size

Post by fmw42 »

The dimensions in pixels should change but the printed page size (resolution and units, say inches) should remain the same when using -resample. Post the information from identify -verbose yourimage for both the input image and the output image (or post the images), so we can see.

Note that you are changing the density of your PDF, then combining the overlay processed using -resample based upon the density you specified, then resampling again. But you specified no units, so its default will be pixels per inch. Why are you doing two -resamples

Also note that PNG only allows density in units of pixels per centimeter. So if you specify pixels per inch for the units, it will get converted to pixels per centimeter.

It would seem to me to be simpler to just use -resize to the pixel dimensions you want and then set the desired density so that it prints to the inches or centimeters desired.

See http://legacy.imagemagick.org/script/co ... hp#density and the note about where Adobe Photoshop stores its density. Also see http://legacy.imagemagick.org/script/co ... .php#units. Also see http://legacy.imagemagick.org/script/co ... p#resample
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Reducing File Size without Reducing Page Size

Post by snibgo »

snibgo wrote:You use "-resample" which changes both pixels and inches. "-density" will change inches only, "-resize" will change pixels only.
That was garbage. Sorry. The following is correct, for raster images:

"-density" will change inches only. (It just change metadata.)

"-resize" will change the number of pixels, and the inches. (It adds or removes pixels, leaving the density metadata setting unchanged, so the inches will change.)

"-resample" will change the number of pixels, but not the inches. (It adds or removes pixels, but also adjusts the density metadata so the number of inches doesn't change.)

In this situation, your raster image is wrapped inside a PDF, which makes the situation horribly complicated.
snibgo's IM pages: im.snibgo.com
mr_hunter
Posts: 16
Joined: 2015-12-09T08:45:47-07:00
Authentication code: 1151

Re: Reducing File Size without Reducing Page Size

Post by mr_hunter »

Hi there,
Here is my current command which is similarly causing problems...
convert.exe -units PixelsPerInch -density 300 "232840_1.tif.pdf" "232840_2.tif.pdf" "232840_3.tif.pdf" "232840_4.tif.pdf" "232840_5.tif.pdf" "232840_6.tif.pdf" "232840_7.tif.pdf" "232840_8.tif.pdf" -adjoin -compress zip -resample 100 "StampedPDF.pdf"

It is shrinking the file dimensions but retaining the quality pretty well.

This produces this which is decent quality however is far smaller than A4:
https://drive.google.com/file/d/0B2WUzl ... sp=sharing

When I use resample such as this...
convert.exe -units PixelsPerInch -resample 300 "232840_1.tif.pdf" "232840_2.tif.pdf" "232840_3.tif.pdf" "232840_4.tif.pdf" "232840_5.tif.pdf" "232840_6.tif.pdf" "232840_7.tif.pdf" "232840_8.tif.pdf" -adjoin -compress zip -resample 150 "StampedPDF_300Sam150.pdf"

This retains the dimensions but instead it looses quality and results in this (compare text in last page especially):-
https://drive.google.com/file/d/0B2WUzl ... sp=sharing

The files I'm dealing with can be 1 bit - 24 bit and from A4 to A0 so the command needs to be flexible. Here are the individual files in the example above:-
https://drive.google.com/open?id=0B2WUz ... jlSODB3b1U

Any thoughts on this would be welcomed.

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

Re: Reducing File Size without Reducing Page Size

Post by fmw42 »

I would suggest you specify a large -density at the beginning before reading the pdfs, then combine them, then use -resize to the desired (smaller) pixel dimensions, then set the desired output density at the end. Don't use -resample.
mr_hunter
Posts: 16
Joined: 2015-12-09T08:45:47-07:00
Authentication code: 1151

Re: Reducing File Size without Reducing Page Size

Post by mr_hunter »

Hi there - I did think about something like that but I couldn't work out a way of getting the dimensions using IM and then passing them onto the final -resample. If you could show my how to do that I would be a happy fella. :-D
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Reducing File Size without Reducing Page Size

Post by fmw42 »

mr_hunter wrote:Hi there - I did think about something like that but I couldn't work out a way of getting the dimensions using IM and then passing them onto the final -resample. If you could show my how to do that I would be a happy fella. :-D

What file size do you want for the result as a factor of the original pdf size? The PDF normally has no size, but when converted to raster such as png, it usually gets 72 dpi unless you specify otherwise. So if you want the same size as the original, then use a density say of 288=72*4, then resize by 25%.
mr_hunter
Posts: 16
Joined: 2015-12-09T08:45:47-07:00
Authentication code: 1151

Re: Reducing File Size without Reducing Page Size

Post by mr_hunter »

Hi there - I would like the filesize to be as close to the original or smaller. I would like the page size (dimensions) to be the same size.

The thing is I'm confused about the following two examples:-
1) Original image is of 150dpi within PDF. Use Density of 600 and resize to 25%.
2) Original image is of 75dpi within PDF. Use Density of 600 and what do I resize by to get original dimensions?

So does my script need to know what the original dpi is of the image within the PDF in order to know what to resize it by?
Or can we say that if you are using density with a PDF then you always need to resize it at a ratio based on 72 dpi as PDFs are normally processed at 72dpi?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Reducing File Size without Reducing Page Size

Post by fmw42 »

Post one PDF that you think is internally 150 dpi and the other PDF that you think is 75 dpi. Then describe what you want for the output PDF dpi. Again IM is not the tool to use to go from vector to vector format. Also if your PDF has an imbedded image, that makes things difficult as well. You would be better off extracting the image from the PDF (see pdfimage) and then changing the dpi and saving again as PDF if that is what you desire.
Post Reply