You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.6 KiB
48 lines
1.6 KiB
import numpy as _np
|
|
import sys as _sys
|
|
|
|
#_PIL_PATH = r'C:\Users\user\AppData\Roaming\Python\Python311\site-packages'
|
|
_PIL_PATH=r'C:\Users\tech\AppData\Local\Programs\Python\Python311\site-packages'
|
|
|
|
_LATIN = 'C:/Windows/Fonts/SpaceMono-Regular.ttf'
|
|
_CJK = 'C:/Windows/Fonts/NotoSansCJKtc-Regular.otf'
|
|
_FC = {}
|
|
|
|
def _font(cjk, sz):
|
|
from PIL import ImageFont
|
|
sz=max(8,int(sz)); k=(cjk,sz)
|
|
if k not in _FC:
|
|
try: _FC[k]=ImageFont.truetype(_CJK if cjk else _LATIN,sz)
|
|
except: _FC[k]=ImageFont.load_default()
|
|
return _FC[k]
|
|
|
|
def _cjk(ch): return '\u4e00'<=ch<='\u9fff'
|
|
|
|
def _dtxt(draw, text, x, y, sz, col):
|
|
cx=float(x)
|
|
for ch in text:
|
|
f=_font(_cjk(ch),sz)
|
|
try:
|
|
bb=draw.textbbox((0,0),ch,font=f)
|
|
draw.text((cx,y-sz*.55),ch,font=f,fill=col)
|
|
cx+=bb[2]-bb[0]
|
|
except: cx+=sz*.6
|
|
|
|
def onSetupParameters(scriptOp): pass
|
|
|
|
def onCook(scriptOp):
|
|
try:
|
|
if _PIL_PATH not in _sys.path: _sys.path.insert(0,_PIL_PATH)
|
|
from PIL import Image, ImageDraw
|
|
root=op('/project1/deep_sound')
|
|
W=int(root.par.Resw.val); H=int(root.par.Resh.val)
|
|
rows=root.fetch('rows',[]); fs=root.fetch('font_size',18)
|
|
img=Image.new('RGBA',(W,H),(0,0,0,0)) # transparent background
|
|
draw=ImageDraw.Draw(img)
|
|
for row in rows:
|
|
for wrd in row['words']:
|
|
_dtxt(draw,wrd['text'],wrd['x'],row['y'],fs,(255,255,255,255))
|
|
arr=_np.ascontiguousarray(_np.flipud(_np.array(img,dtype=_np.float32)/255.))
|
|
scriptOp.copyNumpyArray(arr)
|
|
except Exception as e:
|
|
import traceback; traceback.print_exc()
|
|
|