Beispiel #1
0
def save_embed_fig(
    path: Union[Path, str],
    bbox_inches: Union[Number, str] = "tight",
    transparent: bool = True,
    attempt_fix_empty_white_space: bool = False,
    post_process_crop: bool = False,
    ax: Axes = None,
    suffix: str = ".pdf",
    **kwargs,
) -> None:
    """Save fig for latex pdf embedding"""

    if attempt_fix_empty_white_space:  # remove it
        # pyplot.gca().set_axis_off()
        # pyplot.axis('off') # this rows the rectangular frame
        # ax.get_xaxis().set_visible(False) # this removes the ticks and numbers for x axis
        # ax.get_yaxis().set_visible(False) # this removes the ticks and numbers for y axis
        if ax is None:
            ax = pyplot.gca()
        pyplot.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0)
        pyplot.margins(0, 0)
        ax.xaxis.set_major_locator(pyplot.NullLocator())
        ax.yaxis.set_major_locator(pyplot.NullLocator())

    """
clip_box = Bbox(((0,0),(300,300)))
for o in pyplot.findobj():
o.set_clip_on(True)
o.set_clip_box(clip_box)

"""

    if not isinstance(path, Path):
        path = Path(path)

    path_str = str(path.with_suffix(suffix))
    pyplot.savefig(
        path_str,
        bbox_inches=bbox_inches,
        transparent=transparent,
        **kwargs,
    )

    if (
        post_process_crop and suffix == ".pdf"
    ):  # Generally a good idea since matplotlib does not exclude invisible parts(eg. data points or anchors) of the plot.
        from pdfCropMargins import crop  # pip install pdfCropMargins

        crop(
            [
                "-p",
                "0",  # remove all percentage of old margin
                "-a",
                "-5",  # add 5bp margin around all
                path_str,
            ]
        )
def visualize_sp(zipped: Any) -> None:
    line, hide, output_dir = zipped

    step, qid, did, label, logit, attens = line.split("\t")
    splits = attens.split(" ")
    words, weights = splits[::2], [float(x) for x in splits[1::2]]
    assert len(words) == len(weights)
    for i in range(len(words)):
        if words[i] in hide:
            weights[i] = 0
    dummy = f"{output_dir}/.{step}-{qid}-{did}-{label}-{logit}.pdf"
    name = f"{output_dir}/{step}-{qid}-{did}-{label}-{logit}.pdf"
    colorize(words, weights, False, dummy, max(weights) * 1.2)
    crop(["-p", "0", "-o", name, dummy])
    os.unlink(dummy)
Beispiel #3
0
def pad_pdf(filepath):
    with Pdf.open(filepath) as pdf:
        _, _, W, H = list(pdf.pages[0].MediaBox)
    W = float(W)
    H = float(H)
    #    right_pad = int(W*0.2)
    #    bottom_pad = int(right_pad*H/W)
    #logger.info(f"{filepath} Adding padding. Bottom={bottom_pad} right={right_pad}")
    #subprocess.run(" ".join(["pdfcrop", "--margin", f"'0 0 {right_pad} {bottom_pad}'", filepath, filepath]), shell=True)
    print("Start pad")
    crop([
        "-p", "0", "-p4", "0", "200", "200", "0", "-a4", "-15", "0", "0", "0",
        str(filepath), "-o",
        str(filepath) + "_temp"
    ])
    print("End pad")
    os.rename(str(filepath) + "_temp", str(filepath))
    return filepath
Beispiel #4
0
# absolute off-set from left to left: left, bottom, right, head/pixel
# python -m pip install pdfCropMargins
# python -m pip install pdfCropMargins[gui] --upgrade --user
# if pdf corrupted use --gsFix option.
#    this option requires gswin32c.exe which may be downloaded from https://www.ghostscript.com/download/gsdnld.html
from pdfCropMargins import crop
filename = "Figure3.pdf"
crop(["-p", "0", "-gui", "--gsFix", filename])
Simple test of calling pdfCropMargins from a user Python script.

