Change aspect ratio to fill page? #84

Closed
opened 2021-04-25 19:58:54 +00:00 by josch · 1 comment
Owner

By Felix E. Klee on 2020-08-29T01:27:51.811Z

Sometimes, if no scanner is around, I take photos of documents or postcards. These can be corrected fairly quickly for keystone and crop in Darktable’s, crop and rotate module, plus some color correction. After correction, however, the aspect ratio will never be perfect. An A4 page, for example, will be slightly off the √2 aspect ratio. As far as I can tell there is no way to correct aspect ratio in DarkTable. Even if there was, it would be another cumbersome step in the workflow. It could be avoided if img2pdf would allow stretching an image to fill an entire page.

I wonder if it is possible to stretch an image using a PDF command, and if that could be added to img2pdf, or hacked into a fork.


By josch on 2020-10-05T09:02:54.644Z


Yes, this is totally possible on a technical level. So there is no technical reason why at least a fork of img2pdf would not be able to offer this feature. So lets talk about whether a fork is necessary, or whether this feature should be part of img2pdf itself.

Firstly, we have to talk about feature creep. The Unix philosophy is, to have many tools and have each tool to one job, and do that well. The question then becomes: is "stretching an image" something that img2pdf should do, or are there other tools to do the job. If you process your images before anyways, then you are probably not much interested in the "lossless" property of img2pdf. In that case: what stops you from stretching your images with a different software than darktable? Right now, your feature request sounds like: "I don't want to change my workflow, so instead I offload the extra work I would otherwise have to this other tools I'm using". Why is changing your workflow not feasible for you? Or in other words: why is "changing img2pdf" the better option than you adjusting your workflow?

In a similar context to feature creep we also have to talk about whether this feature will only be useful to you, or whether it will be useful to a broader audience. In other words, currently the only example we have of a scenario where this would be useful is the user of a software that cannot stretch images and where the user doesn't want to adjust their workflow by using different tools. Without any additional scenarios in which this feature would be useful, that sounds like a very weak argument to me.

I also wonder if you really want to stretch the images and thereby end up with every element on the page, text as well as images being slightly crooked because squares will not be squares anymore, circles will not be circles and so on. Is this really what you want? And if yes, which other use-case can you imagine, where a user would want that kind of effect?

So thinking about the last point: isn't what you rather want, to automatically crop the image (if necessary) so that the resulting PDF is DIN A4 in size but the content does not become distorted?

Lastly, yes, the perfect aspect ratio of a DIN A4 page is √2, and that means that using raster images and even using floating point vector graphics (due to limited precision) you can never produce a perfectly sized DIN A4 document. But the DIN EN ISO 216 also comes with tolerances. This means, that the paper you can buy in stores also never has a perfect √2 aspect ratio but instead a DIN A4 paper will be 210 × 297 mm. This is close to √2 but only up to four digits. So what you should actually aim for is not a √2 aspect ratio but a size of 210 × 297 mm and that is practically possible with raster graphics even.


By Felix E. Klee on 2020-10-10T04:51:28.326Z


what stops you from stretching your images with a different software than darktable?

loss of quality, unnecessary increase in size

In other words, currently the only example we have of a scenario where this would be useful is the user of a software that cannot stretch images

Of course, I can stretch images using other software, but see above.

There also are – though exotic nowadays – images with non-square pixels. My first computer screen had non-square pixels.

I also wonder if you really want to stretch the images and thereby end up with every element on the page, text as well as images being slightly crooked because squares will not be squares anymore, circles will not be circles and so on. Is this really what you want?

See it more as a slight correction. From my experience, perspective correction tools such as those in darktable never restore the original to 100% perfect aspect ratio.

In the end, because I don’t know enough about PDF to extend img2pdf, I wrote images_to_pdf. This is a little script that takes a bunch of images and reencodes and stretches them to the desired page size using ImageMagick. Depending on the settings, this of course does result in a major loss of quality and unnecessary increase in size. Still it’s better than nothing.


By josch on 2020-10-10T06:13:54.029Z


Yes, there will be some quality loss if you stretch images like that. But will it be noticable?

There will also be some increase in size but only by a few kb due to the PDF container, right?

I also wonder why you are so focused on changing the aspect ratio of the image. Why don't you use imagemagick to create a DIN A4 sized image and then paste your own image onto that image such that it perfectly fits without stretching, just by resizing a bit. This is similar to the --fit option of img2pdf. I wonder why it is not useful in your case?

When I converted images using imagemagick from jpeg to pdf, I did not notice any significant increase in filesize or visible loss in quality.


By Felix E. Klee on 2020-10-10T07:18:35.881Z


I wonder why it is not useful in your case?

  1. I don't want to lose information, and 2. as I said "perspective correction tools such as those in darktable never restore the original to 100% perfect aspect ratio"

If I scan by taking a photo, then more often than not it's of notes or postcards which may have things written in the corners. Rarely they are in A4 format.

There will also be some increase in size but only by a few kb due to the PDF container, right?

No, more. ImageMagick reencodes everything, which changes file size and affects quality, especially with JPEG images. In the end I don't care too much about that. Scaling to the right aspect ratio is more important for me.

