python生成图片
添加噪声和滤波
from PIL import Image,ImageDraw,ImageFont
from text_renderer_master.libs.config import load_config
from text_renderer_master.textrenderer.noiser import Noiser
from utils2 import create_repeat_number
import cv2
import random
import numpy as np
import gevent
import math
from gevent import monkey
def add_noise_blur(filename):
"""
添加噪声和滤波
:param filename:
:return:
"""
im_new = cv2.imdecode(np.fromfile('/hulk/chenwanhui/wangyunmin/recogModel/train/images/' + filename, dtype=np.uint8), cv2.IMREAD_GRAYSCALE)
cfg = load_config('./text_renderer_master/configs/default.yaml')
noise = Noiser(cfg)
im_new = noise.apply(im_new)
num = [0, 1, 2]
random_num = num[random.randint(0, 2)]
if random_num == 0:
cv2.imwrite('/hulk/chenwanhui/wangyunmin/recogModel/train/images/' + filename, im_new)
elif random_num == 1:
ks = [3, 5]
ksize = random.choice(ks)
sigmas = [0, 1, 2, 3, 4, 5, 6, 7]
sigma = 0
if ksize <= 3:
sigma = random.choice(sigmas)
im_new = cv2.GaussianBlur(im_new, (ksize, ksize), sigma)
cv2.imwrite('/hulk/chenwanhui/wangyunmin/recogModel/train/images/' + filename, im_new)
else:
ks = [2, 3]
kernel = random.choice(ks)
im_new = cv2.blur(im_new, (kernel, kernel))
cv2.imwrite('/hulk/chenwanhui/wangyunmin/recogModel/train/images/' + filename, im_new)
修改图片size
def resize_img(filename):
"""
修改图片size
:param filename:
:return:
"""
img = cv2.imread('/hulk/chenwanhui/wangyunmin/recogModel/train/images/' + filename)
scale = 4 / 7
new_img = cv2.resize(img, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA)
cv2.imwrite('/hulk/chenwanhui/wangyunmin/recogModel/train/images/' + filename, new_img)
生成图片并写上内容
def create_square_image(num):
im = Image.open('templates/square_template2.jpg')
draw = ImageDraw.Draw(im)
font_type1 = ['font/simfang.ttf', 'font/simhei.ttf', 'font/simkai.ttf', 'font/simsun.ttc']
font_type2 = ['font/arial.ttf', 'font/ARIALN.TTF', 'font/calibri.ttf', 'font/tahoma.ttf', 'font/times.ttf', 'font/simfang.ttf', 'font/simsun.ttc']
ttfont1 = ImageFont.truetype(font_type1[random.randint(0, len(font_type1) - 1)], 22)
ttfont2 = ImageFont.truetype(font_type2[random.randint(0, len(font_type2) - 1)], 30)
x = random.randint(30, 180)
y = random.randint(0, 17)
content = '%09d'%random.randint(0, 999999999)
draw.text((x, y), content, fill='black', font=ttfont2)
filename = 'img_' + str(num) + '.jpg'
im.save('/hulk/chenwanhui/wangyunmin/recogModel/train/images/' + filename)
try:
img = cv2.imread('/hulk/chenwanhui/wangyunmin/recogModel/train/images/' + filename)
scale = 2/3
new_image = cv2.resize(img, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA)
cv2.imwrite('/hulk/chenwanhui/wangyunmin/recogModel/train/images/' + filename, new_image)
except:
raise Exception('create template1 error')
im = Image.open('/hulk/chenwanhui/wangyunmin/recogModel/train/images/' + filename)
box = (0, 0, 280, 32)
region = im.crop(box)
im1 = Image.new('RGB', (280, 32), color='white')
im1.paste(region, (0, 0))
im1.save('/hulk/chenwanhui/wangyunmin/recogModel/train/images/' + filename)
with open('output.txt', 'a') as f:
f.write(filename)
f.write(' ')
f.write("¥" + content + "\n")
add_noise_blur(filename)
def create_image(num):
im = Image.open('88.jpg')
m = random.randint(-70, 70)
y = 120 - m
box = (27, y, 307, y + 32)
region = im.crop(box)
im1 = Image.new('RGB', (280, 32), color='white')
im1.paste(region, (0, 0))
draw = ImageDraw.Draw(im1)
font_type1 = ['font/simfang.ttf', 'font/simhei.ttf', 'font/simkai.ttf', 'font/simsun.ttc']
font_type2 = ['font/arial.ttf', 'font/calibri.ttf', 'font/tahoma.ttf', 'font/times.ttf', 'font/simfang.ttf',
'font/simsun.ttc']
ttfont1 = ImageFont.truetype(font_type1[random.randint(0, len(font_type1) - 1)], 25)
ttfont2 = ImageFont.truetype(font_type2[random.randint(0, len(font_type2) - 1)], 20)
list = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '.']
content = ''
for i in range(10):
content += list[random.randint(0, len(list)-1)]
position_x = random.randint(100, 120)
position_y = random.randint(2, 8)
draw.text((position_x, position_y), content, fill='black', font=ttfont1)
filename = 'img_' + str(num) + '.jpg'
im1.save('/hulk/chenwanhui/wangyunmin/recogModel/train/images/' + filename)
with open('output.txt', 'a') as f:
f.write(filename)
f.write(' ')
f.write(content + "\n")
add_noise_blur(filename)
def create_template():
im = Image.open('templates/square_template4.jpg')
x = 290
box = (x, 0, x + 25, 48)
region = im.crop(box)
im2 = Image.open('templates/square_template1.jpg')
box = (0, 0, 420, 48)
region2 = im2.crop(box)
im3 = Image.new('RGB', (420, 48), color='white')
im3.paste(region, (0, 0))
im3.paste(region2, (25, 0))
im3.save('templates2/square_template3.jpg')
去除图片中的字
def add_image_template1(image1, image2):
"""
删除需要替换的部分,构建合同模板
:param image:
:return:
"""
im1 = Image.open(image1)
im2 = Image.open(image2)
size = im1.size
width = size[0]
length = size[1]
# 利用小矩形粘贴覆盖(距离需要完全一致)
# box = (0,0,243,40)
# region = im.crop(box)
# im.paste(region, (1028,324,1271,364))
# im.save('./000.jpg')
# 利用颜色覆盖区域
im1.paste('white', (1028, 324, 1271, 364))
im1.paste('white', (451, 370, 538, 403))
im1.paste('white', (453, 409, 853, 442))
im1.paste('white', (455, 452, 1114, 486))
im1.paste('white', (452, 492, 587, 520))
im1.save('example_images/111.jpg')
im3 = Image.open('example_images/111.jpg')
im4 = Image.open('example_images/222.jpg')
box = (0, 0, width, 1070)
region = im3.crop(box)
im4.paste(region, (0, 0, width, 1070))
im4.save('img_template1.jpg')