-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
ENH: Add color parameter to AnnotationBuilder.rectangle #1723
Conversation
Codecov ReportPatch coverage has no change and project coverage change:
Additional details and impacted files@@ Coverage Diff @@
## main #1723 +/- ##
==========================================
- Coverage 92.49% 92.47% -0.03%
==========================================
Files 34 34
Lines 6533 6514 -19
Branches 1292 1288 -4
==========================================
- Hits 6043 6024 -19
+ Misses 318 317 -1
- Partials 172 173 +1
... and 4 files with indirect coverage changes Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report in Codecov by Sentry. |
I have added colour variable while draw rectangle on pdf change _annotations.py file rectangle function |
I have added colour variable while draw rectangle on pdf change _annotations.py file rectangle function |
Thank you for the PR 🙏 Should we rather call it Also, new parameters need to be added to the end for backwards compatibility. And I think we should start using keyword-only parameters, e.g.:
|
As an alternative, as "/C" is used on many annotations I propose to add a function applicable to all annotations. example :
it would be great if the color could be either an arrayobject, a standard array or tuple of ints, and maybe an string from PIL.ImageColor. @MartinThoma / @hirenpopat67 your opinion ? |
@pubpub-zz What would be the advantage of a method |
Sounds good - but we should do it consistently for all color parameters in pypdf |
the advantage would be to be able to have this solution for all annotations (circle, line,...) also it will be a good approach later to add other fields/properties |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Ensure backwards compatibility by making it a keyword-only argument
- Use a name that is more telling than
rectangle_color
, e.gborder_color
*Dear @MartinThoma,*
Thank you for taking the time to review my pull request. I appreciate your
feedback and suggestions for improvement. I completely agree with your
comments and will make the necessary changes to ensure backwards
compatibility by making the argument a keyword-only one.
Additionally, I will rename the argument to something more descriptive such
as "border_color" to better reflect its purpose. These changes should help
make the code more readable and user-friendly.
Once again, thank you for your help and valuable feedback. I will work on
these changes and submit a revised pull request as soon as possible.
Best regards,
*Hiren Popat*
Software Engineer (Python Developer)
…On Sat, 18 Mar 2023 at 21:21, Martin Thoma ***@***.***> wrote:
***@***.**** requested changes on this pull request.
1. Ensure backwards compatibility by making it a keyword-only argument
2. Use a name that is more telling than rectangle_color, e.g
border_color
—
Reply to this email directly, view it on GitHub
<#1723 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AQ32DDZ7WZJK6A6OXUY6L7TW4XKWXANCNFSM6AAAAAAV6IXZAI>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
You mean as a way to avoid code duplication? The current annotation builder methods return DictionaryObjects. How would we change it so that this method can be added? |
I would propose the AnnotationBuilder functions to create AnnotationDictionaryObject on which class the functions |
I have created the set_border_color() function and would like to request
your feedback before creating a new pull request to merge the code. Please
review the code and let me know if you have any suggestions or concerns.
Review:-
https://github.com/hirenpopat67/pypdf/blob/main/pypdf/generic/_annotations.py
Thank you for your attention to this matter. I look forward to hearing your
feedback.
Best regards,
*Hiren Popat*
Software Engineer (Python Developer)
…On Sun, 19 Mar, 2023, 12:18 am pubpub-zz, ***@***.***> wrote:
the advantage [of a method .set_border_color in contrast to a
keyword-argument] would be to be able to have this solution for all
annotations (circle, line,...)
You mean as a way to avoid code duplication?
The current annotation builder methods return DictionaryObjects. How would
we change it so that this method can be added?
I would propose the AnnotationBuilder functions to create
AnnotationDictionaryObject on which class the functions set_border_color()
and later some other ones such as annotation_name("/NM"),
border_type("/Border"),...
—
Reply to this email directly, view it on GitHub
<#1723 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AQ32DD3JEUS7FSEI5WQ7BGLW4X7ONANCNFSM6AAAAAAV6IXZAI>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
As discussed i have created set_border_color function to avoid duplication |
@hirenpopat67 |
Great ,I have reviewed code its working fine. Thank you for your time and consideration.. |
I hope this email finds you well. I am writing to express my gratitude for
your proposal to merge the code. I have reviewed the code and can confirm
that it is working flawlessly.
I am glad to hear that my contribution has helped in achieving the
project's goals.
Thank you once again..
Best regards,
*Hiren Popat*
Software Engineer (Python Developer)
…On Tue, 21 Mar, 2023, 4:03 pm Hiren Popat, ***@***.***> wrote:
I hope this email finds you well. I am writing to express my gratitude for
your proposal to merge the code. I have reviewed the code and can confirm
that it is working flawlessly.
I am glad to hear that my contribution has helped in achieving the
project's goals.
Thank you once again..
Best regards,
*Hiren Popat*
Software Engineer (Python Developer)
On Tue, 21 Mar, 2023, 3:05 pm Hiren Popat, ***@***.***> wrote:
> Thankyou for PR
>
> From my initial inspection, it looks like your changes are working as
> intended.
>
> In addition to the changes that I made, I also created a new function
> called set_border_colour which allows users to customize the color of the
> annotations that they draw. I think this would be a valuable addition to
> the project and would greatly enhance its functionality.
>
> I would greatly appreciate it if you could take a look at my code and
> merge it into the branch if you agree that it would be a beneficial
> addition. If there are any issues that need to be addressed before merging,
> please let me know and I will be happy to make any necessary changes.
>
> Thank you for your time and consideration.
>
> Best regards,
>
> *Hiren Popat*
>
> Software Engineer (Python Developer)
>
> On Tue, 21 Mar, 2023, 12:37 am pubpub-zz, ***@***.***>
> wrote:
>
>> @hirenpopat67 <https://github.com/hirenpopat67>
>> I've pushed a PR on your branch to show what I had in mind. What do you
>> think of it ?
>>
>> —
>> Reply to this email directly, view it on GitHub
>> <#1723 (comment)>, or
>> unsubscribe
>> <https://github.com/notifications/unsubscribe-auth/AQ32DD4PKGP3IMZERY6DNPDW5CTHZANCNFSM6AAAAAAV6IXZAI>
>> .
>> You are receiving this because you were mentioned.Message ID:
>> ***@***.***>
>>
>
|
@hirenpopat67 , can you complete test_generic.py in order to cover the new function |
I'm still not sure if the AnnotationObject / Are there any other attributes that all annotations have? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to fix mypy and also to cope with RGB on 0-255 ranges
pypdf/generic/_annotations.py
Outdated
try: | ||
from PIL import ImageColor as ImageColorLoaded | ||
except ImportError: | ||
ImageColorLoaded = None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to fix mypy
ImageColorLoaded = None | |
ImageColorLoaded = None # type: ignore |
pypdf/generic/_annotations.py
Outdated
if isinstance(border_color, str): | ||
if ImageColorLoaded is None: | ||
raise ImportError("PIL required but not installed") | ||
border_color = ImageColorLoaded.getrgb(border_color) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
border_color = ImageColorLoaded.getrgb(border_color) | |
border_color = (x/255.0 for x in ImageColorLoaded.getrgb(border_color)[:3]) |
for mypy
pypdf/generic/_annotations.py
Outdated
raise ImportError("PIL required but not installed") | ||
border_color = ImageColorLoaded.getrgb(border_color) | ||
if isinstance(border_color, (list, tuple)): | ||
border_color = ArrayObject([FloatObject(n) for n in border_color[:3]]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
border_color = ArrayObject([FloatObject(n) for n in border_color[:3]]) | |
border_color = ArrayObject([FloatObject(n if n<=1.0 else n/255.0) for n in border_color[:3]]) |
if the array provided is an array of RGB color
I am writing to suggest a small improvement to the existing implementation of the rectangle function. Currently, the function allows users to draw a rectangle on a PDF and specify the interior color of the rectangle using the interior_color argument. However, there is no option to specify the fill color of the rectangle. Rather than create set_border_colour function we can create colour color keyword argument to the rectangle function Here is an example of the updated function signature: @staticmethod I believe that this change would make the function more intuitive and user-friendly. Please let me know if you have any questions or concerns regarding this suggestion. Thank you for considering my suggestion. Best regards, Software Engineer (Python Developer) |
the approach I've proposed will be very efficient in this case adding the new function we will get:
but without extra mods
Do you agree ? |
@pubpub-zz Yes, I agree. Your proposed approach of using the AnnotationBuilder class to create annotations,while setting the border and interior colors, is efficient and should work well in this case. The method chaining syntax you've used makes the code concise and easy to read. |
based on #1740, I recommend |
I think we are now having a way more complicated discussion within this PR. Let's move that to #1741 :-) |
@MartinThoma |
…ectangle function