Example #1
0
def render(input_text, fnum=1, dpath=None, verbose=True):
    """
    fixme or remove
    """
    import pylab as plt
    import matplotlib as mpl
    #verbose = True
    text = make_full_document(input_text)
    cwd = os.getcwd()
    if dpath is None:
        text_dir = join(cwd, 'tmptex')
    else:
        text_dir = dpath
    util_path.ensuredir(text_dir, verbose=verbose)
    text_fname = 'latex_formatter_temp.tex'
    text_fpath = join(text_dir, text_fname)
    pdf_fpath = splitext(text_fpath)[0] + '.pdf'
    jpg_fpath = splitext(text_fpath)[0] + '.jpg'
    try:
        os.chdir(text_dir)
        util_io.write_to(text_fpath, text)
        pdflatex_args = ('pdflatex', '-shell-escape', '--synctex=-1', '-src-specials', '-interaction=nonstopmode')
        args = pdflatex_args + (text_fpath,)
        util_cplat.cmd(*args, verbose=verbose)
        assert util_path.checkpath(pdf_fpath, verbose=verbose), 'latex failed'
        # convert latex pdf to jpeg
        util_cplat.cmd('convert', '-density', '300', pdf_fpath, '-quality', '90', jpg_fpath, verbose=verbose)
        assert util_path.checkpath(jpg_fpath, verbose=verbose), 'imgmagick failed'
        tex_img = plt.imread(jpg_fpath)
        # Crop img bbox
        nonwhite_x = np.where(tex_img.flatten() != 255)[0]
        nonwhite_rows = nonwhite_x // tex_img.shape[1]
        nonwhite_cols = nonwhite_x % tex_img.shape[1]
        x1 = nonwhite_cols.min()
        y1 = nonwhite_rows.min()
        x2 = nonwhite_cols.max()
        y2 = nonwhite_rows.max()
        #util.embed()
        cropped = tex_img[y1:y2, x1:x2]
        fig = plt.figure(fnum)
        fig.clf()
        ax = fig.add_subplot(1, 1, 1)
        ax.imshow(cropped, cmap=mpl.cm.gray)
        #mpl.rc('text', usetex=True)
        #mpl.rc('font', family='serif')
        #plt.figure()
        #plt.text(9, 3.4, text, size=12)
    except Exception as ex:
        print('LATEX ERROR')
        print(text)
        print(ex)
        print('LATEX ERROR')
        pass
    finally:
        os.chdir(cwd)
Example #2
0
def text_dict_write(fpath, dict_):
    """
    Very naive, but readable way of storing a dictionary on disk
    FIXME: This broke on RoseMary's big dataset. Not sure why. It gave bad
    syntax. And the SyntaxError did not seem to be excepted.
    """
    #dict_ = text_dict_read(fpath)
    #dict_[key] = val
    dict_text2 = util_str.dict_str(dict_, strvals=False)
    if VERBOSE:
        print('[cache] ' + str(dict_text2))
    util_io.write_to(fpath, dict_text2)
