Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to start counter generator at index 0 #279

Open
Eskapp opened this issue Jan 22, 2024 · 1 comment
Open

Add an option to start counter generator at index 0 #279

Eskapp opened this issue Jan 22, 2024 · 1 comment

Comments

@Eskapp
Copy link

Eskapp commented Jan 22, 2024

This is a feature request.

As Python indexing starts at 0, it would be natural to have the option to start the pdf2image.generators.counter_generator starting at 0. Currently, it can only start at 1 (except if I missed something).
https://github.com/Belval/pdf2image/blob/master/pdf2image/generators.py#L43

Typically, a simple extra optional argument: index_start (or similar) could be added and default to the current behavior

Also (and this is where the request idea initially comes from), pymupdf / fitz starts page index at 0. Adding this option would allow to get the same indexing for the images generated with pdf2image and the page numbers generated by pymupdf / fitz.

@zachguo
Copy link

zachguo commented Apr 3, 2024

I suppose the interface allows the user to pass your own generator as output_file.

But it seems to be buggy.

from pdf2image.generators import counter_generator

outfn_gen = counter_generator("test")

convert_from_path(
    ...
    output_file=outfn_gen,
    ...
)

The file names would look like

test0001-1.jpg  test0001-3.jpg  test0001-5.jpg  test0001-7.jpg
test0001-2.jpg  test0001-4.jpg  test0001-6.jpg

The zero-padded index (0001) never increments.

def counter_generator(prefix="", suffix="", padding_goal=4):
"""Returns a joined prefix, iteration number, and suffix"""
i = 0
while True:
i += 1
yield str(prefix) + str(i).zfill(padding_goal) + str(suffix)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants