def generate(model_name_dict_sort, input_data, model_file, font_file, image_file, save_dir):
# first match model
model_file_path = os.path.join(model_file, list(model_name_dict_sort.keys())[0])
for name in os.listdir(model_file_path):
if name.split('.')[-1] == 'txt': #front.txt and back.txt
with open(os.path.join(model_file_path, name), 'r') as f_read:
data = json.load(f_read)
w, h = int(data['pageList']['diyWorkAreaWidth']), int(data['pageList']['diyWorkAreaHeight'])
img = Image.new('RGB', (w, h), (255, 255, 255))
draw = ImageDraw.Draw(img)
data_item = data['pageList']['content']['itemList']
for item in data_item:
item_name = switch_case(item['markuuid'])
if item_name in key_word_filter:
if item_name in list(input_data.keys()):
if item['media']['mediaType'] == 'text':
#picture generate
font = ImageFont.truetype(os.path.join(font_file, font_id[item['media']['font']['id']]), \
int(item['media']['font']['pointSize']))
font_color = hex_to_rgb(item['media']['font']['fontColor'])
if font_color == (255, 255, 255):
font_color = (0, 0, 0)
draw.text((item['location']['x'], item['location']['y']), input_data[item_name], \
font_color, font=font)
print('*****write:', input_data[item_name])
#json generate
else:
#picture generate
pic_full_name = input_data[item_name]
pic_name, pic_ext = os.path.splitext(pic_full_name)
if pic_ext == '.xml':
pic_path = os.path.join(image_file, 'svgs', pic_name + '.png')
else:
pic_path = os.path.join(image_file, 'images', pic_full_name)
small_img = Image.open(pic_path)
resized_img = small_img.resize((int(item['location']['width']), int(item['location']['height'])), \
resample=Image.LANCZOS)
img.paste(resized_img, (int(item['location']['x']), int(item['location']['y'])))
#json generate
else:
#json generate
pass
img.save(os.path.join(save_dir, name.split('.')[0] + '_' + str(list(model_name_dict_sort.keys())[0]) + '_generate.jpg'))
增加对齐方式:
font = ImageFont.truetype(os.path.join(font_file, font_id[item['media']['font']['id']]),
pointSize)
font_color = hex_to_rgb(item['media']['font']['fontColor'])
if font_color == (255, 255, 255):
font_color = (0, 0, 0)
if item['media']['font']['gravity'] == 'right':
location_width = item['location']['width']
if input_data[item_name].count('\n') > 0:
font_w = location_width
else:
font_w, font_h = font.getsize(input_data[item_name])
draw.text((item['location']['x'] + location_width - font_w, item['location']['y']), input_data[item_name], \
font_color, font=font, align=item['media']['font']['gravity'])
elif item['media']['font']['gravity'] == 'center':
location_width = item['location']['width']
if input_data[item_name].count('\n') > 0:
font_w = location_width
else:
font_w, font_h = font.getsize(input_data[item_name])
draw.text((int((item['location']['x'] + (location_width - font_w) / 2)), item['location']['y']), \
input_data[item_name], font_color, font=font, align=item['media']['font']['gravity'])
else:
draw.text((item['location']['x'], item['location']['y']), input_data[item_name], \
font_color, font=font, align=item['media']['font']['gravity'])