Django图像处理:单词分为两行

时间:2019-03-26 06:45:59

标签: python django

我具有用于在图像上绘制文本的python函数。文本会打断中间单词,因此1/2单词在一行上,而1/2在另一行上。我们如何确保不会发生这种情况? 我希望整个词都换行。

下面是图片示例

enter image description here

这是我的代码:

from PIL import ImageFont, Image, ImageDraw
from django.contrib.auth import get_user_model
from entities.obituary.forms import ObituaryForm

User = get_user_model()


def imprint(obituary_model, obiuary_sample_model):
    normal_font_path = 'static/fonts/PTF55F.ttf'
    heading_font_path = 'static/fonts/hind-regular.ttf'
    normal_font = ImageFont.truetype(normal_font_path, 18)
    normal_font2 = ImageFont.truetype(normal_font_path, 24)
    heading_font = ImageFont.truetype(heading_font_path, 30)

    text_color = (
    obiuary_sample_model.text_color[0], obiuary_sample_model.text_color[1], obiuary_sample_model.text_color[2], 255)

    image = Image.open(obituary_model.image)
    text_layer = Image.new("RGBA", image.size, (0, 0, 0, 0))
    text_draw = ImageDraw.Draw(text_layer)
    # Texts to imprint
    name_text = obituary_model.name
    life_span_text = obituary_model.date_of_birth.strftime(
        '%B %d, %Y') + ' to ' + obituary_model.date_of_death.strftime('%B %d, %Y')
    location_title_text = 'Funeral Services'
    viewing_title_text = 'Viewing Info'
    location_text = obituary_model.address  # 'Areaxyz round about\nCity,State'
    viewing_location_text = obituary_model.viewing_address  # 'Areaxyz round about\nCity,State'
    funeral_date_time_text = obituary_model.funeral_date.strftime(
        '%B %d, %Y,') + ' ' + obituary_model.funeral_time.strftime('%-I:%M%p')
    viewing_date_time_text = obituary_model.viewing_date.strftime(
        '%B %d, %Y,') + ' ' + obituary_model.viewing_time.strftime('%-I:%M%p')
    family_title_text = 'Surviving Immediate Family'
    family_detail_text = make_multi_line(obituary_model.family_info, 25)
    # donation_title_text = 'Ways To Contribute'
    # donation_info_text = obituary_model.memorial_donation_info
    donation_info_text = 'Ways To Contribute\n' + obituary_model.memorial_donation_info
    description_text = make_multi_line(obituary_model.message, 35)

    location_text = make_multi_line(location_text, 15)
    viewing_location_text = make_multi_line(viewing_location_text, 15)

    name_text_pos = (obiuary_sample_model.name_text_pos[0], obiuary_sample_model.name_text_pos[1])
    life_span_text_pos = (obiuary_sample_model.life_span_text_pos[0], obiuary_sample_model.life_span_text_pos[1])
    viewing_location_title_pos = (
        obiuary_sample_model.viewing_location_title_text_pos[0], obiuary_sample_model.viewing_location_title_text_pos[1])
    viewing_location_text_pos = (obiuary_sample_model.viewing_location_text_pos[0], obiuary_sample_model.viewing_location_text_pos[1])
    viewing_date_time_text_pos = (
        obiuary_sample_model.viewing_date_time_text_pos[0], obiuary_sample_model.viewing_date_time_text_pos[1])
    location_title_pos = (
        obiuary_sample_model.location_title_text_pos[0], obiuary_sample_model.location_title_text_pos[1])
    location_text_pos = (obiuary_sample_model.location_text_pos[0], obiuary_sample_model.location_text_pos[1])
    funeral_date_time_text_pos = (
        obiuary_sample_model.funeral_date_time_text_pos[0], obiuary_sample_model.funeral_date_time_text_pos[1])
    family_title_text_pos = (
        obiuary_sample_model.family_title_text_pos[0], obiuary_sample_model.family_title_text_pos[1])
    family_detail_text_pos = (
        obiuary_sample_model.family_detail_text_pos[0], obiuary_sample_model.family_detail_text_pos[1])
    # donation_title_text_pos = (
    #     obiuary_sample_model.donation_title_text_pos[0], obiuary_sample_model.donation_title_text_pos[1])
    donation_info_text_pos = (
        obiuary_sample_model.donation_info_text_pos[0], obiuary_sample_model.donation_info_text_pos[1])
    description_text_pos = (obiuary_sample_model.description_text_pos[0], obiuary_sample_model.description_text_pos[1])

    add_text_on_background(text_draw, name_text, name_text_pos, heading_font, text_color=text_color)
    add_text_on_background(text_draw, life_span_text, life_span_text_pos, heading_font, text_color=text_color)
    add_text_on_background(text_draw, viewing_title_text, viewing_location_title_pos, normal_font, True,
                           text_color=text_color)
    add_text_on_background(text_draw, viewing_location_text, viewing_location_text_pos, normal_font, True,
                           text_color=text_color)
    add_text_on_background(text_draw, viewing_date_time_text, viewing_date_time_text_pos, normal_font,
                           text_color=text_color)
    add_text_on_background(text_draw, location_title_text, location_title_pos, normal_font, True, text_color=text_color)
    add_text_on_background(text_draw, location_text, location_text_pos, normal_font, True, text_color=text_color)
    add_text_on_background(text_draw, funeral_date_time_text, funeral_date_time_text_pos, normal_font,
                           text_color=text_color)
    add_text_on_background(text_draw, family_title_text, family_title_text_pos, heading_font, text_color=text_color)
    add_text_on_background(text_draw, family_detail_text, family_detail_text_pos, normal_font2, True,
                           text_color=text_color)
    # add_text_on_background(text_draw, donation_title_text, donation_title_text_pos, heading_font, text_color=text_color)
    add_text_on_background(text_draw, donation_info_text, donation_info_text_pos, heading_font, True,
                           text_color=text_color)
    add_text_on_background(text_draw, description_text, description_text_pos, normal_font, True, text_color=text_color)

    profile_image = Image.open(obituary_model.profile_image)
    add_image_on_background(image, profile_image,
                            (obiuary_sample_model.profile_image_pos[0], obiuary_sample_model.profile_image_pos[1]),
                            (obiuary_sample_model.profile_image_size[0], obiuary_sample_model.profile_image_size[1]),
                            obiuary_sample_model.mask
                            )

    # Path to store temporary imprinted image, which will be used to store in actual db
    dir_path = 'static/images'
    file_name = 'card.png'
    imprinted_image = Image.composite(text_layer, image, text_layer)
    imprinted_image.save("%s/%s" % (dir_path, file_name))
    return "%s/%s" % (dir_path, file_name)


