Python生成数字图片代码分享
作者:往来白丁 时间:2023-03-02 04:27:18
本文向大家分享了几段Python生成数字图片的代码,喜欢的朋友可以参考。具体如下:
最终版本
# -*- coding:utf-8 -*-
from PIL import Image,ImageFont,ImageDraw,ImageFilter
import random
import os
import time
class Code(object):
def __init__(self, imgSize=(35,35),\
fontSize=25, bgColor=(255,)*4, fontColor=(0,0,0)):
self.imgSize = imgSize
self.fontSize = fontSize
self.bgColor = bgColor
self.fontColor = fontColor
def setFontSize(self, size):
self.fontSize = size;
def getDigit(self, digit):
return str(digit)
def getPannel(self):
pannel = Image.new('RGBA',self.imgSize,self.bgColor)
return pannel
def getFont(self, fontFile='./Arial.ttf'):
return ImageFont.truetype(fontFile, self.fontSize)
def getTextPos(self, digit, font):
text = self.getDigit(digit)
textWidth,textHeight = font.getsize(text);
imgWidth,imgHeight = self.imgSize
textPos = ((imgWidth-textWidth)/2, (imgHeight-textHeight)/2)
return textPos
def rotateImg(self,image,angle=0, expand=0):
rot = image.rotate(angle, expand)
fff = Image.new('RGBA',rot.size,self.bgColor)
image = Image.composite(rot, fff, rot)
return image
def createImg(self, digit, font, angle):
codeImg = Image.new('RGBA',self.imgSize,self.bgColor)
draw = ImageDraw.Draw(codeImg);
text = self.getDigit(digit)
textPos = self.getTextPos(digit, font)
draw.text(xy=textPos,text=text,fill=self.fontColor,font=font)
codeImg = self.rotateImg(codeImg,angle)
return codeImg
def saveImg(self, img, savePath, imgName):
img.save(savePath+'/'+imgName)
def createPath(path):
if not os.path.exists(path):
os.makedirs(path)
def createImages(code,rootPath='./images',digitList=range(10), fontSizeList=range(18,30),\
angleList=[(45,90),(-45,45),(-45,-90)]):
for index,angles in enumerate(angleList):
if index==0:
angleRange = '-90_-45'
elif index == 1:
angleRange = '-45_45'
else:
angleRange = '45_90'
anglepath = os.path.join(rootPath, angleRange)
createPath(anglepath)
for digit in digitList:
digitpath = os.path.join(anglepath, 'x'+str(digit))
createPath(digitpath)
for size in fontSizeList:
angle = round(random.uniform(angles[0], angles[1]),5)
code.setFontSize(size)
imgName = str(digit)+'_'+str(size)+'_'+str(angle)+'.jpg'
img = code.createImg(digit, code.getFont(),angle)
code.saveImg(img, digitpath, imgName)
if __name__ == '__main__':
imagesPath = './images'
if os.path.exists(imagesPath):
os.system('rm -rf '+imagesPath)
os.mkdir(imagesPath)
code = Code()
for i in range(1000):
createImages(code)
# test ...
# code = Code()
# img = code.createImg(5,code.getFont(),0)
# code.saveImg(img, savePath, 'test.jpg')
# img.show()
print 'hello'
# -*- coding:utf-8 -*-
from PIL import Image,ImageFont,ImageDraw,ImageFilter
import random
import os
class Captcha(object):
def __init__(self,size=(20,24),fontSize=20):
self.font = ImageFont.truetype('./fonts/Arial.ttf',fontSize)
self.size = size
self.image = Image.new('RGBA',self.size,(255,)*4)
# self.texts = self.randNum(1)
self.text = ''
def rotate(self, angle):
# rot = self.image.rotate(random.randint(-10,10),expand=0)
rot = self.image.rotate(angle,expand=0)
fff = Image.new('RGBA',rot.size,(255,)*4)
self.image = Image.composite(rot,fff,rot)
def randColor(self):
self.fontColor = (random.randint(0,250),random.randint(0,250),random.randint(0,250))
# def randNum(self,bits):
# return ''.join(str(random.randint(0,9)) for i in range(bits))
def setNum(self, num):
return num;
def write(self,text,x,y):
draw = ImageDraw.Draw(self.image)
draw.text((x,y),text,fill=self.fontColor,font=self.font)
def writeNum(self, num, angle):
x = 2
y = -2
self.text = num
self.fontColor = (0, 0, 0)
self.write(num, x, y)
self.rotate(angle)
return self.text
# character
# xplus = 15
# for text in self.texts:
# self.randColor()
# self.fontColor = (0, 0, 0)
# self.write(text, x, y)
# self.rotate(angle)
# self.rotate(random.randint(-10,10))
# x += xplus
# return self.texts
def save(self, save_path):
# self.image.save('captcha.jpg')
self.image.save(save_path)
pic_root_path = './pic'
if not os.path.exists(pic_root_path):
os.mkdir(pic_root_path)
angles = [(45,90),(-45,45),(-90,-45)]
for i in range(10):
pic_num_path = os.path.join(pic_root_path, 'pic'+str(i))
if not os.path.exists(pic_num_path):
os.mkdir(pic_num_path)
for angle_i in angles:
angle_name = str(angle_i[0])+'_'+str(angle_i[1])
pic_angle_path = os.path.join(pic_num_path, angle_name)
if not os.path.exists(pic_angle_path):
os.mkdir(pic_angle_path)
for angle in range(angle_i[0], angle_i[1]):
for fontsize in range(25,28):
img = Captcha(size=(20, 24), fontSize=fontsize)
num = img.writeNum(str(i), angle)
img_name = str(i)+'_'+str(fontsize)+'_'+str(angle)+'.bmp'
save_path = os.path.join(pic_angle_path, img_name)
img.save(save_path)
# img = Captcha()
# num = img.writeNum(str(i), random.randint(-90,-45))
# img_name = str(i)+'.jpg'
# pic_path = './pic'+str(i)
# if not os.path.exists(pic_path):
# os.mkdir(pic_path)
# save_path = os.path.join(pic_path, img_name)
# save_path = os.path.join(pic_root_path, save_path)
# img.save(save_path)
# img.image.show()
# img.save()
随机生成各种size和旋转角度的单个数字图片
# -*- coding:utf-8 -*-
from PIL import Image,ImageFont,ImageDraw,ImageFilter
import random
import os
import time
class Captcha(object):
def __init__(self,size=(20,24),fontSize=20):
self.font = ImageFont.truetype('./fonts/Arial.ttf',fontSize)
self.size = size
self.image = Image.new('RGBA',self.size,(255,)*4)
self.text = ''
def rotate(self, angle):
rot = self.image.rotate(angle,expand=0)
fff = Image.new('RGBA',rot.size,(255,)*4)
self.image = Image.composite(rot,fff,rot)
def randColor(self):
self.fontColor = (random.randint(0,250),random.randint(0,250),random.randint(0,250))
def setNum(self, num):
return num;
def write(self,text,x,y):
draw = ImageDraw.Draw(self.image)
draw.text((x,y),text,fill=self.fontColor,font=self.font)
def writeNum(self, num, angle):
x = 2
y = -2
self.text = num
self.fontColor = (0, 0, 0)
self.write(num, x, y)
self.rotate(angle)
return self.text
def save(self, save_path):
# self.image = self.image.filter(ImageFilter.EDGE_ENHANCE_MORE) #滤镜,边界加强
self.image.save(save_path)
pic_root_path = './pic'
if not os.path.exists(pic_root_path):
os.mkdir(pic_root_path)
angles = [(45,90),(-45,45),(-90,-45)]
for i in range(10):
pic_num_path = os.path.join(pic_root_path, 'x'+str(i))
if not os.path.exists(pic_num_path):
os.mkdir(pic_num_path)
for angle_i in angles:
angle_name = str(angle_i[0])+'_'+str(angle_i[1])
pic_angle_path = os.path.join(pic_num_path, angle_name)
if not os.path.exists(pic_angle_path):
os.mkdir(pic_angle_path)
for fontsize in range(25,29):
for j in range(2500):
# Keep 5 decimal places
angle = round(random.uniform(angle_i[0], angle_i[1]),5)
img = Captcha(size=(20, 24), fontSize=fontsize)
num = img.writeNum(str(i), angle)
img_name = 'x'+str(j)+'_'+str(fontsize)+'_'+str(angle)+'_'+str(num)+'.jpg'
save_path = os.path.join(pic_angle_path, img_name)
img.save(save_path)
文字居中
# -*- coding:utf-8 -*-
from PIL import Image,ImageFont,ImageDraw,ImageFilter
import random
import os
import time
imgWidth = 20
imgHeight = 24
fontSize = 28
backGroundColor = (255,)*4
fontColor = (0,)*3
text = '0'
font = ImageFont.truetype('./Arial.ttf', fontSize)
codeimg = Image.new('RGBA',(imgWidth,imgHeight), backGroundColor)
imagePath = './codes'
if not os.path.exists(imagePath):
os.mkdir(imagePath)
textWidth, textHeight = font.getsize(text)
textLeft = (imgWidth-textWidth)/2
textTop = (imgHeight-textHeight)/2
draw = ImageDraw.Draw(codeimg)
draw.text(xy=(textLeft,textTop),text=text,fill=fontColor,font=font)
rot = codeimg.rotate(90,expand=0)
codeimg.rotate
fff = Image.new('RGBA', rot.size,backGroundColor)
codeimg = Image.composite(rot, fff, rot)
codeimg.show()
# codeimg.save('./codes/test.jpg')
以上就是本文关于Python生成数字图片代码分享的全部内容,希望对大家有所帮助。欢迎参阅:Python列表删除的三种方法代码分享、Python文件的读写和异常代码示例等,有问题可以随时留言,欢迎大家交流讨论。
来源:http://blog.csdn.net/zwx2445205419/article/details/73481409
标签:python,生成图片
0
投稿
猜你喜欢
python logging类库使用例子
2023-10-31 11:17:11
python3 requests库实现多图片爬取教程
2023-11-22 15:37:40
python实现二分查找算法
2022-08-30 21:58:54
python 文件读写和数据清洗
2021-02-10 22:58:43
浅谈Pytorch torch.optim优化器个性化的使用
2023-12-19 08:47:12
python 命名规范知识点汇总
2023-04-23 16:03:38
Pygame游戏开发之太空射击实战图像精灵下篇
2022-10-20 16:09:06
Python实现3行代码解简单的一元一次方程
2023-08-29 19:14:50
mysql中影响数据库性能的因素讲解
2024-01-28 02:34:46
解决pip install xxx报错SyntaxError: invalid syntax的问题
2023-02-13 23:53:07
让SQL Server数据库自动执行管理任务(二)
2009-03-20 11:40:00
白鸦:Design IT. (1),迭代的设计
2008-08-27 21:11:00
Python如何截图保存的三种方法(小结)
2023-08-24 22:17:19
mac安装mysql初始密码忘记怎么办
2024-01-16 20:42:45
asp中获取内容中所有图片与获取内容中第一个图片的代码
2011-02-20 10:51:00
利用Python破解斗地主残局详解
2021-06-04 06:16:49
Python调用adb命令实现对多台设备同时进行reboot的方法
2022-08-06 02:40:45
sql server通过脚本进行数据库压缩全备份的方法【推荐】
2024-01-14 18:26:39
Centos7下源码安装Python3 及shell 脚本自动安装Python3的教程
2023-01-11 13:45:04
个人网站与动网整合非官方方法
2009-07-05 18:42:00