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)
def read_from(fpath, verbose=None, aslines=False, strict=True): """ Reads text from a file Args: fpath (str): file path aslines (bool): if True returns list of lines verbose (bool): verbosity flag Returns: text from fpath """ if verbose or (verbose is None and __PRINT_READS__): print('[util_io] * Reading text file: %r ' % util_path.tail(fpath)) try: if not util_path.checkpath(fpath, verbose=verbose, n=3): raise IOError('[io] * FILE DOES NOT EXIST!') with open(fpath, 'r') as file_: if aslines: text = file_.readlines() else: text = file_.read() return text except IOError as ex: from utool.util_dbg import printex if verbose or strict: printex(ex, ' * Error reading fpath=%r' % util_path.tail(fpath), '[io]') if strict: raise
def read_from(fpath, verbose=None, aslines=False, strict=True, n=None, errors='replace'): """ Reads text from a file. Automatically returns utf8. Args: fpath (str): file path aslines (bool): if True returns list of lines verbose (bool): verbosity flag Returns: str: text from fpath (this is unicode) Ignore: x = b'''/whaleshark_003_fors\xc3\xb8g.wmv" />\r\n''' ut.writeto('foo.txt', x) y = ut.readfrom('foo.txt') y.encode('utf8') == x """ if n is None: n = __READ_TAIL_N__ verbose = _rectify_verb_read(verbose) if verbose: print('[util_io] * Reading text file: %r ' % util_path.tail(fpath, n=n)) try: if not util_path.checkpath(fpath, verbose=verbose, n=n): raise IOError('[io] * FILE DOES NOT EXIST!') #with open(fpath, 'r') as file_: with open(fpath, 'rb') as file_: if aslines: #text = file_.readlines() if six.PY2: # python2 writes in bytes, so read as bytes then convert to # utf8 text = [line.decode('utf8', errors=errors) for line in file_.readlines()] else: text = [line.decode('utf8', errors=errors) for line in file_.readlines()] #text = file_.readlines() else: # text = file_.read() if six.PY2: text = file_.read().decode('utf8', errors=errors) else: #text = file_.read() text = file_.read().decode('utf8', errors=errors) return text except IOError as ex: from utool import util_dbg if verbose or strict: util_dbg.printex(ex, ' * Error reading fpath=%r' % util_path.tail(fpath, n=n), '[io]') if strict: raise
def view_directory(dname=None, verbose=True): """ view directory Args: dname (str): directory name verbose (bool): CommandLine: python -m utool.util_cplat --test-view_directory Example: >>> # DOCTEST_DISABLE >>> from utool.util_cplat import * # NOQA >>> import utool as ut >>> dname = ut.truepath('~') >>> verbose = True >>> view_directory(dname, verbose) """ from utool.util_arg import STRICT from utool.util_path import checkpath if verbose: print('[cplat] view_directory(%r) ' % dname) dname = os.getcwd() if dname is None else dname open_prog = { 'win32': 'explorer.exe', 'linux': 'nautilus', 'darwin': 'open' }[OS_TYPE] dname = normpath(dname) if STRICT: assert checkpath(dname, verbose=verbose) if dname.find(' ') != -1 and not dname.startswith(('"', '\'')): dname = '"%s"' % dname import pipes if WIN32: dname_ = dname else: dname_ = pipes.quote(dname) command = open_prog + ' ' + dname_ print(command) # spawn deteched process args = (open_prog, dname_) subprocess.Popen(args) #import utool as ut #ut.embed() #pass print('[cplat] exit view directory')
def grab_test_imgpath(key='lena.png', allow_external=True, verbose=True): r""" Gets paths to standard / fun test images. Downloads them if they dont exits Args: key (str): one of the standard test images, e.g. lena.png, carl.jpg, ... allow_external (bool): if True you can specify existing fpaths Returns: str: testimg_fpath - filepath to the downloaded or cached test image. SeeAlso: ut.get_valid_test_imgkeys CommandLine: python -m utool.util_grabdata --test-grab_test_imgpath Example: >>> # ENABLE_DOCTEST >>> from utool.util_grabdata import * # NOQA >>> import utool as ut >>> # build test data >>> key = 'carl.jpg' >>> # execute function >>> testimg_fpath = grab_test_imgpath(key) >>> # verify results >>> ut.assertpath(testimg_fpath) """ if allow_external and key not in TESTIMG_URL_DICT: testimg_fpath = key if not util_path.checkpath(testimg_fpath, verbose=True): import utool as ut raise AssertionError( 'testimg_fpath=%r not found did you mean %s' % ( testimg_fpath, ut.conj_phrase(get_valid_test_imgkeys(), 'or'))) else: testimg_fname = key testimg_url = TESTIMG_URL_DICT[key] testimg_fpath = grab_file_url(testimg_url, fname=testimg_fname, verbose=verbose) return testimg_fpath
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
def view_directory(dname=None, fname=None, verbose=True): """ View a directory in the operating system file browser. Currently supports windows explorer, mac open, and linux nautlius. Args: dname (str): directory name fname (str): a filename to select in the directory (nautlius only) verbose (bool): CommandLine: python -m utool.util_cplat --test-view_directory Example: >>> # DOCTEST_DISABLE >>> from utool.util_cplat import * # NOQA >>> import utool as ut >>> dname = ut.truepath('~') >>> verbose = True >>> view_directory(dname, verbose) Example: >>> # SCRIPT_TEST >>> from utool.util_cplat import * # NOQA >>> import utool as ut >>> base = ut.ensure_app_resource_dir('utool', 'test_vd') >>> dirs = [ >>> '', >>> 'dir1', >>> 'has space', >>> 'space at end ', >>> ' space at start ', >>> '"quotes and spaces"', >>> "'single quotes and spaces'", >>> 'Frogram Piles (y2K)', >>> ] >>> dirs_ = [ut.ensuredir(join(base, d)) for d in dirs] >>> for dname in dirs_: >>> ut.view_directory(dname, verbose=False) >>> fpath = join(base, 'afile.txt') >>> ut.touch(fpath) >>> ut.view_directory(base, fpath, verbose=False) """ from utool.util_arg import STRICT from utool.util_path import checkpath # from utool.util_str import SINGLE_QUOTE, DOUBLE_QUOTE if HAVE_PATHLIB and isinstance(dname, pathlib.Path): dname = str(dname) if verbose: print('[cplat] view_directory(%r) ' % dname) dname = os.getcwd() if dname is None else dname open_prog = { 'win32': 'explorer.exe', 'linux': 'nautilus', 'darwin': 'open' }[OS_TYPE] dname = normpath(dname) if STRICT: assert checkpath(dname, verbose=verbose), 'directory doesnt exit' if fname is not None and OS_TYPE == 'linux': arg = join(dname, fname) else: arg = dname # if ' ' in dname and not dname.startswith((SINGLE_QUOTE, DOUBLE_QUOTE)): # # Ensure quotations # dname = '"%s"' % dname # if not WIN32: # arg = dname # # arg = subprocess.list2cmdline([dname]) # # arg = pipes.quote(dname) # else: # arg = dname # spawn and detatch process args = (open_prog, arg) print(subprocess.list2cmdline(args)) subprocess.Popen(args)
def read_from(fpath, verbose=None, aslines=False, strict=True, n=None, errors='replace'): """ Reads text from a file. Automatically returns utf8. Args: fpath (str): file path aslines (bool): if True returns list of lines verbose (bool): verbosity flag Returns: str: text from fpath (this is unicode) Ignore: x = b'''/whaleshark_003_fors\xc3\xb8g.wmv" />\r\n''' ut.writeto('foo.txt', x) y = ut.readfrom('foo.txt') y.encode('utf8') == x """ if n is None: n = __READ_TAIL_N__ verbose = _rectify_verb_read(verbose) if verbose: print('[util_io] * Reading text file: %r ' % util_path.tail(fpath, n=n)) try: if not util_path.checkpath(fpath, verbose=verbose, n=n): raise IOError('[io] * FILE DOES NOT EXIST!') #with open(fpath, 'r') as file_: with open(fpath, 'rb') as file_: if aslines: #text = file_.readlines() if six.PY2: # python2 writes in bytes, so read as bytes then convert to utf8 text = [ line.decode('utf8', errors=errors) for line in file_.readlines() ] else: text = [ line.decode('utf8', errors=errors) for line in file_.readlines() ] #text = file_.readlines() else: # text = file_.read() if six.PY2: text = file_.read().decode('utf8', errors=errors) else: #text = file_.read() text = file_.read().decode('utf8', errors=errors) return text except IOError as ex: from utool import util_dbg if verbose or strict: util_dbg.printex( ex, ' * Error reading fpath=%r' % util_path.tail(fpath, n=n), '[io]') if strict: raise
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)
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