Example #3
0
def compile_latex_text(input_text, fnum=1, dpath=None, verbose=True, fname=None, title=None, **kwargs):
    """
    pdflatex -shell-escape --synctex=-1 -src-specials -interaction=nonstopmode /home/joncrall/code/ibeis/tmptex/latex_formatter_temp.tex

    Example1:
        >>> # DISABLE_DOCTEST
        >>> from utool.util_latex import *  # NOQA
        >>> import utool as ut
        >>> verbose = True
        >>> dpath = '/home/joncrall/code/ibeis/aidchallenge'
        >>> ut.vd(dpath)
        >>> orig_fpath_list = ut.list_images(dpath, fullpath=True)
        >>> figure_str = ut.get_latex_figure_str(new_rel_fpath_list, width_str='2.4in', nCols=2)
        >>> input_text = figure_str
        >>> pdf_fpath = ut.compile_latex_text(input_text, dpath=dpath, verbose=verbose)
        >>> output_pdf_fpath = ut.compress_pdf(pdf_fpath)

        fpath_list
        def clipwhite_ondisk(fpath_in):
            import utool as ut
            import vtool as vt
            fpath_out = ut.augpath(fpath_in, '_clipwhite')
            img = vt.imread(fpath_in)
            thresh = 128
            fillval = [255, 255, 255]
            cropped_img = vt.crop_out_imgfill(img, fillval=fillval, thresh=thresh)
            vt.imwrite(fpath_out, cropped_img)
            return fpath_out
        fpath_list_ = [clipwhite_ondisk(fpath) for fpath in fpath_list]
        tmpfig = join(dpath, 'tmpfig')
        ut.ensuredir(tmpfig)
        # Weirdness
        from os.path import *
        new_fpath_list = []
        for fpath in fpath_list_:
            fname, ext = splitext(basename(fpath))
            fname_ = ut.hashstr(fname, alphabet=ut.ALPHABET_16) + ext
            fpath_ = join(tmpfig, fname_)
            ut.move(fpath, fpath_)
            new_fpath_list.append(fpath_)
        new_rel_fpath_list = [ut.relpath_unix(fpath_, dpath) for fpath_ in new_fpath_list]

    """
    #import pylab as plt
    #import matplotlib as mpl
    #verbose = True
    text = make_full_document(input_text, title=title)
    cwd = os.getcwd()
    if dpath is None:
        text_dir = join(cwd, 'tmptex')
    else:
        text_dir = dpath
    util_path.ensuredir(text_dir, verbose=verbose)
    if fname is None:
        fname = 'latex_formatter_temp'
    text_fname = fname + '.tex'
    #text_fname = 'latex_formatter_temp.tex'
    text_fpath = join(text_dir, text_fname)
    pdf_fpath = splitext(text_fpath)[0] + '.pdf'
    #jpg_fpath = splitext(text_fpath)[0] + '.jpg'
    try:
        os.chdir(text_dir)
        util_io.write_to(text_fpath, text)
        pdflatex_args = ('pdflatex', '-shell-escape', '--synctex=-1', '-src-specials', '-interaction=nonstopmode')
        args = pdflatex_args + (text_fpath,)
        util_cplat.cmd(*args, verbose=verbose, **kwargs)
        assert util_path.checkpath(pdf_fpath, verbose=verbose), 'latex failed'
    except Exception as ex:
        ut.printex(ex, 'LATEX ERROR')
    finally:
        os.chdir(cwd)
    return pdf_fpath
Example #4
0
def render(input_text, fnum=1, dpath=None, verbose=True):
    """
    fixme or remove
    """
    import pylab as plt
    import matplotlib as mpl
    #verbose = True
    text = make_full_document(input_text)
    cwd = os.getcwd()
    if dpath is None:
        text_dir = join(cwd, 'tmptex')
    else:
        text_dir = dpath
    util_path.ensuredir(text_dir, verbose=verbose)
    text_fname = 'latex_formatter_temp.tex'
    text_fpath = join(text_dir, text_fname)
    pdf_fpath = splitext(text_fpath)[0] + '.pdf'
    jpg_fpath = splitext(text_fpath)[0] + '.jpg'
    try:
        os.chdir(text_dir)
        util_io.write_to(text_fpath, text)
        pdflatex_args = ('pdflatex', '-shell-escape', '--synctex=-1',
                         '-src-specials', '-interaction=nonstopmode')
        args = pdflatex_args + (text_fpath, )
        util_cplat.cmd(*args, verbose=verbose)
        assert util_path.checkpath(pdf_fpath, verbose=verbose), 'latex failed'
        # convert latex pdf to jpeg
        util_cplat.cmd('convert',
                       '-density',
                       '300',
                       pdf_fpath,
                       '-quality',
                       '90',
                       jpg_fpath,
                       verbose=verbose)
        assert util_path.checkpath(jpg_fpath,
                                   verbose=verbose), 'imgmagick failed'
        tex_img = plt.imread(jpg_fpath)
        # Crop img bbox
        nonwhite_x = np.where(tex_img.flatten() != 255)[0]
        nonwhite_rows = nonwhite_x // tex_img.shape[1]
        nonwhite_cols = nonwhite_x % tex_img.shape[1]
        x1 = nonwhite_cols.min()
        y1 = nonwhite_rows.min()
        x2 = nonwhite_cols.max()
        y2 = nonwhite_rows.max()
        #util.embed()
        cropped = tex_img[y1:y2, x1:x2]
        fig = plt.figure(fnum)
        fig.clf()
        ax = fig.add_subplot(1, 1, 1)
        ax.imshow(cropped, cmap=mpl.cm.gray)
        #mpl.rc('text', usetex=True)
        #mpl.rc('font', family='serif')
        #plt.figure()
        #plt.text(9, 3.4, text, size=12)
    except Exception as ex:
        print('LATEX ERROR')
        print(text)
        print(ex)
        print('LATEX ERROR')
        pass
    finally:
        os.chdir(cwd)
