import random, hashlib, math, io from PIL import Image, ImageChops as C, ImageOps as O, ImageDraw, ImageFilter def generate_image(text: str) -> bytes: """Generated by ChatGPT""" r = random.Random(hashlib.sha256(text.encode("utf-8", "surrogatepass")).digest()) u, pick = r.uniform, r.choice sin, cos, pi = math.sin, math.cos, math.pi S, N = 640, 192 scale = S / 384 def px(n): return max(1, round(n * scale)) B = Image.Resampling.BICUBIC size = (S, S) def noise(n, s=S): return Image.frombytes('L',(n,n),r.randbytes(n*n)).resize((s,s),B) # OKLCH -> sRGB, with gamut mapping (reduce chroma rather than clip). def rgb(L, c, h): for _ in range(12): a,b = c*cos(h),c*sin(h) l=(L+.3963377774*a+.2158037573*b)**3 m=(L-.1055613458*a-.0638541728*b)**3 s=(L-.0894841775*a-1.291485548*b)**3 v=(4.0767416621*l-3.3077115913*m+.2309699292*s, -1.2684380046*l+2.6097574011*m-.3413193965*s, -.0041960863*l-.7034186147*m+1.707614701*s) if min(v)>=0 and max(v)<=1: break c*=.84 return tuple(round(255*max(0,min(1,12.92*x if x<=.0031308 else 1.055*x**(1/2.4)-.055))) for x in v) # type: ignore h=u(0,2*pi); dh=pick((-1,1))*u(.18,1.05) accent=h+dh*pick((1,1,1.5,2.5))+pick((0,0,0,pi*.7)) colors=[rgb(u(.27,.36),u(.075,.13),h), rgb(u(.46,.57),u(.15,.23),h+dh*.35), rgb(u(.62,.73),u(.17,.25),h+dh),rgb(u(.74,.83),u(.13,.21),accent), rgb(u(.87,.93),u(.06,.12),accent)] keys=[0,u(.2,.3),u(.45,.6),u(.72,.86),1]; pal=[]; j=0 for i in range(256): t=i/255 if j<3 and t>keys[j+1]: j+=1 w=(t-keys[j])/(keys[j+1]-keys[j]); w=w*w*(3-2*w) pal.append(tuple(round(a+(b-a)*w) for a,b in zip(colors[j],colors[j+1]))) def paint(im): im=im.copy(); im.putpalette([v for p in pal for v in p]); return im.convert('RGB') def normalize(im): return O.autocontrast(im,cutoff=1) def field(): dx,dy=noise(r.randint(3,6),N).tobytes(),noise(r.randint(3,6),N).tobytes() a,b,c=u(1.3,5.5),u(1.1,4.5),u(-3,3) ox,oy,t=u(-.9,.9),u(-.9,.9),u(0,2*pi) ct,st,w=cos(t),sin(t),u(.12,.85) f=pick((lambda x,y: sin(a*x+b*sin(c*y+x)), lambda x,y: sin(a*math.hypot(x,y)+b*math.atan2(y,x)), lambda x,y: sin(a*x)*cos(b*y)+.55*x, lambda x,y: sin(a*x+b*y)+.7*cos(b*x-a*y+c), lambda x,y: cos(a*(x*x-y*y)+b*x*y)+.6*y, lambda x,y: math.hypot(sin(a*x),cos(b*y+c)), lambda x,y: math.hypot(x+.4*sin(b*y),y+.4*cos(a*x))+.4*sin(c*x))) vals=[] for i,(p,q) in enumerate(zip(dx,dy)): x=(i%N)/(N/2)-1+w*(p-128)/128+ox y=(i//N)/(N/2)-1+w*(q-128)/128+oy vals.append(f(ct*x-st*y,st*x+ct*y)) lo,hi=min(vals),max(vals); k=255/max(hi-lo,1e-9) return Image.frombytes('L',(N,N),bytes(min(255,int((v-lo)*k)) for v in vals)).resize(size,B) def height(): im=field() if r.random()<.7 else noise(r.randint(3,6)) im=Image.blend(im,noise(r.randint(5,11)),u(.12,.36)) im=Image.blend(im,noise(r.randint(16,28)),u(.06,.14)) return normalize(im) def shade(im, strength): dx,dy=pick(((2,1),(-2,1),(1,-2))) dx,dy=round(dx*scale),round(dy*scale) g=C.subtract(C.offset(im,dx,dy),im,offset=128) g=g.point(lambda i:max(65,min(255,235+strength*(i-128)))) return Image.merge('RGB',(g,g,g)) mode=r.randrange(9) if mode<3: im=height() if r.random()<.3: im=C.invert(im) if mode==2: a=field(); im=normalize(Image.blend(im,pick((C.difference,C.multiply,C.lighter))(im,a),u(.35,.8))) if mode==1: # terraced contours n=r.randint(4,11); phase=u(0,1) t=im.point(lambda i: int(255*(.78*(int(i*n/256)/n)+.22*i/255))) result=paint(t) edges=im.point([round(255*max(0,1-abs((i*n/255+phase)%1-.5)*18)) for i in range(256)]) result=Image.composite(Image.new('RGB',size,pal[pick((12,242))]),result,edges.point(lambda i:i*.64)) result=C.multiply(result,shade(im,2.3)) else: result=C.multiply(paint(im),shade(im,u(4.0,9.0))) if r.random()<.8: n=r.randint(3,10) lines=im.point([int(200*max(0,1-abs((i*n/255)%1-.5)*24)) for i in range(256)]) result=Image.composite(Image.new('RGB',size,pal[pick((12,230,250))]),result,lines) elif mode==3 or mode==4: # streamlines / ribbons bg=Image.blend(height(),Image.new('L',size,pick((10,20,60,130,165))),.84) result=paint(bg); d=ImageDraw.Draw(result,'RGBA') a,b,p=u(2,6),u(2,6),u(-pi,pi) cx,cy=u(.15,.85),u(.15,.85) k=u(.7,2.2); step=u(1.7,3.8)*scale; steps=r.randint(35,90) vector=pick((lambda x,y:p+2.5*sin(a*x+b*y)+1.2*cos(b*y-p), lambda x,y:p+2*sin(a*x+p)*cos(b*y-p), lambda x,y:math.atan2(y-cy,x-cx)+pi/2+k*sin(a*x+b*y+p), lambda x,y:p+1.8*sin(a*math.hypot(x-cx,y-cy)+b*x))) for _ in range(r.randint(210,390)): x,y=u(-.16,1.16)*S,u(-.16,1.16)*S; pts=[] for i in range(steps): pts.append((x,y)); ang=vector(x/S,y/S) x+=step*cos(ang); y+=step*sin(ang) idx=r.randint(110,250) if mode==3 else r.randint(10,245); width=pick((1,1,2,2,3) if mode==3 else (1,2,4,7)) d.line(pts,fill=(*pal[idx],r.randint(105,245)),width=px(width),joint='curve') if width>=4: d.line(pts,fill=(*pal[min(255,idx+45)],185),width=px(1)) elif mode==5: # Peter de Jong strange attractor best=0 for _ in range(8): a,b,c,d=(u(-pi,pi) for _ in range(4)); x=y=.1; seen=set() for i in range(2600): x,y=sin(a*y)-cos(b*x),sin(c*x)-cos(d*y) if i>100: seen.add((int(x*35),int(y*35))) if len(seen)>best: best,params=len(seen),(a,b,c,d) if best>1400: break a,b,c,d=params; x=y=.1; hist=[0]*(S*S) # type: ignore for i in range(120000): x,y=sin(a*y)-cos(b*x),sin(c*x)-cos(d*y) if i>=100: hist[int((y+2)*(.23*S)+.04*S)*S+int((x+2)*(.23*S)+.04*S)]+=1 hits=sorted(v for v in hist if v) peak=max(8,hits[-max(1,len(hits)//100)]) lut=[min(255,round(255*math.log1p(i)/math.log1p(peak))) for i in range(max(hist)+1)] im=Image.frombytes('L',size,bytes(lut[i] for i in hist)) if best<150: im=height(); result=C.multiply(paint(im),shade(im,3)) else: core=im.filter(ImageFilter.GaussianBlur(.25*scale)) glow=im.filter(ImageFilter.GaussianBlur(4*scale)).point(lambda i:min(255,2.4*i)) # type: ignore bg=Image.blend(noise(5),Image.new('L',size,10),.88) result=paint(C.add(C.add(bg,glow),core)) elif mode==6: # projected parametric surface bg=Image.blend(height(),Image.new('L',size,pick((14,38,100,170))),.88) result=paint(bg); draw=ImageDraw.Draw(result,'RGBA') a,b=r.randint(2,7),r.randint(1,5) phase,rot,tilt=u(0,2*pi),u(0,2*pi),u(.35,1.25) ct,st=cos(rot),sin(rot); ctil,stil=cos(tilt),sin(tilt); R=u(.21,.32)*S; q=u(.10,.24)*S cx,cy=u(.42,.58)*S,u(.42,.58)*S count=r.randint(42,86); curves=[]; form=r.randrange(3) for j in range(count): v=2*pi*j/count; pts=[]; depth=0 for i in range(481): t=2*pi*i/480 radius=R+q*cos(a*t+v+phase) x=radius*cos(b*t); y=radius*sin(b*t); z=q*sin(a*t+v+phase) if form==1: x=R*sin(a*t+v)+q*cos(b*t); y=R*cos(b*t+v)+q*sin(a*t); z=q*sin(a*t-b*t+v) elif form==2: x=radius*cos(t); y=radius*sin(t); z=q*cos(b*t+v)*sin(a*t+phase) y,z=y*ctil-z*stil,y*stil+z*ctil pts.append((cx+ct*x-st*y,cy+st*x+ct*y)); depth+=z curves.append((depth,j,pts)) for _,j,pts in sorted(curves): k=int(110+140*(.5+.5*sin(j*2*pi/count+phase))) draw.line(pts,fill=(*pal[k],r.randint(110,210)),width=px(pick((1,1,2)))) elif mode==7: # layered, fractured geometry with independent contour fields im=height(); other=field() result=paint(im) n=r.randint(4,8) sites=[(u(0,S),u(0,S)) for _ in range(n)] def cell(i): x,y=sites[i]; poly=[(0,0),(S,0),(S,S),(0,S)] for j,(a,b) in enumerate(sites): if i==j: continue dx,dy=a-x,b-y; k=(a*a+b*b-x*x-y*y)/2 clipped=[] for p,q in zip(poly,poly[1:]+poly[:1]): P=p[0]*dx+p[1]*dy-k; Q=q[0]*dx+q[1]*dy-k if P<=0: clipped.append(p) if (P<=0)!=(Q<=0): t=P/(P-Q); clipped.append((p[0]+t*(q[0]-p[0]),p[1]+t*(q[1]-p[1]))) poly=clipped return poly polys=[cell(i) for i in range(n)] for i in range(n): m=Image.new('L',size); d=ImageDraw.Draw(m) poly=polys[i] d.polygon(poly,fill=255) source=pick((im,other,C.invert(im),C.invert(other))) shift=r.randint(-65,65) source=source.point(lambda x:max(0,min(255,x+shift))) # type: ignore if r.random()<.6: k=r.randint(3,9) source=source.point(lambda x:int(255*(.7*int(x*k/256)/k+.3*x/255))) result.paste(C.multiply(paint(source),shade(source,4)),mask=m) draw=ImageDraw.Draw(result,'RGBA') if r.random()<.7: for poly in polys: draw.line(poly+poly[:1],fill=(*pal[240],100),width=px(1)) else: # Mandelbrot: randomized zoom and entropy-based selection best=-float("inf") for _ in range(6): a,b=pick(((-.743643887,.131825904),(-.1011,.9563),(-1.25,0),(-.5,.55),(-.75,.1))) z=10**u(-2.2,-.55); a+=u(-.35,.35)*z; b+=u(-.35,.35)*z extent=(a-z,b-z,a+z,b+z); n=r.randint(70,115) test=Image.effect_mandelbrot((96,96),extent,n) score=test.entropy()-2*test.histogram()[0]/9216 if score>best: best,chosen=score,(extent,n) if score>4.5: break im=Image.effect_mandelbrot(size,*chosen) # type: ignore im=normalize(im.point(lambda i:255*(i/255)**.43)) # type: ignore if best<2.8: im=normalize(Image.blend(height(),im,.35)) else: im=normalize(Image.blend(im,noise(r.randint(3,8)),u(.08,.15))) result=C.multiply(paint(im),shade(im,2)) # Subtle, deterministic film grain; high-quality downsampling and JPEG. result=result.resize((512,512),Image.Resampling.LANCZOS) grain=Image.frombytes('L',(512,512),r.randbytes(512*512)).point(lambda i: i*.035+123) result=C.add(result,Image.merge('RGB',(grain,grain,grain)),offset=-127) out=io.BytesIO(); result.save(out,'JPEG',quality=94,subsampling=0) return out.getvalue()