From 2d737cf2ecf75e848a5d177c6a8132a11d74ed29 Mon Sep 17 00:00:00 2001 From: "E. McConville" Date: Fri, 6 Aug 2021 19:58:36 -0500 Subject: [PATCH] Fixed Image.background_color when image not loaded --- docs/changes.rst | 1 + wand/image.py | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/docs/changes.rst b/docs/changes.rst index 93330d6f..c5d9783b 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -20,6 +20,7 @@ Unreleased. - Added :meth:`Image.image_set() ` method. - Added :meth:`Image.image_swap() ` method. - Fixed sub-image extraction on read. [:issue:`532`] + - Fixed :attr:`~wand.image.BaseImage.background_color` attribute when image was not read. - [DOC] Completed :doc:`Distortion <./guide/distortion>` guide. [:issue:`534`] - [DOC] Added :doc:`Morphology <./guide/morphology>` guide. diff --git a/wand/image.py b/wand/image.py index 3cfdaf65..2671f081 100644 --- a/wand/image.py +++ b/wand/image.py @@ -1476,9 +1476,15 @@ def background_color(self): .. versionadded:: 0.1.9 + .. versionchanged:: 0.6.7 + Allow property to be set before image read. """ pixel = library.NewPixelWand() - result = library.MagickGetImageBackgroundColor(self.wand, pixel) + if library.MagickGetNumberImages(self.wand): + result = library.MagickGetImageBackgroundColor(self.wand, pixel) + else: + pixel = library.MagickGetBackgroundColor(self.wand) + result = True if not result: # pragma: no cover self.raise_exception() else: @@ -1493,10 +1499,12 @@ def background_color(self, color): color = Color(color) assertions.assert_color(color=color) with color: - result = library.MagickSetImageBackgroundColor(self.wand, - color.resource) - if not result: # pragma: no cover - self.raise_exception() + # Only set the image background if an image was loaded. + if library.MagickGetNumberImages(self.wand): + result = library.MagickSetImageBackgroundColor(self.wand, + color.resource) + if not result: # pragma: no cover + self.raise_exception() # Also set the image stack. result = library.MagickSetBackgroundColor(self.wand, color.resource)