From 1019f1c1097e96f5837bca660a50b49ca6d0759c Mon Sep 17 00:00:00 2001 From: duangsuse Date: Tue, 19 Nov 2024 19:19:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=95=E5=90=8C=E5=AD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Others/py/apple.py | 135 +++++++++++++++++++++++++++++++++++++++++++++ Others/py/bin.py | 58 +++++++++++++++++++ 2 files changed, 193 insertions(+) create mode 100644 Others/py/apple.py create mode 100644 Others/py/bin.py diff --git a/Others/py/apple.py b/Others/py/apple.py new file mode 100644 index 0000000..899f830 --- /dev/null +++ b/Others/py/apple.py @@ -0,0 +1,135 @@ +import cv2, numpy as np +import time,os,sys; now=time.time + +grays = [*(" .-:=*+%@#" if 0 else " .-~:;!=*%$@#")] +def ascii(img, wh=np.int32(os.popen('stty size', 'r').read().split()[::-1]) ): + a=cv2.cvtColor(cv2.resize(img, wh) , cv2.COLOR_BGR2GRAY) + b=asci[ np.int32(a)*len(asci)//256 ] + return np.array([''.join(y) for y in b]) + +doDiff=0 +match os.environ.get('cp'): + case None:pass + case "sq": + import quadtree + case "tri": + from scipy.spatial import Delaunay,Voronoi + case "dot": + import circlify + case _: grays=[f'\x1B[{c}m ' for c in [40,100,47,37,107] ]; doDiff=1 +def lineANSI(): + a=iter(lambda:'',0) + while True: + b=yield + for i in (np.where(a!=b)[0] if doDiff else range(b.size)): sys.stdout.write("\x1B[%d;0H%s" % (i,b[i])) + a=b + +asci=np.array(grays) + +def mpread(fp): + cap,ok,f = cv2.VideoCapture(fp), True, None + fps = 1/cap.get(cv2.CAP_PROP_FPS) + @task + def seek(): + t,t0=0,0; nonlocal f + for x in os.popen('ffplay 2>&1 %s' %(repr(fp),) ): + try: + t=float(x.strip().split(' ',1)[0]); (t0:=t,f:=lambda:cap.set(cv2.CAP_PROP_POS_MSEC, t*1000) ) if abs(t-t0)>11 else () + except:pass + try: + while ok: + t0=now() + if f:f();f=None + ok,img=cap.read() + yield img; t=now() + while now(){"+s+"}"+f")().then(r=>{k}=r||null).catch(e=>{k}=e+'')")) !='{}':bad() + while ''==(v:=js(k)): time.sleep(.1) + try:js("delete "+k); return json.loads(v or 'null') + except:bad() + +def ffread(v,w,h): + with os.popen(f'ffmpeg -s {w}x{h} {v} -c:v rawvideo -pix_fmt bgr24 -loglevel quiet -f image2pipe -') as p: + while (b:=p.buffer.read(w*h*3)): yield np.frombuffer(b, np.uint8).reshape(h,w,3) + +def ffrec(f): + wi=win('REC'); x,y,w,h=wi.cropScr() + for x in ffread(f'-f x11grab -i :0+{x},{y}' if os.name!='nt' else f'-f gdigrab -i desktop -offset_x {x} -offset_y {y}',w,h): + wi.imshow(f(x));cv2.waitKey(1) + +class win: + def __init__(o,k,chk=cv2.WINDOW_NORMAL):o.k=k; cv2.namedWindow(k,chk) + def off(o): cv2.destroyWindow(o.k) + __getattr__=lambda o,k:o.__dict__.get(k) or (lambda*a:getattr(cv2,k)(o.k,*a)) + __setitem__=lambda o,k,v: cv2.setWindowProperty(o.k,k,v) + def cropScr(w): + try: + r=gjs(''' + let ucall=(o,k, f)=>new Promise(ok=>{f=o[k];if(!f.did){f=f.bind(o);o[k]=(...a)=>(o[k].did(a),f(...a))} o[k].did=ok}) + + var o=Main.screenshotUI,r + o.open(); await ucall(o,'close'); r=o._getSelectedGeometry() + return r[3]?r: globalThis.eval(` + with(o._windowSelectors[0].windows().find(x=>x.checked).boundingBox){[x,y,width,height] } `) + ''') + except: + from PIL import ImageGrab + w[cv2.WND_PROP_FULLSCREEN]=1 + r=w.selectROI(np.array(ImageGrab.grab()));w.off() + return r + +if 0: + @ffrec + def f(x): + A,B=colorANSI + return mockTTY(x,(30,24),lambda x: np.apply_along_axis(lambda x:B[A(x)], 2,x) ) + +import threading +task=lambda f: threading.Thread(target=f).start() + +_,s=sys.argv +try: + print(*ascii(cv2.imread(s)),"\x1B[0m", sep="\n") +except: + io=lineANSI(); next(io) + for x in mpread(s): io.send(ascii(x)) diff --git a/Others/py/bin.py b/Others/py/bin.py new file mode 100644 index 0000000..ea5baae --- /dev/null +++ b/Others/py/bin.py @@ -0,0 +1,58 @@ +from socket import* +import json as J +json=(J.loads,J.dumps) +import struct as S +import io + +class Bin: + def __init__(o,pat): o.m=pat; o._n=S.calcsize(pat) + def read(o, s):return S.unpack(o.m, s.read(o._n)) + def write(o, s,obj):s.write(S.pack(o.m, obj)) + +letIf=lambda o,p,f:f(o)if p else o +def bin(pat): + n=S.calcsize(pat); return( + lambda s:letIf(S.unpack(pat, s.read(n)), len(pat)==1,lambda x:x[0]), + lambda s,o:s.write(S.pack(pat, o)) ) + +def lenPrefix(m): + a,b=m; A=lambda s: s.read(a(s)) + def B(s,o): b(s,len(o)); s.write(o) + return A,B +def pipe(f2,F2): + a,b=f2;A,B=F2 + return (lambda s:A(a(s)) ), lambda s,o:b(s,bytes(B(o),enc)) + +enc='utf8' +class Binary: + def __init__(o,it, **cd): + if isinstance(it,(str,bytes,list)): it=io.BytesIO(bytes(it,**cd)) + o.read=it.read or it.recv + o.write=it.write or it.send + o.val=it.getvalue + def r(o, pat):return pat[0](o) + def w(o, pat,obj):pat[1](o,obj) + +mdFile= pipe(lenPrefix(bin("i")),json) #can use marshal however + +b=Binary("",encoding="utf8") +b.w(mdFile,[1,2,3]) +Binary(b.val()).r(mdFile) + + +def mlist(*m, flat={0}): + def A(s,a): + a.append(ns:=[]) + for i,x in enumerate(m): + one=x[0](s) + if i in flat: a.extend(one); ns.append(len(one)) + else: a.append(one) + def B(s,a): + ns=a.pop(0) + for i,x in enumerate(m): + n=ns.pop()if(i in flat)else 1 + x[1](s,a[:n]);del a[:n] + return onnew(list,A),B +onnew=lambda make,f:lambda*a: f(*a,make()) + +mJF=mlist(mJ,mBuf)