Skip to content

[WIP] convert and optimize image in a single pass#289

Draft
cking100 wants to merge 1 commit intoopenzim:mainfrom
cking100:convert-optimize-single-pass
Draft

[WIP] convert and optimize image in a single pass#289
cking100 wants to merge 1 commit intoopenzim:mainfrom
cking100:convert-optimize-single-pass

Conversation

@cking100
Copy link
Copy Markdown

Fixes #284

Changes:

  • Enhanced optimize_xxx functions to accept a convert parameter
  • Modified optimize_image and optimize_gif to accept io.BytesIO as input
  • Added support for bytes in src
  • optimize_xxx now works on single pass

TODO

  • Write test cases

@cking100 cking100 changed the title convert and optimize image in a single pass [WIP] convert and optimize image in a single pass Mar 29, 2026
@benoit74 benoit74 marked this pull request as draft March 30, 2026 08:55
@benoit74
Copy link
Copy Markdown
Collaborator

I've converted PR to draft since it is not ready, please mark it as ready once ... ready 🤣

@benoit74 benoit74 marked this pull request as ready for review March 30, 2026 08:56
@benoit74 benoit74 marked this pull request as draft March 30, 2026 08:56
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 30, 2026

Codecov Report

❌ Patch coverage is 58.99281% with 57 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.32%. Comparing base (8c16fa6) to head (5e59e5c).

Files with missing lines Patch % Lines
src/zimscraperlib/image/optimization.py 58.99% 43 Missing and 14 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #289      +/-   ##
==========================================
- Coverage   99.52%   97.32%   -2.20%     
==========================================
  Files          41       41              
  Lines        2516     2584      +68     
  Branches      354      383      +29     
==========================================
+ Hits         2504     2515      +11     
- Misses          8       51      +43     
- Partials        4       18      +14     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@cking100 cking100 force-pushed the convert-optimize-single-pass branch 3 times, most recently from e5357ec to c10b2eb Compare March 30, 2026 23:01
@cking100
Copy link
Copy Markdown
Author

@benoit74 I will add the test cases soon. But, before that, can I please get a review on the changes?

Copy link
Copy Markdown
Collaborator

@benoit74 benoit74 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few remarks. You are on the right path, thank you

@cking100 cking100 force-pushed the convert-optimize-single-pass branch 2 times, most recently from 631d2e7 to b0eea3c Compare April 1, 2026 20:02
@cking100
Copy link
Copy Markdown
Author

cking100 commented Apr 1, 2026

@benoit74 I have made the requested changes. Can you please take a look? Thanks

Copy link
Copy Markdown
Collaborator

@benoit74 benoit74 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I realize we still have "details" to sort out ^^

Another general question: do we really need the @overload definitions now? I don't get anymore what they are adding in this specific case since it looks to me we now support all combinations (src is Path and dst is BytesIo, src is BytesIO and dst is Path, ...)

delete_src: whether to remove src file upon success (boolean)
values: True | False
convert: whether/how to convert from source before optimizing (str or boolean)
convert: whether to convert from source before optimizing (boolean)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I just realize now that dst_format was expected to be in convert variable.

I kinda prefer the new API which is more self-explanatory than "hiding" the dst_format in convert variable.

I also feel like the convert option is even not needed at all. We can automatically decide when conversion is needed based on src and dst format (either passed explicity or guessed from file names).

But this is a breaking API change, which would require a major release. I don't think we need it now, but we can prepare for it:

  • still support convert to contain both bool and dst_format (in addition to dst_format variable)
  • if something is passed in convert act accordingly (it "wins") but indicate it is a deprecated way of calling the API, expected to be remove in scraperlib 6
  • prepare another issue / PR to remove the convert parameter

WDYT?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good Idea. For now, we should support both convert variable as str as well as dst_format for backward compatibility, but raise a deprecated warning as you said, and we can prepare and remove(in future) the convert parameter directly since it's not needed anymore.

@cking100
Copy link
Copy Markdown
Author

cking100 commented Apr 2, 2026

Regarding the Overloads, since these functions are used by multiple scrapers, you might know better than me. We can either remove overloads and have cleaner code, but then we will have to deal with union type every time the functions are being called, or keep them and don't worry about it. @benoit74

@benoit74
Copy link
Copy Markdown
Collaborator

benoit74 commented Apr 2, 2026

Regarding the Overloads, since these functions are used by multiple scrapers, you might know better than me. We can either remove overloads and have cleaner code, but then we will have to deal with union type every time the functions are being called, or keep them and don't worry about it. @benoit74

Let keep it as-is for now and I will double-check this later

@cking100 cking100 force-pushed the convert-optimize-single-pass branch from b0eea3c to bb3e1c6 Compare April 2, 2026 12:24
@cking100 cking100 requested a review from benoit74 April 2, 2026 12:34
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

Successfully merging this pull request may close these issues.

How to convert and optimize an image to webp in one step with default settings?

2 participants