2020-05-07 03:27:39 -07:00
|
|
|
# © 2020 James R Barlow: https://github.com/jbarlow83
|
|
|
|
#
|
2020-08-05 00:14:17 -07:00
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
2020-05-07 03:27:39 -07:00
|
|
|
#
|
2020-08-05 00:14:17 -07:00
|
|
|
# The above copyright notice and this permission notice shall be included in all
|
|
|
|
# copies or substantial portions of the Software.
|
2020-05-07 03:27:39 -07:00
|
|
|
#
|
2020-08-05 00:14:17 -07:00
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
# SOFTWARE.
|
2020-05-07 03:27:39 -07:00
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
from ocrmypdf import hookimpl
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
@hookimpl
|
|
|
|
def add_options(parser):
|
|
|
|
parser.add_argument('--grayscale-ocr', action='store_true')
|
|
|
|
|
|
|
|
|
|
|
|
@hookimpl
|
|
|
|
def prepare(options):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@hookimpl
|
|
|
|
def validate(pdfinfo, options):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@hookimpl
|
|
|
|
def filter_ocr_image(page, image):
|
|
|
|
if page.options.grayscale_ocr:
|
|
|
|
log.info("graying")
|
|
|
|
return image.convert('L')
|
|
|
|
return image
|
|
|
|
|
|
|
|
|
|
|
|
@hookimpl
|
|
|
|
def filter_page_image(page, image_filename):
|
|
|
|
output = image_filename.with_suffix('.jpg')
|
|
|
|
with Image.open(image_filename) as im:
|
|
|
|
im.save(output)
|
|
|
|
return output
|