def imagevisual(self, image_path, no, udct, nsig, page): x1, y1, x2, y2 = udct.get(b'signaturebox', (0, 0, 0, 0)) annotation = Image( Location(x1=x1, y1=y1, x2=x2, y2=y2, page=0), Appearance(image=image_path), ) pdfa = annotation.as_pdf_object(identity(), page=None) pdfar = b'[%d %d %d %d]' % tuple(pdfa.Rect) pdfao = pdfa.AP.N visual, nav = self.makeannotation(pdfao, no + 4) obj = [ self.makeobj( no + 3, b''' /Type /Annot /Subtype %s /Rect %s /AP <</N %d 0 R>> /F 4 /P %d 0 R /FT /Sig %s /T(Signature%d) /V %d 0 R ''' % (b'/Widget' if udct.get(b'sigbutton', False) else b'/Square', pdfar, no + 4, page, b'/SM(TabletPOSinline)' if udct.get( b'sigbutton', False) else b'', nsig, nav + 1)), visual, ] return b''.join(obj), nav
def _add_explicit_image_annotation(self, a): """Add an image annotation using ContentStream commands instead of the Image type's commands. This is testing that the external XObjects API works, and that images can be embedded inside other, more complex annotations. """ x1, y1, x2, y2 = 10, 310, 50, 350 location = Location(x1=x1, y1=y1, x2=x2, y2=y2, page=0) content_stream = ContentStream([ StrokeColor(1, 0, 0), Rect(x1, y1, x2 - x1, y2 - y1), Save(), # The image is inside an outer rectangle CTM(Image.get_ctm(x1 + 10, y1 + 10, x2 - 10, y2 - 10)), XObject('MyXObject'), Restore(), Stroke(), ]) appearance = Appearance( appearance_stream=content_stream, xobjects={ 'MyXObject': Image.make_image_xobject(PNG_FILES[0]), }, ) a.add_annotation( 'square', location=location, appearance=appearance, )
def test_as_pdf_object(self): x1, y1, x2, y2 = 10, 20, 100, 200 image = Image( location=Location(x1=x1, y1=y1, x2=x2, y2=y2, page=0), appearance=Appearance(stroke_width=0, image=PNG_FILES[0]), ) obj = image.as_pdf_object(identity(), page=None) # Appearance stream should place the Image correctly assert obj.AP.N.stream == ( 'q 0 0 0 RG 0 w 10 20 90 180 re 90 0 0 180 10 20 cm /Image Do Q') assert obj.Rect == [x1, y1, x2, y2] assert obj.AP.N.BBox == [x1, y1, x2, y2] assert obj.AP.N.Matrix == translate(-x1, -y1)
def test_convert_noop(self): image, smask = Image.convert_to_compatible_image( self.grayscale_image, 'PNG', ) assert image.mode == GRAYSCALE_MODE assert smask is None
def test_convert_grayscale_with_alpha(self): image = add_alpha(self.grayscale_image) assert image.mode == GRAYSCALE_ALPHA_MODE appropriate_image, smask = Image.convert_to_compatible_image( image, 'PNG', ) assert appropriate_image.mode == GRAYSCALE_MODE assert smask.Width == image.size[0] assert smask.Height == image.size[1]