mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-11-10 19:56:47 +03:00
issue #2959: rename pdfParser.py to PdfParser.py
This commit is contained in:
parent
9be8d669f9
commit
c15a0b2fce
|
@ -1,5 +1,5 @@
|
|||
from helper import unittest, PillowTestCase, hopper
|
||||
from PIL import Image, pdfParser
|
||||
from PIL import Image, PdfParser
|
||||
import io
|
||||
import os
|
||||
import os.path
|
||||
|
@ -19,7 +19,7 @@ class TestFilePdf(PillowTestCase):
|
|||
# Assert
|
||||
self.assertTrue(os.path.isfile(outfile))
|
||||
self.assertGreater(os.path.getsize(outfile), 0)
|
||||
with pdfParser.PdfParser(outfile) as pdf:
|
||||
with PdfParser.PdfParser(outfile) as pdf:
|
||||
if kwargs.get("append_images", False) or kwargs.get("append", False):
|
||||
self.assertGreater(len(pdf.pages), 1)
|
||||
else:
|
||||
|
@ -106,10 +106,10 @@ class TestFilePdf(PillowTestCase):
|
|||
|
||||
def test_pdf_open(self):
|
||||
# fail on a buffer full of null bytes
|
||||
self.assertRaises(pdfParser.PdfFormatError, pdfParser.PdfParser, buf=bytearray(65536))
|
||||
self.assertRaises(PdfParser.PdfFormatError, PdfParser.PdfParser, buf=bytearray(65536))
|
||||
|
||||
# make an empty PDF object
|
||||
with pdfParser.PdfParser() as empty_pdf:
|
||||
with PdfParser.PdfParser() as empty_pdf:
|
||||
self.assertEqual(len(empty_pdf.pages), 0)
|
||||
self.assertEqual(len(empty_pdf.info), 0)
|
||||
self.assertFalse(empty_pdf.should_close_buf)
|
||||
|
@ -119,7 +119,7 @@ class TestFilePdf(PillowTestCase):
|
|||
pdf_filename = self.helper_save_as_pdf("RGB")
|
||||
|
||||
# open the PDF file
|
||||
with pdfParser.PdfParser(filename=pdf_filename) as hopper_pdf:
|
||||
with PdfParser.PdfParser(filename=pdf_filename) as hopper_pdf:
|
||||
self.assertEqual(len(hopper_pdf.pages), 1)
|
||||
self.assertTrue(hopper_pdf.should_close_buf)
|
||||
self.assertTrue(hopper_pdf.should_close_file)
|
||||
|
@ -127,14 +127,14 @@ class TestFilePdf(PillowTestCase):
|
|||
# read a PDF file from a buffer with a non-zero offset
|
||||
with open(pdf_filename, "rb") as f:
|
||||
content = b"xyzzy" + f.read()
|
||||
with pdfParser.PdfParser(buf=content, start_offset=5) as hopper_pdf:
|
||||
with PdfParser.PdfParser(buf=content, start_offset=5) as hopper_pdf:
|
||||
self.assertEqual(len(hopper_pdf.pages), 1)
|
||||
self.assertFalse(hopper_pdf.should_close_buf)
|
||||
self.assertFalse(hopper_pdf.should_close_file)
|
||||
|
||||
# read a PDF file from an already open file
|
||||
with open(pdf_filename, "rb") as f:
|
||||
with pdfParser.PdfParser(f=f) as hopper_pdf:
|
||||
with PdfParser.PdfParser(f=f) as hopper_pdf:
|
||||
self.assertEqual(len(hopper_pdf.pages), 1)
|
||||
self.assertTrue(hopper_pdf.should_close_buf)
|
||||
self.assertFalse(hopper_pdf.should_close_file)
|
||||
|
@ -149,13 +149,13 @@ class TestFilePdf(PillowTestCase):
|
|||
|
||||
def test_pdf_append(self):
|
||||
# make a PDF file
|
||||
pdf_filename = self.helper_save_as_pdf("RGB", producer="pdfParser")
|
||||
pdf_filename = self.helper_save_as_pdf("RGB", producer="PdfParser")
|
||||
|
||||
# open it, check pages and info
|
||||
with pdfParser.PdfParser(pdf_filename, mode="r+b") as pdf:
|
||||
with PdfParser.PdfParser(pdf_filename, mode="r+b") as pdf:
|
||||
self.assertEqual(len(pdf.pages), 1)
|
||||
self.assertEqual(len(pdf.info), 1)
|
||||
self.assertEqual(pdf.info.Producer, "pdfParser")
|
||||
self.assertEqual(pdf.info.Producer, "PdfParser")
|
||||
|
||||
# append some info
|
||||
pdf.info.Title = "abc"
|
||||
|
@ -167,7 +167,7 @@ class TestFilePdf(PillowTestCase):
|
|||
pdf.write_xref_and_trailer()
|
||||
|
||||
# open it again, check pages and info again
|
||||
with pdfParser.PdfParser(pdf_filename) as pdf:
|
||||
with PdfParser.PdfParser(pdf_filename) as pdf:
|
||||
self.assertEqual(len(pdf.pages), 1)
|
||||
self.assertEqual(len(pdf.info), 6)
|
||||
self.assertEqual(pdf.info.Title, "abc")
|
||||
|
@ -178,12 +178,12 @@ class TestFilePdf(PillowTestCase):
|
|||
mode_CMYK.save(pdf_filename, append=True, save_all=True, append_images=[mode_P])
|
||||
|
||||
# open the PDF again, check pages and info again
|
||||
with pdfParser.PdfParser(pdf_filename) as pdf:
|
||||
with PdfParser.PdfParser(pdf_filename) as pdf:
|
||||
self.assertEqual(len(pdf.pages), 3)
|
||||
self.assertEqual(len(pdf.info), 6)
|
||||
self.assertEqual(pdfParser.decode_text(pdf.info[b"Title"]), "abc")
|
||||
self.assertEqual(PdfParser.decode_text(pdf.info[b"Title"]), "abc")
|
||||
self.assertEqual(pdf.info.Title, "abc")
|
||||
self.assertEqual(pdf.info.Producer, "pdfParser")
|
||||
self.assertEqual(pdf.info.Producer, "PdfParser")
|
||||
self.assertEqual(pdf.info.Keywords, "qw)e\\r(ty")
|
||||
self.assertEqual(pdf.info.Subject, u"ghi\uABCD")
|
||||
|
||||
|
@ -192,7 +192,7 @@ class TestFilePdf(PillowTestCase):
|
|||
pdf_filename = self.helper_save_as_pdf("RGB", title="title", author="author", subject="subject", keywords="keywords", creator="creator", producer="producer")
|
||||
|
||||
# open it, check pages and info
|
||||
with pdfParser.PdfParser(pdf_filename) as pdf:
|
||||
with PdfParser.PdfParser(pdf_filename) as pdf:
|
||||
self.assertEqual(len(pdf.info), 6)
|
||||
self.assertEqual(pdf.info.Title, "title")
|
||||
self.assertEqual(pdf.info.Author, "author")
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
from helper import unittest, PillowTestCase
|
||||
|
||||
from PIL.pdfParser import IndirectObjectDef, IndirectReference, PdfBinary, PdfDict, PdfFormatError, PdfName, PdfParser, PdfStream, decode_text, encode_text, pdf_repr
|
||||
from PIL.PdfParser import IndirectObjectDef, IndirectReference, PdfBinary, PdfDict, PdfFormatError, PdfName, PdfParser, PdfStream, decode_text, encode_text, pdf_repr
|
||||
|
||||
|
||||
class TestPdfParser(PillowTestCase):
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
# Image plugin for PDF images (output only).
|
||||
##
|
||||
|
||||
from . import Image, ImageFile, ImageSequence, pdfParser
|
||||
from . import Image, ImageFile, ImageSequence, PdfParser
|
||||
import io
|
||||
|
||||
__version__ = "0.5"
|
||||
|
@ -55,9 +55,9 @@ def _save(im, fp, filename, save_all=False):
|
|||
producer = im.encoderinfo.get("producer", None)
|
||||
|
||||
if is_appending:
|
||||
existing_pdf = pdfParser.PdfParser(f=fp, filename=filename, mode="r+b")
|
||||
existing_pdf = PdfParser.PdfParser(f=fp, filename=filename, mode="r+b")
|
||||
else:
|
||||
existing_pdf = pdfParser.PdfParser(f=fp, filename=filename, mode="w+b")
|
||||
existing_pdf = PdfParser.PdfParser(f=fp, filename=filename, mode="w+b")
|
||||
|
||||
if title:
|
||||
existing_pdf.info.Title = title
|
||||
|
@ -123,26 +123,26 @@ def _save(im, fp, filename, save_all=False):
|
|||
|
||||
if im.mode == "1":
|
||||
filter = "ASCIIHexDecode"
|
||||
colorspace = pdfParser.PdfName("DeviceGray")
|
||||
colorspace = PdfParser.PdfName("DeviceGray")
|
||||
procset = "ImageB" # grayscale
|
||||
bits = 1
|
||||
elif im.mode == "L":
|
||||
filter = "DCTDecode"
|
||||
# params = "<< /Predictor 15 /Columns %d >>" % (width-2)
|
||||
colorspace = pdfParser.PdfName("DeviceGray")
|
||||
colorspace = PdfParser.PdfName("DeviceGray")
|
||||
procset = "ImageB" # grayscale
|
||||
elif im.mode == "P":
|
||||
filter = "ASCIIHexDecode"
|
||||
palette = im.im.getpalette("RGB")
|
||||
colorspace = [pdfParser.PdfName("Indexed"), pdfParser.PdfName("DeviceRGB"), 255, pdfParser.PdfBinary(palette)]
|
||||
colorspace = [PdfParser.PdfName("Indexed"), PdfParser.PdfName("DeviceRGB"), 255, PdfParser.PdfBinary(palette)]
|
||||
procset = "ImageI" # indexed color
|
||||
elif im.mode == "RGB":
|
||||
filter = "DCTDecode"
|
||||
colorspace = pdfParser.PdfName("DeviceRGB")
|
||||
colorspace = PdfParser.PdfName("DeviceRGB")
|
||||
procset = "ImageC" # color images
|
||||
elif im.mode == "CMYK":
|
||||
filter = "DCTDecode"
|
||||
colorspace = pdfParser.PdfName("DeviceCMYK")
|
||||
colorspace = PdfParser.PdfName("DeviceCMYK")
|
||||
procset = "ImageC" # color images
|
||||
else:
|
||||
raise ValueError("cannot save mode %s" % im.mode)
|
||||
|
@ -175,11 +175,11 @@ def _save(im, fp, filename, save_all=False):
|
|||
width, height = im.size
|
||||
|
||||
existing_pdf.write_obj(image_refs[pageNumber], stream=op.getvalue(),
|
||||
Type=pdfParser.PdfName("XObject"),
|
||||
Subtype=pdfParser.PdfName("Image"),
|
||||
Type=PdfParser.PdfName("XObject"),
|
||||
Subtype=PdfParser.PdfName("Image"),
|
||||
Width=width, # * 72.0 / resolution,
|
||||
Height=height, # * 72.0 / resolution,
|
||||
Filter=pdfParser.PdfName(filter),
|
||||
Filter=PdfParser.PdfName(filter),
|
||||
BitsPerComponent=bits,
|
||||
DecodeParams=params,
|
||||
ColorSpace=colorspace)
|
||||
|
@ -188,9 +188,9 @@ def _save(im, fp, filename, save_all=False):
|
|||
# page
|
||||
|
||||
existing_pdf.write_page(page_refs[pageNumber],
|
||||
Resources=pdfParser.PdfDict(
|
||||
ProcSet=[pdfParser.PdfName("PDF"), pdfParser.PdfName(procset)],
|
||||
XObject=pdfParser.PdfDict(image=image_refs[pageNumber])),
|
||||
Resources=PdfParser.PdfDict(
|
||||
ProcSet=[PdfParser.PdfName("PDF"), PdfParser.PdfName(procset)],
|
||||
XObject=PdfParser.PdfDict(image=image_refs[pageNumber])),
|
||||
MediaBox=[0, 0, int(width * 72.0 / resolution), int(height * 72.0 / resolution)],
|
||||
Contents=contents_refs[pageNumber]
|
||||
)
|
||||
|
@ -198,7 +198,7 @@ def _save(im, fp, filename, save_all=False):
|
|||
#
|
||||
# page contents
|
||||
|
||||
page_contents = pdfParser.make_bytes(
|
||||
page_contents = PdfParser.make_bytes(
|
||||
"q %d 0 0 %d 0 0 cm /image Do Q\n" % (
|
||||
int(width * 72.0 / resolution),
|
||||
int(height * 72.0 / resolution)))
|
||||
|
|
Loading…
Reference in New Issue
Block a user