Refactor xy-pair for resolution to tuple

This commit is contained in:
James R. Barlow
2020-04-16 15:38:33 -07:00
parent 4581027246
commit 57771f06a3
9 changed files with 57 additions and 76 deletions
+15 -18
View File
@@ -201,12 +201,12 @@ def validate_pdfinfo_options(context):
def get_page_dpi(pageinfo, options):
"Get the DPI when nonsquare DPI is tolerable"
xres = max(
pageinfo.xres or VECTOR_PAGE_DPI,
pageinfo.xyres[0] or VECTOR_PAGE_DPI,
options.oversample or 0,
VECTOR_PAGE_DPI if pageinfo.has_vector else 0,
)
yres = max(
pageinfo.yres or VECTOR_PAGE_DPI,
pageinfo.xyres[1] or VECTOR_PAGE_DPI,
options.oversample or 0,
VECTOR_PAGE_DPI if pageinfo.has_vector else 0,
)
@@ -215,8 +215,8 @@ def get_page_dpi(pageinfo, options):
def get_page_square_dpi(pageinfo, options):
"Get the DPI when we require xres == yres, scaled to physical units"
xres = pageinfo.xres or 0
yres = pageinfo.yres or 0
xres = pageinfo.xyres[0] or 0
yres = pageinfo.xyres[1] or 0
userunit = pageinfo.userunit or 1
return float(
max(
@@ -232,8 +232,8 @@ def get_canvas_square_dpi(pageinfo, options):
"""Get the DPI when we require xres == yres, in Postscript units"""
return float(
max(
(pageinfo.xres) or VECTOR_PAGE_DPI,
(pageinfo.yres) or VECTOR_PAGE_DPI,
(pageinfo.xyres[0]) or VECTOR_PAGE_DPI,
(pageinfo.xyres[1]) or VECTOR_PAGE_DPI,
VECTOR_PAGE_DPI if pageinfo.has_vector else 0,
options.oversample or 0,
)
@@ -321,9 +321,8 @@ def rasterize_preview(input_file, page_context):
ghostscript.rasterize_pdf(
input_file,
output_file,
xres=canvas_dpi,
yres=canvas_dpi,
raster_device='jpeggray',
xyres=(canvas_dpi, canvas_dpi),
page_dpi=(page_dpi, page_dpi),
pageno=page_context.pageinfo.pageno + 1,
)
@@ -436,9 +435,8 @@ def rasterize(
ghostscript.rasterize_pdf(
input_file,
output_file,
xres=canvas_dpi,
yres=canvas_dpi,
raster_device=device,
xyres=(canvas_dpi, canvas_dpi),
page_dpi=(page_dpi, page_dpi),
pageno=pageinfo.pageno + 1,
rotation=correction,
@@ -488,8 +486,7 @@ def create_ocr_image(image, page_context):
# pink = ImageColor.getcolor('#ff0080', im.mode)
draw = ImageDraw.ImageDraw(im)
xres, yres = im.info['dpi']
log.debug('resolution %r %r' % (xres, yres))
log.debug('resolution %r', im.info['dpi'])
if not options.force_ocr:
# Do not mask text areas when forcing OCR, because we need to OCR
@@ -505,12 +502,12 @@ def create_ocr_image(image, page_context):
# without regard whatever resolution is in pageinfo (may differ or
# be None)
bbox = [float(v) for v in textarea]
xscale, yscale = float(xres) / 72.0, float(yres) / 72.0
xyscale = tuple(float(coord) / 72.0 for coord in im.info['dpi'])
pixcoords = [
bbox[0] * xscale,
im.height - bbox[3] * yscale,
bbox[2] * xscale,
im.height - bbox[1] * yscale,
bbox[0] * xyscale[0],
im.height - bbox[3] * xyscale[1],
bbox[2] * xyscale[0],
im.height - bbox[1] * xyscale[1],
]
pixcoords = [int(round(c)) for c in pixcoords]
log.debug('blanking %r', pixcoords)
@@ -524,7 +521,7 @@ def create_ocr_image(image, page_context):
del draw
# Pillow requires integer DPI
dpi = round(xres), round(yres)
dpi = tuple(round(coord) for coord in im.info['dpi'])
im.save(output_file, dpi=dpi)
return output_file
+6 -7
View File
@@ -135,32 +135,31 @@ def extract_text(input_file, pageno=1):
def rasterize_pdf(
input_file,
output_file,
xres,
yres,
*,
raster_device,
xyres,
pageno=1,
page_dpi=None,
rotation=None,
filter_vector=False,
):
"""Rasterize one page of a PDF at resolution (xres, yres) in canvas units.
"""Rasterize one page of a PDF at resolution xyres in canvas units.
The image is sized to match the integer pixels dimensions implied by
(xres, yres) even if those numbers are noninteger. The image's DPI will
(xyres[0], xyres[1]) even if those numbers are noninteger. The image's DPI will
be overridden with the values in page_dpi.
:param input_file: pathlike
:param output_file: pathlike
:param xres: resolution at which to rasterize page
:param yres:
:param raster_device:
:param xyres: resolution at which to rasterize page
:param pageno: page number to rasterize (beginning at page 1)
:param page_dpi: resolution tuple (x, y) overriding output image DPI
:param rotation: 0, 90, 180, 270: clockwise angle to rotate page
:param filter_vector: if True, remove vector graphics objects
:return:
"""
res = round(xres, 6), round(yres, 6)
res = round(xyres[0], 6), round(xyres[1], 6)
if not page_dpi:
page_dpi = res
+15 -21
View File
@@ -356,12 +356,11 @@ class ImageInfo:
return self._enc
@property
def xres(self):
return _get_dpi(self._shorthand, (self._width, self._height))[0]
@property
def yres(self):
return _get_dpi(self._shorthand, (self._width, self._height))[1]
def xyres(self):
return (
_get_dpi(self._shorthand, (self._width, self._height))[0],
_get_dpi(self._shorthand, (self._width, self._height))[1],
)
def __repr__(self):
class_locals = {
@@ -371,7 +370,7 @@ class ImageInfo:
}
return (
"<ImageInfo '{name}' {type_} {width}x{height} {color} "
"{comp} {bpc} {enc} {xres}x{yres}>"
"{comp} {bpc} {enc} {xyres}>"
).format(**class_locals)
@@ -607,9 +606,9 @@ def _pdf_get_pageinfo(pdf, pageno: int, infile: PathLike, xmltext: str):
pageinfo['images'] = [im for im in contentsinfo if isinstance(im, ImageInfo)]
if pageinfo['images']:
xres = Decimal(max(image.xres for image in pageinfo['images']))
yres = Decimal(max(image.yres for image in pageinfo['images']))
pageinfo['xres'], pageinfo['yres'] = xres, yres
xres = Decimal(max(image.xyres[0] for image in pageinfo['images']))
yres = Decimal(max(image.xyres[1] for image in pageinfo['images']))
pageinfo['xyres'] = xres, yres
pageinfo['width_pixels'] = int(round(xres * pageinfo['width_inches']))
pageinfo['height_pixels'] = int(round(yres * pageinfo['height_inches']))
@@ -679,11 +678,11 @@ class PageInfo:
@property
def width_pixels(self):
return int(round(self.width_inches * self.xres))
return int(round(self.width_inches * self.xyres[0]))
@property
def height_pixels(self):
return int(round(self.height_inches * self.yres))
return int(round(self.height_inches * self.xyres[1]))
@property
def rotation(self):
@@ -723,12 +722,8 @@ class PageInfo:
)
@property
def xres(self):
return self._pageinfo.get('xres', None)
@property
def yres(self):
return self._pageinfo.get('yres', None)
def xyres(self):
return self._pageinfo.get('xyres', (0, 0))
@property
def userunit(self):
@@ -743,14 +738,13 @@ class PageInfo:
def __repr__(self):
return (
'<PageInfo ' 'pageno={} {}"x{}" rotation={} res={}x{} has_text={}>'
'<PageInfo ' 'pageno={} {}"x{}" rotation={} res={} has_text={}>'
).format(
self.pageno,
self.width_inches,
self.height_inches,
self.rotation,
self.xres,
self.yres,
self.xyres,
self.has_text,
)