"""

from __future__ import print_function, division, absolute_import
import sys
import os

bin_dir = os.path.dirname(os.path.realpath(os.path.expanduser(__file__)))
package_dir = os.path.abspath(os.path.join(bin_dir, "..", "src"))
sys.path.insert(0, package_dir)
from pdfCropMargins import crop

try:
    crop([
        "~/papersToRead/dimethylethanolamine-DMAE-andSelectedSaltsAndEsters_2002.pdf",
        "-gui", "-Zv"
    ])
except BaseException as e:
    print("\nBad command args!  Exception is:\n", e, sep="")

crop([
    "~/papersToRead/dimethylethanolamine-DMAE-andSelectedSaltsAndEsters_2002.pdf",
    "-gui", "-v"
])
crop([
    "~/papersToRead/canWeBelieveInA-PurelyUnitaryQuantumDynamics_Herbut2005.pdf",
    "-gui", "-v"
])
Beispiel #6
0
os.system(cmd)
doc = fitz.open(locfile)

# doc.delete_pages(chap_end, -1)
# doc.delete_pages(0, chap_start - 2)

# repeat every page two times
for origin_pagenum in range(len(doc)):
    pagenum = origin_pagenum * 2
    doc.fullcopy_page(pagenum, pagenum)

doc.save(xmpdf)
doc.close()

# crop each page
crop(["-b", "c", "-ap", "73", "-p", "5", "-v", xmpdf, "-o", xmcpdf])

xmdoc = fitz.open(xmpdf)
xmcdoc = fitz.open(xmcpdf)

for pagenum in range(0, len(xmdoc), 2):
    xmpage = xmdoc.load_page(pagenum)
    X0 = xmpage.cropbox.x0
    Y0 = xmpage.cropbox.y0
    X1 = xmpage.cropbox.x1
    Y1 = xmpage.cropbox.y1
    xmcpage = xmcdoc.load_page(pagenum)
    x0 = xmcpage.cropbox.x0
    y0 = xmcpage.cropbox.y0
    x1 = xmcpage.cropbox.x1
    y1 = xmcpage.cropbox.y1
import platform

test_installed_package = True

if not test_installed_package:
    bin_dir = os.path.dirname(os.path.realpath(os.path.expanduser(__file__)))
    package_dir = os.path.abspath(os.path.join(bin_dir, "..", "src"))
    sys.path.insert(0, package_dir)

from pdfCropMargins import crop

try:  # Catch an exception, in this case a bad argument.
    print("running bad command-line test")
    crop([
        "$tpdfc/dimethylethanolamine-DMAE-andSelectedSaltsAndEsters_2002.pdf",
        "-gui", "-Zv"
    ],
         string_io=False)
except BaseException as e:  # Note BaseException is needed to catch a SystemExit.
    print("\nCaught exception {} running pdfCropMargins:\n   ".format(
        type(e).__name__),
          e,
          sep="")

#crop(["$tpdfc/canWeBelieveInA-PurelyUnitaryQuantumDynamics_Herbut2005.pdf", "-gui", "-pf", "-v"])
#crop(["$tpdfc/dimethylethanolamine-DMAE-andSelectedSaltsAndEsters_2002.pdf", "-gui", "-pf", "-v"])

exit_code, stdout, stderr = None, None, None
try:
    # Run capturing any SystemExit exit code and the string output.
    exit_code, stdout, stderr = crop([
Beispiel #8
0
doc.delete_pages(chap_end, -1)
doc.delete_pages(0, chap_start - 2)

# repeat every page three times
for origin_pagenum in range(len(doc)):
    pagenum = origin_pagenum * 3
    doc.fullcopy_page(pagenum, pagenum)
    pagenum = pagenum + 1
    doc.fullcopy_page(pagenum, pagenum)

doc.save(xmpdf)
doc.close()

# crop each page
crop(["-b", "c", "-ap", "10", "-p", "5", xmpdf, "-o", xmcpdf])

xmdoc = fitz.open(xmpdf)
xmcdoc = fitz.open(xmcpdf)

for pagenum in range(0, len(xmdoc), 3):
    xmpage = xmdoc.load_page(pagenum)
    X0 = xmpage.cropbox.x0
    Y0 = xmpage.cropbox.y0
    X1 = xmpage.cropbox.x1
    Y1 = xmpage.cropbox.y1
    xmcpage = xmcdoc.load_page(pagenum)
    x0 = xmcpage.cropbox.x0
    y0 = xmcpage.cropbox.y0
    x1 = xmcpage.cropbox.x1
    y1 = xmcpage.cropbox.y1