-
Notifications
You must be signed in to change notification settings - Fork 19
Add the PDF Image loader #1
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
Changes from all commits
9701e6c
81f8e24
b30cc83
2aef3bd
dbf807a
3f591fb
db07c00
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| """ | ||
|
|
||
| Dataclass for doing stuff on images of pages of a document | ||
|
|
||
| @kylel, @shannons | ||
|
|
||
| """ | ||
|
|
||
| import base64 | ||
| from io import BytesIO | ||
|
|
||
| from PIL import Image | ||
|
|
||
| # Monkey patch the PIL.Image methods to add base64 conversion | ||
|
|
||
| def tobase64(self): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we want to support 2 forms: base64 and proper image file that one can download & view |
||
| # Ref: https://stackoverflow.com/a/31826470 | ||
| buffered = BytesIO() | ||
| self.save(buffered, format="PNG") | ||
| img_str = base64.b64encode(buffered.getvalue()) | ||
|
|
||
| return img_str | ||
|
|
||
| def frombase64(img_str): | ||
| # Use the same naming style as the original Image methods | ||
|
|
||
| buffered = BytesIO(base64.b64decode(img_str)) | ||
| img = Image.open(buffered) | ||
| return img | ||
|
|
||
| Image.Image.tobase64 = tobase64 # This is the method applied to individual Image classes | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. prefer we define our own Image class, inherit from PIL's Image class, and override the methods. |
||
| Image.frombase64 = frombase64 # This is bind to the module, used for loading the images | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| intervaltree=3.1.0 | ||
| intervaltree=3.1.0 | ||
| pdf2image=0.1.12 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| """ | ||
|
|
||
| Tests for PDF Image parser | ||
|
|
||
| @kylel | ||
|
|
||
|
|
||
| """ | ||
|
|
||
| import unittest | ||
|
|
||
| from PIL import ImageChops | ||
|
|
||
| from mmda.parsers.parser import BaseParser | ||
| from mmda.types.image import Image | ||
|
|
||
| class TestLoadPDFImages(unittest.TestCase): | ||
|
|
||
| @classmethod | ||
| def setUpClass(cls): | ||
| cls.pdf_path = "tests/fixtures/1903.10676.pdf" | ||
| cls.parser = BaseParser() | ||
|
|
||
| def test_load_image(self): | ||
| images = self.parser.load_images(self.pdf_path) | ||
|
|
||
| assert hasattr(images[0], "tobase64") | ||
|
|
||
| recovered_image = Image.frombase64(images[0].tobase64()) | ||
| diff = ImageChops.difference(recovered_image, images[0]) | ||
|
|
||
| assert diff.getbbox() is 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.
why the rename?