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

Draw does not work on a SingleImage #289

Closed
rkpatel33 opened this Issue May 21, 2016 · 1 comment

Comments

2 participants
@rkpatel33
Copy link

rkpatel33 commented May 21, 2016

I have a multi-page PDF on which I want to draw some annotation, however, when I try to call draw.draw(singleimage) Wand throws an error:

TypeError: image must be a wand.image.Image instance, not <wand.sequence.SingleImage: 947cceb (612x792)>

Here is my sample code:

image = Image(filename='Acme.pdf')
singleimage = image.sequence[0]

draw = Drawing()
draw.stroke_color = Color('transparent')
draw.stroke_width = 1
draw.fill_opacity = 0.10
draw.fill_color = Color('rgba(0, 0, 255, 0.10)')
draw.rectangle(left=0, top=0, right=200, bottom=792)

draw.draw(singleimage)

Is this a bug or is there a reason this not possible? If you simply call the same code on the whole document image instead with draw.draw(image), it draws the rectangle on the last page, which is also odd behavior.

PDF is attached.

Acme.pdf

@emcconville

This comment has been minimized.

Copy link
Owner

emcconville commented May 23, 2016

Why not? The current wand.drawing.Drawing.draw method validates for an instances of wand.image.Image, but there's no reason to validated for wand.image.BaseImage instead. (but I might be wrong)

Workaround

Create a new instance of wand.image.Image from sequence slice.

from wand.image import Image
from wand.color import Color
from wand.drawing import Drawing

with Image(filename='Acme.pdf') as image:
    with Image(image.sequence[0]) as singleimage:
        with Drawing() as draw:
            draw.stroke_color = Color('transparent')
            draw.stroke_width = 1
            draw.fill_opacity = 0.10
            draw.fill_color = Color('rgba(0, 0, 255, 0.10)')
            draw.rectangle(left=0, top=0, right=200, bottom=792)
            draw.draw(singleimage)

Another workaround

Extend wand.drawing.Drawing.draw to validate against BaseImage. Untested

from wand.drawing import Drawing
from wand.image import BaseImage

class MyDrawing(Drawing):
    def draw(self, image):
        if not isinstance(image, BaseImage):
            raise TypeError('image must be a wand.image.BaseImage instance, not '
                            + repr(image))
        res = library.MagickDrawImage(image.wand, self.resource)
        if not res:
            self.raise_exception()

@emcconville emcconville added this to the 0.5.0 milestone Dec 19, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment