当前位置: 首页 > news >正文

python 制作3d立体隐藏图

生成文件的3d图,例子:
文字:
在这里插入图片描述
隐藏图:
在这里插入图片描述

使用建议:
1、建议不用中文,因为中文太复杂,生成立体图效果不好。
2、需要指定FONT_PATH,为一个ttf文件,linux在/usr/share/fonts目录下,windows在C:\Windows\Fonts
3、建议字体TEXT_SIZE调大,不然不好分辨
4、指定的TEXT不要太长
5、多试几次,找到合适的纹理

from PIL import Image, ImageDraw, ImageFont
from random import *
import numpy as np
from io import BytesIOFONT_PATH = '/usr/share/fonts/dejavu/DejaVuSans.ttf'   # 字体位置,linux在/usr/share/fonts/,windows
TEXT = 'love yannis'
TEXT_SIZE = 100 # 字体大小
WIDTH = 100 + int(len(TEXT) * TEXT_SIZE / 1.8)
HEIGHT = 256
SHIFT = 4 # 叠加位移# 生成纹理
def clamp(n, smallest, largest):return max(smallest, min(n, largest))def rand_ratio(base=1, delta=1):return delta * randint(1,100)*1.0/100 + baserand_ratios = [rand_ratio(), rand_ratio(), rand_ratio()]color_starts = [randint(0,200), randint(0,200), randint(0,200)]def rand_color(i):return clamp(randint(color_starts[i],int(rand_ratios[i] * color_starts[i])), 0, 255) def rand_colors():return (rand_color(0),rand_color(1),rand_color(2))perodic_x = 40
perodic_y = 40
texture = np.zeros((perodic_x, perodic_y, 3))
same_to_last_x = [False] * perodic_x
same_to_last_y = [False] * perodic_y
for x in range(0, perodic_x):same_to_last_x[x] =  random() < 0.1
for y in range(0, perodic_y):same_to_last_y[y] =  random() < 0.3texture[0][0] = rand_colors()for x in range(0,perodic_x):if same_to_last_x[x]:texture[x][0] = texture[x-1][0]else:texture[x][0] = rand_colors()# 纹理平铺
for x in range(0, perodic_x):for y in range(0,perodic_y):if same_to_last_y[y]:texture[x][y] = texture[x][y-1]elif same_to_last_x[x]:texture[x][y] = texture[x-1][y]else:texture[x][y] = rand_colors()# 生成纹理平铺图片
img = Image.new('RGB', (WIDTH, HEIGHT), color = 'white')
img_draw = ImageDraw.Draw(img) 
for x in range(0, WIDTH):for y in range(0,HEIGHT):color = texture[x % perodic_x][y % perodic_y].astype(int).tolist()img_draw.point([x,y], fill = tuple(color))# 生成文字
text = Image.new('RGB', (WIDTH, HEIGHT), color = 'black')
text_draw = ImageDraw.Draw(text) 
font = ImageFont.truetype(FONT_PATH, size=TEXT_SIZE)
text_draw.text((50,(HEIGHT-TEXT_SIZE)/2), TEXT, font=font, fill='white')# 文字叠加
img_mat = np.asarray(img)
text_mat = np.asarray(text)for x in range(0, WIDTH - SHIFT):for y in range(0, HEIGHT):if text_mat[y][x][0] != 0:img_draw.point([x,y], fill = tuple(img_mat[y, x - SHIFT].tolist()))
text.save('../tmp2.png', 'png')
img.save('../tmp.png', 'png')
http://www.lryc.cn/news/252686.html

相关文章:

  • layui+ssm实现数据批量删除
  • 国产AI边缘计算盒子,双核心A55丨2.5Tops算力
  • C++作业4
  • 计算机网络(二)| 物理层上 | 数据通信基础知识 调制 频率范围 信噪比
  • [STM32-1.点灯大师上线】
  • Web测试自动化工具Selenium的使用
  • VUE2+THREE.JS 按照行动轨迹移动人物模型并相机视角跟随人物
  • Hadoop YARN组件
  • Java架构师技术架构路线
  • guacamole docker一键部署脚本
  • 蓝桥杯算法心得——想吃冰淇淋和蛋糕(dp)
  • LLM之RAG实战(二):使用LlamaIndex + Metaphor实现知识工作自动化
  • 【容器】Docker打包Linux操作系统迁移
  • redis基本数据结构
  • Learning Normal Dynamics in Videos with Meta Prototype Network 论文阅读
  • Unity 关于SpriteRenderer 和正交相机缩放
  • HarmonyOS应用开发者基础认证考试(98分答案)
  • Ubuntu20.04 Kimera Semantic运行记录
  • 服务器RAID系统的常见故障,结合应用场景谈谈常规的维修处理流程
  • 计算机网络——数据链路层-封装成帧(帧定界、透明传输-字节填充,比特填充、MTU)
  • MySQL笔记-第03章_基本的SELECT语句
  • FTP服务文件上传失败,错误码553的排故过程
  • 音频录制软件哪个好?帮助你找到最合适的一款
  • 9.Unity搭建HTTP服务器
  • C# 热键注册工具类
  • nodejs微信小程序+python+PHP天天网站书城管理系统的设计与实现-计算机毕业设计推荐
  • Hive环境准备[重点学习]
  • 软件工程 室友整理
  • JVM==>图解字节码指令
  • MISRA C 2012 标准浅析