*By Felix E. Klee on 2020-08-29T01:27:51.811Z* Sometimes, if no scanner is around, I take photos of documents or postcards. These can be corrected fairly quickly for keystone and crop in Darktable’s, *crop and rotate* module, plus some color correction. After correction, however, the aspect ratio will never be perfect. An A4 page, for example, will be slightly off the √2 aspect ratio. As far as I can tell there is no way to correct aspect ratio in DarkTable. Even if there was, it would be another cumbersome step in the workflow. It could be avoided if `img2pdf` would allow stretching an image to fill an entire page. I wonder if it is possible to stretch an image using a PDF command, and if that could be added to `img2pdf`, or hacked into a fork. --- *By josch on 2020-10-05T09:02:54.644Z* --- Yes, this is totally possible on a technical level. So there is no technical reason why at least a fork of img2pdf would not be able to offer this feature. So lets talk about whether a fork is necessary, or whether this feature should be part of img2pdf itself. Firstly, we have to talk about [feature creep](https://en.wikipedia.org/wiki/Feature_creep). The Unix philosophy is, to have many tools and have each tool to one job, and do that well. The question then becomes: is "stretching an image" something that img2pdf should do, or are there other tools to do the job. If you process your images before anyways, then you are probably not much interested in the "lossless" property of img2pdf. In that case: what stops you from stretching your images with a different software than darktable? Right now, your feature request sounds like: "I don't want to change my workflow, so instead I offload the extra work I would otherwise have to this other tools I'm using". Why is changing your workflow not feasible for you? Or in other words: why is "changing img2pdf" the better option than you adjusting your workflow? In a similar context to feature creep we also have to talk about whether this feature will only be useful to you, or whether it will be useful to a broader audience. In other words, currently the only example we have of a scenario where this would be useful is the user of a software that cannot stretch images and where the user doesn't want to adjust their workflow by using different tools. Without any additional scenarios in which this feature would be useful, that sounds like a very weak argument to me. I also wonder if you really want to stretch the images and thereby end up with every element on the page, text as well as images being slightly crooked because squares will not be squares anymore, circles will not be circles and so on. Is this really what you want? And if yes, which other use-case can you imagine, where a user would want that kind of effect? So thinking about the last point: isn't what you rather want, to automatically crop the image (if necessary) so that the resulting PDF is DIN A4 in size but the content does not become distorted? Lastly, yes, the perfect aspect ratio of a DIN A4 page is √2, and that means that using raster images and even using floating point vector graphics (due to limited precision) you can never produce a perfectly sized DIN A4 document. But the DIN EN ISO 216 also comes with tolerances. This means, that the paper you can buy in stores also never has a perfect √2 aspect ratio but instead a DIN A4 paper will be 210 × 297 mm. This is close to √2 but only up to four digits. So what you should actually aim for is not a √2 aspect ratio but a size of 210 × 297 mm and that is practically possible with raster graphics even. --- *By Felix E. Klee on 2020-10-10T04:51:28.326Z* --- > what stops you from stretching your images with a different software than darktable? loss of quality, unnecessary increase in size > In other words, currently the only example we have of a scenario where this would be useful is the user of a software that cannot stretch images Of course, I can stretch images using other software, but see above. There also are – though exotic nowadays – images with non-square pixels. My first computer screen had non-square pixels. > I also wonder if you really want to stretch the images and thereby end up with every element on the page, text as well as images being slightly crooked because squares will not be squares anymore, circles will not be circles and so on. Is this really what you want? See it more as a slight correction. From my experience, perspective correction tools such as those in darktable never restore the original to 100% perfect aspect ratio. In the end, because I don’t know enough about PDF to extend `img2pdf`, I wrote [images_to_pdf][1]. This is a little script that takes a bunch of images and reencodes and stretches them to the desired page size using ImageMagick. Depending on the settings, this of course does result in a major loss of quality and unnecessary increase in size. Still it’s better than nothing. [1]: https://gist.github.com/feklee/1e395deafb33404372926e8db424fe91 --- *By josch on 2020-10-10T06:13:54.029Z* --- Yes, there will be some quality loss if you stretch images like that. But will it be noticable? There will also be some increase in size but only by a few kb due to the PDF container, right? I also wonder why you are so focused on changing the aspect ratio of the image. Why don't you use imagemagick to create a DIN A4 sized image and then paste your own image onto that image such that it perfectly fits without stretching, just by resizing a bit. This is similar to the `--fit` option of img2pdf. I wonder why it is not useful in your case? When I converted images using imagemagick from jpeg to pdf, I did not notice any significant increase in filesize or visible loss in quality. --- *By Felix E. Klee on 2020-10-10T07:18:35.881Z* --- > I wonder why it is not useful in your case? 1. I don't want to lose information, and 2. as I said "perspective correction tools such as those in darktable never restore the original to 100% perfect aspect ratio" If I scan by taking a photo, then more often than not it's of notes or postcards which may have things written in the corners. Rarely they are in A4 format. > There will also be some increase in size but only by a few kb due to the PDF container, right? No, more. ImageMagick reencodes everything, which changes file size and affects quality, especially with JPEG images. In the end I don't care too much about that. Scaling to the right aspect ratio is more important for me.
Author
Owner

Closing because of inactivity.

Closing because of inactivity.
josch closed this issue 2022-04-11 06:27:56 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
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#84
No description provided.