Example #5
0
def compile_latex_text(input_text,
                       fnum=1,
                       dpath=None,
                       verbose=True,
                       fname=None,
                       title=None,
                       nest_in_doc=None,
                       **kwargs):
    r"""
    pdflatex -shell-escape --synctex=-1 -src-specials -interaction=nonstopmode /home/joncrall/code/ibeis/tmptex/latex_formatter_temp.tex

    CommandLine:
        python -m utool.util_latex --test-compile_latex_text --show

    Example1:
        >>> # DISABLE_DOCTEST
        >>> from utool.util_latex import *  # NOQA
        >>> import utool as ut
        >>> verbose = True
        >>> #dpath = '/home/joncrall/code/ibeis/aidchallenge'
        >>> dpath = dirname(ut.grab_test_imgpath())
        >>> #ut.vd(dpath)
        >>> orig_fpath_list = ut.list_images(dpath, fullpath=True)
        >>> figure_str = ut.get_latex_figure_str(orig_fpath_list, width_str='2.4in', nCols=2)
        >>> input_text = figure_str
        >>> pdf_fpath = ut.compile_latex_text(input_text, dpath=dpath, verbose=verbose)
        >>> output_pdf_fpath = ut.compress_pdf(pdf_fpath)
        >>> print(pdf_fpath)
        >>> ut.quit_if_noshow()
        >>> ut.startfile(pdf_fpath)

        fpath_list
        def clipwhite_ondisk(fpath_in):
            import utool as ut
            import vtool as vt
            fpath_out = ut.augpath(fpath_in, '_clipwhite')
            img = vt.imread(fpath_in)
            thresh = 128
            fillval = [255, 255, 255]
            cropped_img = vt.crop_out_imgfill(img, fillval=fillval, thresh=thresh)
            vt.imwrite(fpath_out, cropped_img)
            return fpath_out
        fpath_list_ = [clipwhite_ondisk(fpath) for fpath in fpath_list]
        tmpfig = join(dpath, 'tmpfig')
        ut.ensuredir(tmpfig)
        # Weirdness
        from os.path import *
        new_fpath_list = []
        for fpath in fpath_list_:
            fname, ext = splitext(basename(fpath))
            fname_ = ut.hashstr(fname, alphabet=ut.ALPHABET_16) + ext
            fpath_ = join(tmpfig, fname_)
            ut.move(fpath, fpath_)
            new_fpath_list.append(fpath_)
        new_rel_fpath_list = [ut.relpath_unix(fpath_, dpath) for fpath_ in new_fpath_list]

    """
    #import pylab as plt
    #import matplotlib as mpl
    #verbose = True
    if nest_in_doc or (nest_in_doc is None
                       and input_text.find('documentclass') == -1):
        text = make_full_document(input_text, title=title)
    #text = make_full_document(input_text, title=title)
    cwd = os.getcwd()
    if dpath is None:
        text_dir = join(cwd, 'tmptex')
    else:
        text_dir = dpath
    util_path.ensuredir(text_dir, verbose=verbose)
    if fname is None:
        fname = 'latex_formatter_temp'
    text_fname = fname + '.tex'
    #text_fname = 'latex_formatter_temp.tex'
    text_fpath = join(text_dir, text_fname)
    pdf_fpath = splitext(text_fpath)[0] + '.pdf'
    #jpg_fpath = splitext(text_fpath)[0] + '.jpg'
    try:
        os.chdir(text_dir)
        util_io.write_to(text_fpath, text)
        pdflatex_args = ('pdflatex', '-shell-escape', '--synctex=-1',
                         '-src-specials', '-interaction=nonstopmode')
        args = pdflatex_args + (text_fpath, )
        util_cplat.cmd(*args, verbose=verbose, **kwargs)
        assert util_path.checkpath(pdf_fpath, verbose=verbose), 'latex failed'
    except Exception as ex:
        import utool as ut
        print('Error compiling')
        ut.print_code(text, 'latex')
        ut.printex(ex, 'LATEX ERROR')
        raise
    finally:
        os.chdir(cwd)
    return pdf_fpath