def _test_png_predictors(self, columns, encoded_data, pixel_data):
		obj = EncodedObject(encoded_data, Filter.Uncompressed, columns = columns, predictor = Predictor.PNGPredictionOptimum)
		obj_decode = obj.decode()
		self.assertEqual(len(obj_decode), len(pixel_data))
		for line_offset in range(0, len(pixel_data), columns):
			self.assertEqual(obj_decode[line_offset : line_offset + columns], pixel_data[line_offset : line_offset + columns], "Row %d" % (line_offset // columns))
		self.assertEqual(obj_decode, pixel_data)
 def _encode_image(cls, image_filename, lossless):
     if lossless:
         img = PnmPicture.read_file(image_filename)
         if img.img_format == PnmPictureFormat.Bitmap:
             # PDFs use exactly inverted syntax for 1-bit images
             img.invert()
         imgdata = EncodedObject.create(img.data)
         (colorspace, bits_per_component) = {
             PnmPictureFormat.Bitmap: (PDFImageColorSpace.DeviceGray, 1),
             PnmPictureFormat.Graymap: (PDFImageColorSpace.DeviceGray, 8),
             PnmPictureFormat.Pixmap: (PDFImageColorSpace.DeviceRGB, 8),
         }[img.img_format]
         width = img.width
         height = img.height
     else:
         with open(image_filename, "rb") as f:
             imgdata = EncodedObject(encoded_data=f.read(),
                                     filtering=Filter.DCTDecode)
         (width, height, colorspace,
          bits_per_component) = cls._get_image_geometry(image_filename)
         colorspace = {
             "Gray": PDFImageColorSpace.DeviceGray,
             "sRGB": PDFImageColorSpace.DeviceRGB,
         }[colorspace]
     return PDFImage(width=width,
                     height=height,
                     colorspace=colorspace,
                     bits_per_component=bits_per_component,
                     imgdata=imgdata,
                     inverted=False)
Exemple #3
0
 def test_flate_decompress(self):
     compressed_data = bytes.fromhex(
         "78 9c 73 cb cf 4f 4a 2c  02 00 07 eb 02 5a")
     obj = EncodedObject(compressed_data, Filter.FlateDecode)
     self.assertEqual(obj.decode(), b"Foobar")
Exemple #4
0
 def test_no_decompress(self):
     uncompressed_data = b"Foobar"
     obj = EncodedObject(uncompressed_data, Filter.Uncompressed)
     self.assertEqual(obj.decode(), b"Foobar")
	def put_image(self, pdfimage):
		if pdfimage.image_format not in [ "JPEG", "RGB", "GRAY" ]:
			raise UnsupportedError("PDF can only handle JPEG, RGB or GRAY image formats, but %s was supplied." % (pdfimage.image_format))

		custom_metadata = {
			"resolution_dpi":	list(pdfimage.resolution_dpi),
			"comment":			pdfimage.comment,
		}
		image = self.pdf.new_object({
			PDFName("/Type"):				PDFName("/XObject"),
			PDFName("/Subtype"):			PDFName("/Image"),
			PDFName("/Interpolate"):		True,
			PDFName("/Width"):				pdfimage.dimensions.width,
			PDFName("/Height"):				pdfimage.dimensions.height,
			PDFName("/CustomMetadata"):		PDFString(json.dumps(custom_metadata)),
		})
		if pdfimage.image_format == "JPEG":
			image.set_stream(EncodedObject(encoded_data = pdfimage.data, filtering = Filter.DCTDecode))
		elif pdfimage.image_format in [ "RGB", "GRAY" ]:
			image.set_stream(EncodedObject.create(pdfimage.data, compress = True))
		else:
			raise NotImplementedError(pdfimage.image_format)

		if pdfimage.pixel_format == PixelFormat.RGB:
			image.content[PDFName("/ColorSpace")] = PDFName("/DeviceRGB")
			image.content[PDFName("/BitsPerComponent")] = 8
		elif pdfimage.pixel_format == PixelFormat.Grayscale:
			image.content[PDFName("/ColorSpace")] = PDFName("/DeviceGray")
			image.content[PDFName("/BitsPerComponent")] = 8
		elif pdfimage.pixel_format == PixelFormat.BlackWhite:
			image.content[PDFName("/ColorSpace")] = PDFName("/DeviceGray")
			image.content[PDFName("/BitsPerComponent")] = 1
		else:
			raise NotImplementedError(pdfimage.pixel_format)

		image_extents_mm = pdfimage.extents_mm
		image_scale_x = self.printable_area_mm.width / image_extents_mm.width
		image_scale_y = self.printable_area_mm.height / image_extents_mm.height
		image_scalar = min(image_scale_x, image_scale_y)
		if image_scalar > 1:
			# Never enlarge
			image_scalar = 1

		printed_size_mm = image_extents_mm * image_scalar
		page_dimensions_mm = self._hl_page.extents_mm
		offset_mm = (page_dimensions_mm - printed_size_mm) / 2
		offset_dots = offset_mm * 72 / 25.4
		printed_size_dots = printed_size_mm * 72 / 25.4
		params = {
			"xoffset":	offset_dots.width,
			"yoffset":	offset_dots.height,
			"xscalar":	printed_size_dots.width,
			"yscalar":	printed_size_dots.height,
		}

		self._hl_page.append_stream(textwrap.dedent("""\
		%(xscalar)f 0 0 %(yscalar)f %(xoffset)f %(yoffset)f cm
		/Img Do
		""" % (params)))

		page_obj = self._hl_page.page_obj
		if not PDFName("/Resources") in page_obj.content:
			page_obj.content[PDFName("/Resources")] = { }

		if not PDFName("/XObject") in page_obj.content[PDFName("/Resources")]:
			page_obj.content[PDFName("/Resources")][PDFName("/XObject")] = { }

		page_obj.content[PDFName("/Resources")][PDFName("/XObject")][PDFName("/Img")] = image.xref