This is not working as expected. Problems with dpi and borders. #177

Open
opened 2023-10-25 00:47:51 +00:00 by FBU · 7 comments

img2pdf 0.4.4
Brew installed.

I am trying to get an output file that is letter size with 300dpi resolution and images fit to pages centered with padding if necessary.

So far I have tried:

img2pdf -o out.pdf -S letter *.jpg

This gave me centered images at full size, like I want.
SCR-20231024-pemp.png

img2pdf -o out.pdf -S letter -s 300x300 *.jpg
img2pdf -o out.pdf -S letter -s 300 *.jpg

Images centered inside enormous borders.
SCR-20231024-pcbm.png

img2pdf -o out.pdf -S letter -s 300dpix300dpi *.jpg

Image cut off at top and sides and bottom cut off.
SCR-20231024-pibw.png

No matter what I try, I always get the same result.
magick identify out.pdf:
Resolution: 72x72
Print size: 8.5x11

What is the proper way to achieve what I want?
Resolution: 300x300
Print size: 8.5x11

`img2pdf 0.4.4` Brew installed. I am trying to get an output file that is letter size with 300dpi resolution and images fit to pages centered with padding if necessary. So far I have tried: `img2pdf -o out.pdf -S letter *.jpg` This gave me centered images at full size, like I want. ![SCR-20231024-pemp.png](/attachments/d43a53ea-4556-48b8-98b8-e25667cc5f1b) `img2pdf -o out.pdf -S letter -s 300x300 *.jpg` `img2pdf -o out.pdf -S letter -s 300 *.jpg` Images centered inside enormous borders. ![SCR-20231024-pcbm.png](/attachments/96209c71-775a-4ae0-b70d-04216fc1b57a) `img2pdf -o out.pdf -S letter -s 300dpix300dpi *.jpg` Image cut off at top and sides and bottom cut off. ![SCR-20231024-pibw.png](/attachments/e7012fcc-5296-4a43-9590-20fb26672029) No matter what I try, I always get the same result. `magick identify out.pdf`: Resolution: 72x72 Print size: 8.5x11 What is the proper way to achieve what I want? Resolution: 300x300 Print size: 8.5x11
Owner

Can you send me your input image?

If you want letter pages with 300dpi images, then

img2pdf -o out.pdf -S letter -s 300dpix300dpi

should be doing what you want. If it does not, maybe there is a bug somewhere.

Can you send me your input image? If you want letter pages with 300dpi images, then img2pdf -o out.pdf -S letter -s 300dpix300dpi should be doing what you want. If it does not, maybe there is a bug somewhere.
Author

arthurrackhamtre00rack_0018.arthurrackham2.gcv.im.jpg

img2pdf -o out.pdf -S letter -s 300dpix300dpi "arthurrackhamtre00rack_0018.arthurrackham2.gcv.im.jpg"

SCR-20231025-golv-2.png

Resolution: 72x72
Print size: 8.5x11

![arthurrackhamtre00rack_0018.arthurrackham2.gcv.im.jpg](/attachments/d233200b-a148-47ac-9092-b5b836243340) `img2pdf -o out.pdf -S letter -s 300dpix300dpi "arthurrackhamtre00rack_0018.arthurrackham2.gcv.im.jpg"` ![SCR-20231025-golv-2.png](/attachments/f56863d2-fc1d-447b-a1ac-f8bfc5650ab9) Resolution: 72x72 Print size: 8.5x11
Owner

So that input image is 3032x4063 pixels. If you scale that to 300 dpi, then that's 10.1 x 13.5 inch. This means it does not fit onto the letter paper format and it makes sense that parts are cut off.

I don't see the bug. What do you think would happen?

So that input image is 3032x4063 pixels. If you scale that to 300 dpi, then that's 10.1 x 13.5 inch. This means it does not fit onto the letter paper format and it makes sense that parts are cut off. I don't see the bug. What do you think would happen?
Author

Using img2pdf -o "$of" -s 300dpix300dpi "arthurrackhamtre00rack_0018.arthurrackham2.gcv.im.jpg" gives:

Page Size: 10.11 x 13.54 in
Image Size: 3032x4063

If I resize the image to 2550x3417 (300 dpi) and use img2pdf -o out -S letter "arthurrackhamtre00rack_0018.arthurrackham2.gcv.im.w=2550.px.jpg", I get:

Page Size: 8.50 x 11.00 in
Image Size: 2550x3417

This is what I want. How do I do this with img2pdf without resizing the original image? I see the -f FIT option but it is not clear which option to use. Is it possible to specify page size and dpi and fit this somehow automatically without knowing image size?

If I do img2pdf -o "out8.5x11.pdf" -S letter "arthurrackhamtre00rack_0018.arthurrackham2.gcv.im.jpg", I get:

Page Size: 8.50 x 11.00 in
Image Size: 3032×4063

I'd rather have Image Size: 2550x3417 without having to resize original image.

