mglib/mglib/tiff.py

52 lines
1.1 KiB
Python
Raw Normal View History

import os
import logging
from mglib.runcmd import run
2020-08-11 19:47:35 +02:00
from .conf import settings
logger = logging.getLogger(__name__)
2021-01-19 08:37:36 +01:00
def pdfname_from_tiffname(doc_url):
"""
Given tiff document url, will return
respective pdf file name. Returned
file name can be use used as destination
for tiff2pdf tool.
Returns a tuple (new_doc_url, new_filename).
new_doc_url - is new absolute path to the pdf file
new_filename - is new pdf filename
"""
# basename is filename + ext (no path)
basename = os.path.basename(doc_url)
base_root, base_ext = os.path.splitext(basename)
root, ext = os.path.splitext(doc_url)
new_doc_url = f"{root}.pdf"
2021-01-19 08:37:36 +01:00
return new_doc_url, f"{base_root}.pdf"
2021-01-19 11:30:01 +01:00
2021-01-19 08:37:36 +01:00
def convert_tiff2pdf(doc_url):
logger.debug(f"convert_tiff2pdf for {doc_url}")
new_doc_url, new_filename = pdfname_from_tiffname(
doc_url
)
logger.debug(
f"tiff2pdf source={doc_url} dest={new_doc_url}"
)
cmd = (
2020-08-11 19:47:35 +02:00
settings.BINARY_CONVERT,
doc_url,
new_doc_url,
)
run(cmd)
# returns new filename
2021-01-19 08:37:36 +01:00
return new_filename