-
Notifications
You must be signed in to change notification settings - Fork 227
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 cmyk support #756
Add cmyk support #756
Conversation
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## master #756 +/- ##
==========================================
- Coverage 93.25% 93.08% -0.17%
==========================================
Files 27 27
Lines 7312 7293 -19
Branches 1322 1324 +2
==========================================
- Hits 6819 6789 -30
- Misses 308 315 +7
- Partials 185 189 +4
☔ View full report in Codecov by Sentry. |
Hi @devdev29 and welcome! 😊 This seems promising! On I think we currently do not have any CMYK image among
I have to admit that I don't know much neither about CMYK 😅 Regarding your code and your question, the way you handle the 4 channels of CMYK seems fine to me. |
Ohk then, I was going to write a test for the addition but I just wanted to confirm whether I was approaching the issue correctly, I'll be sure to add the test by today evening, also about that CMYK image file I just converted an existing jpeg to cmyk using pillow for a test script so |
So I wrote the test for inserting the image but the test seems to be generating a different hash for every new pdf file I try even though the each file is the exact same, another issue I've run into is that only TIFF files in the cmyk format work jpegs produce weird effects when inserted into the document. I'll try to fix the jpeg issue asap but I don't know why the test is failing. |
That is strange...
Alright. |
fpdf2 doesn't currently support pixel transparency, so the A(lpha) channel gets dropped (as eg. in converting from RGBA to RGB). |
Sure here's the code - https://pastebin.com/cu0STbcq edit - P.S. - sorry for the late reply got a bit busy with school 😅 |
Actually fpdf2 DOES support pixel transparency, I was confused about the context of some related code in But still, your code addition in the same place: if img.mode == "CMYK":
img = img.convert("CMYK") is a null-operation that wastes time for doing nothing. |
…into add_cmyk_support
Yea I realised that a few days after the commit and removed the code, but thanks anyway for pointing it out :), I've also fixed the jpg issue, turns out the condition check in the image parsing file which extracts the info for images was setting the channel and other info for cmyk images the same as rgb, which seemed to be running it through the wrong |
Thank you, and no worries 😊 I'd be happy to have a look, but could you please add your reference |
I have tried to reproduce your issue using the following code: #!/usr/bin/env python3
from datetime import datetime, timezone
from fpdf import FPDF
pdf = FPDF()
pdf.creation_date = datetime(1969, 12, 31, 19, 00, 00).replace(tzinfo=timezone.utc)
pdf.add_page()
pdf.image("flower-cmyk.jpg") # from https://stackoverflow.com/a/59854479/636849
pdf.add_page()
pdf.image("test/image/image_types/test.tiff", w=pdf.epw)
pdf.output("issue_756.pdf") And I get a consistant sha256 hash:
Note: I have latest So I guess your problem may be with the The good news is that the CMYK JPEG image seems to be correctly embedded in the PDF! |
Yep either that or there's some issue with my pillow installation.
That's great! It's working on my pdf viewers as well. I'll just add the test to the PR and then it can be merged! 🎉 |
Hi @devdev29! How is it going regarding this PR? 😊 |
Hi Lucas, sorry for abandoning the PR so suddenly, I'm still facing some issues with CMYK TIFFs, will definitely fix them and make a final commit by tomorrow. Sorry for taking so long on such a simple PR. |
No worries! I'm just happy if you plan to finish this valuable work 😊 |
Heya Lucas, I've figured out the problem with the TIFFs and JPGs, apparently the dct decode stream for CMYK JPEGs is inverted and the decode array needs to be inverted from the default for CMYK i.e. Here's the answer that made me realise what was going on with the different image formats, hope you find it helpful - |
Well done @devdev29! The GitHub Actions CI pipeline currently fails, That should be easily resolved by running the new unit tests once while passing If you have time to fix, go for it. |
… flag in assert_pdf_equal in test_insert_images.py
All done and dusted. |
Merged! |
@allcontributors please add @devdev29 for code |
I've put up a pull request to add @devdev29! 🎉 |
Add CMYK support to image conversion in pdf
tries to fix #711
I've just added a basic condition in the image parser file to check if the mode is specified to be CMYK and to convert the image to the appropriate format if that is the case. This is my first time working with CMYK so I'd appreciate it if the reviewer could tell me whether I need to treat the Key channel in CMYK like the Alpha channel in RGBA.
Checklist:
The GitHub pipeline is OK (green),
meaning that both
pylint
(static code analyzer) andblack
(code formatter) are happy with the changes of this PR.A unit test is covering the code added / modified by this PR
This PR is ready to be merged, this should probably be a draft pr
In case of a new feature, docstrings have been added, with also some documentation in the
docs/
folderA mention of the change is present in
CHANGELOG.md
By submitting this pull request, I confirm that my contribution is made under the terms of the GNU LGPL 3.0 license.