def crt_apnm_img(_ap: str, _nm_note: str, _inx: str) -> object: _ap = pth.ncnp(_ap) if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath() if not difi.chk_exst_fi(_ap): raise exc.ExceptionNotExistsFile() if not bool(pth.get_ext(_ap)): raise exc.ExceptionNotExistsFileExtension() if not pth.get_ext(_ap) in var.img_ext: raise exc.ExceptionNotExistsImageFile() nm_fi = pth.get_ap_innermst(_ap) em = crt_apnm_embed(_ap, _nm_note, _inx) at = "" if get_s_lst(nm_fi).isnumeric(): at = "{}-{}-{}.{}".format(_nm_note, _inx, var.bak, pth.get_ext(nm_fi)) else: at = "{}-{}-{}-{}.{}".format( _nm_note, _inx, pth.rm_ext(dttz.rm_prefix(nm_fi), pth.get_ext(nm_fi)), var.bak, pth.get_ext(nm_fi)) return op.struct(apa=pth.jo(pth.get_ap_1(_ap), at), ape=em.ap, nma=at, nme=em.nm, inx=_inx)
def get_img_dim(_ap:str) -> tuple: _ap = pth.ncnp(_ap) if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath() if not difi.chk_exst_fi(_ap): raise exc.ExceptionNotExistsFile() if not pth.get_ext(_ap) in var.img_ext: raise exc.ExceptionNotExistsImageFile() with Image.open(_ap) as img: return img.size return ()
def chk_md_b(_ap: str): _ap = pth.ncnp(_ap) if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath() if not difi.chk_exst_fi(_ap): raise exc.ExceptionNotExistsFile() if not pth.get_ext(_ap) == "md": raise exc.ExceptionNotExistsMDFile() """ The function of `rd_md()` is used to read all lines in `_ap`. If it returns `0` it means the .md file in `_ap` has no line written in it. """ return True if len(rd_md(_ap)) == 0 else False
def rd_md(_ap: str) -> list: _ap = pth.ncnp(_ap) if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath() if not difi.chk_exst_fi(_ap): raise exc.ExceptionNotExistsFile() if not pth.get_ext(_ap) == "md": raise exc.ExceptionNotExistsMDFile() md = open(_ap, "r") l = md.readlines() md.close() return l
def crt_apnm_attach(_ap: str, _nm_note: str, _inx: str) -> object: _ap = pth.ncnp(_ap) if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath() if not difi.chk_exst_fi(_ap): raise exc.ExceptionNotExistsFile() if not bool(pth.get_ext(_ap)): raise exc.ExceptionNotExistsFileExtension() nm = "{}-{}-{}".format(_nm_note, _inx, dttz.rm_prefix(pth.get_ap_innermst(_ap))) inx = _inx + 1 """ `pth.get_ap_1(_ap)` because the `_ap` refer to the file. The absolute path used \in `return` should refer to the note directory. """ return op.struct(ap=pth.jo(pth.get_ap_1(_ap), nm), nm=nm, inx=inx)
def get_img_dim_h(_ap:str) -> int: _ap = pth.ncnp(_ap) if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath() if not difi.chk_exst_fi(_ap): raise exc.ExceptionNotExistsFile() if not pth.get_ext(_ap) in var.img_ext: raise exc.ExceptionNotExistsImageFile() try: with Image.open(_ap) as img: return img.size[1] except ValueError: try: comm = subprocess.check_output(["convert \"{}\" -ping -format \"%h\" info:".format(_ap)], shell=True) return int(str(comm).split("'")[1]) except subprocess.CalledProcessError: pass return 0
def aw_md_(_ap: str, _ls: list, _m: str) -> bool: _ap = pth.ncnp(_ap) if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath() if not difi.chk_exst_fi(_ap): raise exc.ExceptionNotExistsFile() if not pth.get_ext(_ap) == "md": raise exc.ExceptionNotExistsMDFile() if not all(isinstance(i, str) for i in _ls): raise exc.ExceptionListNotAllString() if not _m in var.opn_mode: raise exc.ExceptionNotExistsOpenMode() md = open(_ap, _m) for i in _ls: print(i, end="", file=md) md.close() return True
def crt_apnm_img_cnvrt(_ap:str) -> object: _ap = pth.ncnp(_ap) if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath() if not difi.chk_exst_fi(_ap): raise exc.ExceptionNotExistsFile() if not pth.get_ext(_ap) in var.img_ext: raise exc.ExceptionNotExistsImageFile() """ Original image file. """ org_ap1 = pth.get_ap_1(_ap) org = pth.get_ap_innermst(_ap) orge = pth.get_ext(org) orgne = pth.rm_ext(org, orge) """ Backup image file. """ bnm = "{}_{}.{}".format(orgne, var.bak, orge) bnm_ap = pth.jo(org_ap1, bnm) """ Converted image file. """ cnvrt = "{}.{}".format(orgne, "png") cnvrt_ap = pth.jo(org_ap1, cnvrt) return op.struct(ap_bak=bnm_ap, ap_cn=cnvrt_ap, nm_bak=bnm, nm_cn=cnvrt)
def cnvrt_img_ip(_ap:str, _w:int=0, _h:int=0) -> str: _ap = pth.ncnp(_ap) if not pth.chk_ap(_ap): raise exc.ExceptionNotAbsolutePath() if not difi.chk_exst_fi(_ap): raise exc.ExceptionNotExistsFile() if not pth.get_ext(_ap) in var.img_ext: raise exc.ExceptionNotExistsImageFile() _w = "" if _w <= 0 else _w _h = "" if _h <= 0 else _h nm_fi = crt_apnm_img_cnvrt(_ap) difi.ren(_ap, nm_fi.nm_bak) _ap = nm_fi.ap_bak """ Convert `_ap` into `nm_fi.ap_cn`. The original `_ap` is retained. """ com = "convert \"{}\" -resize {}x{} \"{}\"".format(_ap, _w, _h, nm_fi.ap_cn) subprocess.run(com, shell=True) """ CAUTION: Make sure to check if conversion failed (I created file with image extension although it is not image file per se). I only use this for debugging purposes. """ if not difi.chk_exst_fi(nm_fi.ap_cn): difi.ren(_ap, nm_fi.nm_cn) else: difi.de(_ap) # Because ImageMagick's `convert` create a new copy of the converted # image, this program needs to delete the backup file. return nm_fi.ap_cn