当前位置:网站首页>将一张图片以自己输入文字方式组成
将一张图片以自己输入文字方式组成
2022-07-21 10:42:00 【一只想飞的小蚂蚁】
from PIL import Image, ImageDraw, ImageFont
origin_name='D:\python测试\\3.jpg'#原图片名
dst_name='D:\python测试\\222.jpg'#目标图片名
CHILD_W = CHILD_H = 16
txt = '您好' # 内容组成
font = ImageFont.truetype("D:\python测试\Alibaba-PuHuiTi-Bold.ttf", CHILD_W) # 字体及大小
if __name__ == '__main__':
imgSrc = Image.open(origin_name) # 打开原图像
w, h = imgSrc.size # 原图像宽高
imageChild = Image.new("RGB", (CHILD_W, CHILD_H)) # 新建子图
imageDst = Image.new("RGB", (CHILD_W * w, CHILD_H * h)) # 新建子图
textW, textH = font.getsize("迷") # 取单个文字的宽高信息
offsetX = (CHILD_W - textW) >> 1 # 居中
offsetY = (CHILD_H - textH) >> 1
charIndex = 0 # 记录文字下标
draw = ImageDraw.Draw(imageChild) # 取最小绘图对象,用于绘制文字
for y in range(h):
for x in range(w):
draw.rectangle((0, 0, CHILD_W, CHILD_H), fill="lightgray")
draw.text((offsetX, offsetY), txt[charIndex], font=font, fill=imgSrc.getpixel((x, y)))
imageDst.paste(imageChild,(x*CHILD_W,y*CHILD_H) )
charIndex=(charIndex+1)%len(txt)
imageDst.save(dst_name)
边栏推荐
- 百度地图、高德地图、腾讯地图三位一体地图定位开发
- Specific use of typescript in cocos
- The whole process of installing mysql5.7 in centos7 and the answers to various questions
- Array / slice of go
- RNN之LSTM和GRU_Attention机制
- 简易shader实践记录4-流光效果
- Unity surface shader foundation memo
- 第106篇 Compound 中的 Governance
- likeshop单商户SAAS商城系统无限多开
- 问一下REITs是什么呢?请问,手机开户股票开户安全吗?
猜你喜欢
百度地图、高德地图、腾讯地图三位一体地图定位开发
游戏中的数学之角与弧度、三角函数
Data too long for column ‘......‘ at row 2
Random forest thought
Alibaba cloud toolkit one click automatic deployment of jar packages
【JS 逆向百例】某公共資源交易網,公告 URL 參數逆向分析
Simple shader practice record 5-Screen gradient
简易特效制作ByUnityParticleSystem
极化SAR——极化椭圆
Data too long for column ‘......‘ at row 2
随机推荐
第107篇 Compound 中的 InterestRateModel
Custom annotation verification parameters
Batch efficient feature decomposition of eccv2022 small and medium-sized matrices
Harbor warehouse construction and simple use
Linux Installation mysql8 (Xiaobai must see)
Tips for using efficient development tools
Polarimetric SAR - Polarimetric ellipse
Use okgo to update, download and install the software version
C#超大整数的加减法
Policy mode rewriting if else advanced use
How to make the demand unable to go online smoothly as scheduled (V) business acceptance stage
第93篇 合约间调用(二)
list集合根据对象属性去重
How to make the demand unable to go online smoothly as scheduled (II) demand stage
(板子)筛素数--线性筛和埃氏筛
Go处理字符串到合法文件名
ECCV 2022 | multi domain long tailed distributed learning to solve the problem of unbalanced domain generalization
Lamda expression uses
Self defined multiple code of station B
怎样才能让需求无法如期顺利上线(二)需求阶段