Using `img2pdf -o "$of" -s 300dpix300dpi "arthurrackhamtre00rack_0018.arthurrackham2.gcv.im.jpg"` gives: Page Size: 10.11 x 13.54 in Image Size: 3032x4063 If I resize the image to 2550x3417 (300 dpi) and use `img2pdf -o out -S letter "arthurrackhamtre00rack_0018.arthurrackham2.gcv.im.w=2550.px.jpg"`, I get: Page Size: 8.50 x 11.00 in Image Size: 2550x3417 This is what I want. How do I do this with `img2pdf` without resizing the original image? I see the -f FIT option but it is not clear which option to use. Is it possible to specify page size and dpi and fit this somehow automatically without knowing image size? If I do `img2pdf -o "out8.5x11.pdf" -S letter "arthurrackhamtre00rack_0018.arthurrackham2.gcv.im.jpg"`, I get: Page Size: 8.50 x 11.00 in Image Size: 3032×4063 I'd rather have Image Size: 2550x3417 without having to resize original image.
Owner

Oh, wait, just to confirm: you have your original image of 3032x4063 pixels and you would like img2pdf to change that so that the final image in the pdf is at around 2550x3417 pixels? You expect img2pdf to change the image for you?

Oh, wait, just to confirm: you have your original image of 3032x4063 pixels and you would like img2pdf to change that so that the final image in the pdf is at around 2550x3417 pixels? You expect img2pdf to **change** the image for you?
Author

I'm hoping that img2pdf can fit the original image scaled to 300 dpi into an 8.5x11 pdf like imagemagick can:

i="arthurrackhamtre00rack_0018.arthurrackham2.gcv.im.jpg"
oi="arthurrackhamtre00rack_0018.arthurrackham2.gcv.im.300dpi.pdf"
convert "$i" -resize 2550x3300 -density 300 -units pixelsperinch -background black -gravity center -extent 2550x3300 "$oi"

SCR-20231026-pqku_CR.jpg

I made the background black to show where the width of the image was fit without stretching.

arthurrackhamtre00rack_0018.arthurrackham2.gcv.im.jpg

arthurrackhamtre00rack_0018.arthurrackham2.gcv.im.jpg
arthurrackhamtre00rack_0018.arthurrackham2.gcv.im.300dpi.pdf

Can the -f FIT option do this?

I'm hoping that `img2pdf` can fit the original image scaled to 300 dpi into an 8.5x11 pdf like `imagemagick` can: ``` i="arthurrackhamtre00rack_0018.arthurrackham2.gcv.im.jpg" oi="arthurrackhamtre00rack_0018.arthurrackham2.gcv.im.300dpi.pdf" convert "$i" -resize 2550x3300 -density 300 -units pixelsperinch -background black -gravity center -extent 2550x3300 "$oi" ``` ![SCR-20231026-pqku_CR.jpg](/attachments/5304fc66-4924-4d99-a1d8-6c849e19cac5) I made the background black to show where the width of the image was fit without stretching. arthurrackhamtre00rack_0018.arthurrackham2.gcv.im.jpg ![arthurrackhamtre00rack_0018.arthurrackham2.gcv.im.jpg](/attachments/b70ad00b-345c-4d48-b7c9-6e478fe7d540) [arthurrackhamtre00rack_0018.arthurrackham2.gcv.im.300dpi.pdf](/attachments/71706491-5376-431a-89af-dd823e0a81a2) Can the -f FIT option do this?
Owner

Hi, I think I now understand the problem. Let me quote the very first sentence of the README:

Lossless conversion of raster images to PDF

Or the first point of the list below in bold font:

  1. always lossless

The word "lossless" is mentioned ten times in the README. The whole point of why img2pdf exists is to provide lossless conversion of images into the PDF container format. If you do not want this property, then there exist plenty of other pieces of software that can do that, see the five "Comparison" sections at the bottom of the README.

What you want is to scale the image by changing its resolution. That operation is clearly not lossless and that's why img2pdf cannot do it. If imagemagick can do what you want then you should just use that as the tool for your job.

I'm a bit puzzled that this property of img2pdf was not clear from the README. Can you point out what I could change so that you wouldn't have have been misled into thinking that img2pdf could actually change your image? I thought stating that it would be "always lossless" would make that quite clear. Should I add more explanations?

Hi, I think I now understand the problem. Let me quote the very first sentence of the README: > Lossless conversion of raster images to PDF Or the first point of the list below in bold font: > 1. always lossless The word "lossless" is mentioned ten times in the README. The whole point of why img2pdf exists is to provide lossless conversion of images into the PDF container format. If you do not want this property, then there exist plenty of other pieces of software that can do that, see the five "Comparison" sections at the bottom of the README. What you want is to scale the image by changing its resolution. That operation is clearly not lossless and that's why img2pdf cannot do it. If imagemagick can do what you want then you should just use that as the tool for your job. I'm a bit puzzled that this property of img2pdf was not clear from the README. Can you point out what I could change so that you wouldn't have have been misled into thinking that img2pdf could actually change your image? I thought stating that it would be "always lossless" would make that quite clear. Should I add more explanations?
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: josch/img2pdf#177
No description provided.