fzliu
/
emulate-sign-and-scan
copied
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Readme
Files and versions
73 lines
2.1 KiB
73 lines
2.1 KiB
|
4 years ago
|
|
||
|
|
import subprocess
|
||
|
|
from typing import NamedTuple
|
||
|
|
|
||
|
|
from towhee.operator.base import Operator
|
||
|
|
from towhee.types import Image
|
||
|
|
|
||
|
|
|
||
|
|
class EmulateSignAndScan(Operator):
|
||
|
|
"""
|
||
|
|
A one line summary of this class.
|
||
|
|
|
||
|
|
Args:
|
||
|
|
init_arg_1 (`xxx`):
|
||
|
|
This argument is ...
|
||
|
|
"""
|
||
|
|
def __init__(self) -> None:
|
||
|
|
super().__init__()
|
||
|
|
|
||
|
|
def __call__(self, doc_path: str, out_path: str, sig_img: Image = None, x_off: int = 0, y_off: int = 0) -> NamedTuple('Outputs', [('out_path', str)]):
|
||
|
|
"""
|
||
|
|
A one line summary of this function.
|
||
|
|
|
||
|
|
Args:
|
||
|
|
doc_path (`str`):
|
||
|
|
Document which needs to be signed and scanned in PDF format.
|
||
|
|
|
||
|
|
out_path (`str`):
|
||
|
|
Output document path.
|
||
|
|
|
||
|
|
sig_img (`towhee.types.Image`):
|
||
|
|
Image containing the cropped signature
|
||
|
|
|
||
|
|
x_off (`int`):
|
||
|
|
`x` offset value (used to place the signature in the document.).
|
||
|
|
|
||
|
|
y_off (`int`):
|
||
|
|
`y` offset_value (used to place the signature in the document.)
|
||
|
|
|
||
|
|
Returns:
|
||
|
|
(`Tuple[('result_1', xxx)]`)
|
||
|
|
A tuple with one values, which is ...
|
||
|
|
"""
|
||
|
|
|
||
|
|
# step 1: copy signature image into document
|
||
|
|
if sig_img:
|
||
|
|
cv2.imwrite(sig_img.to_ndarray(), "/tmp/pdf2scan-sig-img.jpg")
|
||
|
|
cmd = ["composite",
|
||
|
|
"-density", "150",
|
||
|
|
"-gravity", "NorthWest",
|
||
|
|
"-geometry", "+{0}+{1}".format(x_off, y_off),
|
||
|
|
"/tmp/pdf2scan-sig-img.jpg",
|
||
|
|
doc_path,
|
||
|
|
out_path]
|
||
|
|
else:
|
||
|
|
cmd = ["cp", doc_path, out_path]
|
||
|
|
subprocess.call(cmd)
|
||
|
|
|
||
|
|
# step 2: "scanify" the document
|
||
|
|
cmd = ["convert",
|
||
|
|
"-density", "150",
|
||
|
|
out_path,
|
||
|
|
"-rotate", "-0.66",
|
||
|
|
"-attenuate", "0.2",
|
||
|
|
"+noise", "Multiplicative",
|
||
|
|
"-colorspace", "Gray",
|
||
|
|
"-blur", "3x0.5",
|
||
|
|
out_path]
|
||
|
|
subprocess.call(cmd)
|
||
|
|
|
||
|
|
Output = NamedTuple('Output', [('out_path', str)])
|
||
|
|
return Output(out_path)
|