Adding an "imgposition" option #53
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
By Ben on 2018-12-02T21:47:11.928Z
Scenario:
Actual result:
Expect option
Example:
mg2pdf --output out.pdf --pagesize Legal --imgposition North *.jpg
ImageMagick > Image Positioning using Gravity
So --imgposition should support:
http://www.imagemagick.org/Usage/annotating/#gravity
http://www.imagemagick.org/script/command-line-options.php?#gravity
By josch on 2018-12-02T22:50:34.484Z
img2pdf is primarily supposed to be a simple converter from image to pdf. It is not supposed to be a competitor to powerful tools like imagemagick or other image manipulation tools. As it is, img2pdf already contains a number of convenience options that go far beyond the capabilities of a "simple converter". As by the UNIX philosophy each tool should only do one thing and do that one thing well. Thus I recommend that you either edit your image with a tool like ImageMagick in a way that you described in your initial post before sending it to img2pdf or you use another tool like LaTeX to convert a Letter-sized PDF into a Legal-sized PDF with the border that you want it to have.
By Ben on 2018-12-03T01:38:41.235Z
I completely agree with you about the importance of sticking to the UNIX philosophy. img2pdf is fast / light and get the job done perfectly based on my preliminary tests, that IS the reason why I want to use it over other solutions. Also, I think that --imgposition would respect the "rules" governing the UNIX philosophy. The proposition is based on the following logic :
In summary, not having this option could create, in some circumstances, unintended and negative "externalities" to other users / developers that would be quite "inexpensive" to fix right away in img2pdf...
By josch on 2018-12-03T06:51:24.082Z
Thanks for your extensive reasoning about this issue.
I think I have a solution that could make both of us happy.
Instead of adding yet another option and thus increasing the complexity of img2pdf command line arguments, the same functionality could also be implemented by improving the
--border
option. That option so far only allows either one or two values to specify border size. If it would also allow four values, then it could be used to position the image on the page in the way that you requested like so:The order of border values could be the same as used in the CSS
border-size
property which might be familiar to some:The behaviour with one and two values is already what img2pdf is doing right now. So the example command I gave above would put the image on a Legal sized paper such that there are 3 inches of space at the bottom of the page.
By Ben on 2018-12-03T08:08:10.224Z
If I understand your proposition right, it would work well in the case of images with a known and standardized size for each batch of images.
But what about book excerpts with variable sizes, images that we want to be aligned vertically at the top and horizontally at the center of the page in a single pdf file. Each image position would need to be adjusted using the borders?
I've bring the ImageMagick example because I find elegant the concept of "gravity". You calculate the remaining area after applying the borders, apply the "fit" function to the image and apply the gravity as requested. It allows to automate the positioning of images with random sizes and keep option to a bare minimum. That probably respects the philosophy behind img2pdf.
Now, building on existing option parameters, why not using the circumflex to our advantage...
or more CSS style:
That would generate a page with the image on a Legal sized paper such that there are 3 inches of space at the bottom of the page
and the image is top / center
.That would be quite elegant and powerful!
By josch on 2018-12-03T12:06:00.995Z
Would your specific use-case (letter -> legal) be covered by extending the
--border
option?Or are you also using img2pdf on scans from books as you describe?
By Ben on 2018-12-03T15:39:37.841Z
Yes and yes.
Book excerpts and receipts (some scanned, faxed or coming from smartphone cameras and cropped by the user), all end up with some random sizes.
Something like
--border 0:0:3in^Top:Center
would cover all the scenarios the pipelines I'm working on must handle.Also, your idea of a more powerful "border" option would indeed be handy, for example on papers that could have standardized pre-printed contents, like a letterhead with contents on three sides, that would allow to create "protected area" quite easily and push the image directly where it should go... very nice!
By josch on 2018-12-03T20:11:39.307Z
Hrm... I guess I could be persuaded. I have to sleep over this.
Though I fear I will not have time to implement this myself. And I also lack motivation because I personally have no need for this feature. But pull requests are welcome!
By Ben on 2018-12-04T04:58:34.384Z
Great! That was probably the hardest part ;-)
I'm no Python programmer but I will see what I can do. After a very quick review:
We know / have the following:
Right?
If so, we could apply an offset based on the center coordinates to get a gravity function.
The +/-offset(s) (x, y or x/y) would be the space(s) length between the image edge(s) and inside edge(s) of border(s) in the direction of the "gravity", if greater than 0.
The offset length would be
((pagesize/2)-border) - (imgsize/2)
Make sense?
The borders "à la" CSS, would require more work but the gravity function could be written for future support of four borders width.
For reference, the ImageMagick
GravityAdjustGeometry()
function:By josch on 2018-12-04T07:32:37.437Z
Whatever you implement, your pull request must contain tests!
Have a look at
git/img2pdf/src/tests/__init__.py
where at the top you can find an extensive test of nearly all possible combinations of the layout options. Naturally, none of these tests must be broken by your changes (check withsetup.py test
) but you also have to add a sufficient number of your own tests if you implement the gravity function.By Alba on 2019-01-24T02:56:38.400Z
I'm also in search of this! I modified the code so that the layout function can also (optionally) return
imgxpdf
andimgypdf
, and it will be used instead of being calculated to be centered. This change allows one to implement weird positioning as needed by usingimg2pdf
as a library. And it doesn't break compatibility.If an option is desired, the next change would be to modify
get_layout_fun
so it handles asymmetric borders.By josch on 2020-04-05T13:25:03.461Z
Hi @mildsunrise sorry for taking so long to come back to this. The change you propose would indeed not break anything. Do you still need this functionality? Is the interface useful to you the way you propose it? If you send a merge request or a full git format-patch or just tell me the name and email you would like to be attributed by, then I can include your changes. Thanks!
Closing because of inactivity.