From f59b2e2dc7e657b2fa11d7eb2533864c2d6c68dd Mon Sep 17 00:00:00 2001 From: j-t-1 <120829237+j-t-1@users.noreply.github.com> Date: Mon, 2 Dec 2024 22:08:52 +0000 Subject: [PATCH 1/4] STY: Slight refactor of remove_images Remove duplication for easier readability. --- pypdf/_writer.py | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/pypdf/_writer.py b/pypdf/_writer.py index 463bc2988..c4fc0a672 100644 --- a/pypdf/_writer.py +++ b/pypdf/_writer.py @@ -2194,29 +2194,24 @@ def remove_images( Remove images from this output. Args: - to_delete : The type of images to be deleted + to_delete: The type of images to be deleted (default = all images types) """ if isinstance(to_delete, bool): to_delete = ImageType.ALL - i = ( - ( - ObjectDeletionFlag.XOBJECT_IMAGES - if to_delete & ImageType.XOBJECT_IMAGES - else ObjectDeletionFlag.NONE - ) - | ( - ObjectDeletionFlag.INLINE_IMAGES - if to_delete & ImageType.INLINE_IMAGES - else ObjectDeletionFlag.NONE - ) - | ( - ObjectDeletionFlag.DRAWING_IMAGES - if to_delete & ImageType.DRAWING_IMAGES - else ObjectDeletionFlag.NONE - ) - ) + + i = ObjectDeletionFlag.NONE + + if to_delete & ImageType.XOBJECT_IMAGES: + i |= ObjectDeletionFlag.XOBJECT_IMAGES + + if to_delete & ImageType.INLINE_IMAGES: + i |= ObjectDeletionFlag.INLINE_IMAGES + + if to_delete & ImageType.DRAWING_IMAGES + i |= ObjectDeletionFlag.DRAWING_IMAGES + for page in self.pages: self.remove_objects_from_page(page, i) From 3c36f992b3d0ec6ea7a14c08c892e0c39fb5e8b4 Mon Sep 17 00:00:00 2001 From: j-t-1 <120829237+j-t-1@users.noreply.github.com> Date: Mon, 2 Dec 2024 22:12:56 +0000 Subject: [PATCH 2/4] STY: Slight refactor of remove_images Increase readability. --- pypdf/_writer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pypdf/_writer.py b/pypdf/_writer.py index c4fc0a672..351017c1a 100644 --- a/pypdf/_writer.py +++ b/pypdf/_writer.py @@ -2209,7 +2209,7 @@ def remove_images( if to_delete & ImageType.INLINE_IMAGES: i |= ObjectDeletionFlag.INLINE_IMAGES - if to_delete & ImageType.DRAWING_IMAGES + if to_delete & ImageType.DRAWING_IMAGES: i |= ObjectDeletionFlag.DRAWING_IMAGES for page in self.pages: From 75e4de767f650112dee83dcc0b37f2d6afcc7ee0 Mon Sep 17 00:00:00 2001 From: j-t-1 <120829237+j-t-1@users.noreply.github.com> Date: Tue, 3 Dec 2024 09:11:08 +0000 Subject: [PATCH 3/4] STY: Slight refactor of remove_images Increase readability. --- pypdf/_writer.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pypdf/_writer.py b/pypdf/_writer.py index 351017c1a..c1c53c893 100644 --- a/pypdf/_writer.py +++ b/pypdf/_writer.py @@ -2203,6 +2203,9 @@ def remove_images( i = ObjectDeletionFlag.NONE + if to_delete & ImageType.IMAGES: + i |= to_delete & ObjectDeletionFlag.IMAGES + """ if to_delete & ImageType.XOBJECT_IMAGES: i |= ObjectDeletionFlag.XOBJECT_IMAGES @@ -2211,7 +2214,7 @@ def remove_images( if to_delete & ImageType.DRAWING_IMAGES: i |= ObjectDeletionFlag.DRAWING_IMAGES - + """ for page in self.pages: self.remove_objects_from_page(page, i) From 966ba1c1faba88a4e82728d0ed9e73f83db343e8 Mon Sep 17 00:00:00 2001 From: j-t-1 <120829237+j-t-1@users.noreply.github.com> Date: Tue, 3 Dec 2024 10:42:34 +0000 Subject: [PATCH 4/4] STY: Slight refactor of remove_images Increase readability. --- pypdf/_writer.py | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/pypdf/_writer.py b/pypdf/_writer.py index c1c53c893..1d9e84a38 100644 --- a/pypdf/_writer.py +++ b/pypdf/_writer.py @@ -2203,18 +2203,10 @@ def remove_images( i = ObjectDeletionFlag.NONE - if to_delete & ImageType.IMAGES: - i |= to_delete & ObjectDeletionFlag.IMAGES - """ - if to_delete & ImageType.XOBJECT_IMAGES: - i |= ObjectDeletionFlag.XOBJECT_IMAGES - - if to_delete & ImageType.INLINE_IMAGES: - i |= ObjectDeletionFlag.INLINE_IMAGES + for image in ("XOBJECT_IMAGES", "INLINE_IMAGES", "DRAWING_IMAGES"): + if to_delete & ImageType[image]: + i |= ObjectDeletionFlag[image] - if to_delete & ImageType.DRAWING_IMAGES: - i |= ObjectDeletionFlag.DRAWING_IMAGES - """ for page in self.pages: self.remove_objects_from_page(page, i)