def add_text_on_background(text_draw, text, text_pos, font, multi_line=False, align='center',
                           text_color=(0, 0, 0, 255)):
    w, h = text_draw.textsize(text, font)
    w = (text_pos[0] - (w / 2))
    if multi_line:
        text_draw.multiline_text((w, text_pos[1]), text, font=font, align=align, fill=text_color)
    else:
        text_draw.text((w, text_pos[1]), text, font=font, fill=text_color)


def add_image_on_background(bg_image, profile_image, profile_image_pos, profile_image_size, mask):
    mask = Image.open(mask).convert("RGBA")
    mask = mask.resize((profile_image_size[0], profile_image_size[1]))
    profile_image_pos = (profile_image_pos[0], profile_image_pos[1])
    profile_image = profile_image.resize((profile_image_size[0], profile_image_size[1]))
    x, y = profile_image.size
    x = (profile_image_pos[0] - (x / 2))
    y = (profile_image_pos[1] - (y / 2))
    bg_image.paste(profile_image, (int(x), int(y)), mask=mask)


def make_multi_line(msg, characters_per_line):
    total_length = len(msg)
    if total_length < characters_per_line:
        return msg
    i = 0
    while i < total_length - 1:
        if (i + characters_per_line - 1) < (total_length - 1):
            i = i + characters_per_line - 1
            while msg[i] != ' ' and i < total_length - 2:
                i += 1
            msg = msg[:i] + '\n' + msg[i + 1:]
            total_length -= 1
        else:
            msg = msg[:i] + '\n' + msg[i + 1:]
            break
    return msg

在没有空余的空间时,我们如何保持一致?

0 个答案:

没有答案