[WIP] convert and optimize image in a single pass#289
[WIP] convert and optimize image in a single pass#289cking100 wants to merge 1 commit intoopenzim:mainfrom
Conversation
|
I've converted PR to draft since it is not ready, please mark it as ready once ... ready 🤣 |
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
7148ef8 to
e5357ec
Compare
e5357ec to
c10b2eb
Compare
|
@benoit74 I will add the test cases soon. But, before that, can I please get a review on the changes? |
benoit74
left a comment
There was a problem hiding this comment.
Few remarks. You are on the right path, thank you
| *, | ||
| convert: bool = False, | ||
| ) -> pathlib.Path | io.BytesIO: | ||
| """method to optimize PNG files using a pure python external optimizer""" |
There was a problem hiding this comment.
Docstring is not accurate anymore
| """Optimize image, automatically selecting correct optimizer | ||
|
|
||
| Arguments: | ||
| fmt: format of the source image, needed when src is io.BytesIO or bytes |
There was a problem hiding this comment.
I agree it is convenient to have the option to pass this when we already know the format to save the addition format_for, but I don't get why it would be mandatory for io.BytesIO, can you explain? format_for accepts io.BytesIO AFAIK.
I would recommend to rename this to src_format for clarity.
There was a problem hiding this comment.
You're right. format_for does detect io.BytesIO and thus, fmt is not mandatory. This is a mistake, I will fix it and also rename it since we would need dst_format to accept io.BytesIO as dst.
| def optimize_image( | ||
| src: pathlib.Path, | ||
| src: pathlib.Path | io.BytesIO | bytes, | ||
| dst: pathlib.Path, |
There was a problem hiding this comment.
I would like (if reasonably feasible) that we accept at least io.BytesIO as dst, bytes as well if feasible. This mandates to add dst_format as argument, but I'm fine with this.
We also need to add all meaningful signatures with @overloads so that it is clearer when a given argument is optional or mandatory at type checking time (in addition to the live checks the function performs afterwards).
There was a problem hiding this comment.
I agree we should accept io.BytesIO as dst, but I am not sure how we would be able to work with dst as bytes, as they are immutable. If you have any ideas lmk.
I will push a patch for dst to accept io.BytesIO soon.
There was a problem hiding this comment.
Indeed, dst as bytes makes no sense, too early comment on my side 🤣
Fixes #284
Changes:
optimize_xxxfunctions to accept aconvertparameteroptimize_imageandoptimize_gifto acceptio.BytesIOas inputbytesinsrcoptimize_xxxnow works on single passTODO