예제 #1
0
    def __init__(self, text, font, max_width):
        self.text = text
        self.text_lines = [
            ' '.join([w.strip() for w in l.split(' ') if w])
            for l in text.split('\n') if l
        ]
        self.font = font
        self.max_width = max_width

        self.draw = ImageDraw.Draw(Image.new(mode='RGB', size=(100, 100)))

        self.space_width = self.draw.textsize(text=' ', font=self.font)[0]
예제 #2
0
파일: search.py 프로젝트: ziminyuri/VDA
def _create_image_for_pair(rows, model_m, pair):
    length = len(rows)
    if length > 10:
        return

    w, h = 480, 480
    distance = 30
    interval = int((w - distance) / (length + 3))  # Интервал между стрелок

    if abs(float(rows[0][1])) > abs(float(rows[length - 1][1])):
        max = float(rows[0][1])

    else:
        max = float(rows[length - 1][1])

    min_value = str(round(rows[-1][1], 2))

    h_scale = h - 80  # ДЛя графиков чтобы оставить место подписи

    for row in rows:
        if (max < 0 and float(row[1]) > 0):
            row[1] = int((float(row[1])) * (h_scale / 2 - 10) / max * (-1))
        elif (max < 0 and float(row[1]) < 0):
            row[1] = int((float(row[1])) * (h_scale / 2 - 10) / max * (-1))
        else:
            row[1] = int((float(row[1])) * (h_scale / 2 - 10) / max)

    im = Image.new('RGB', (w, h), (195, 197, 200))
    na = np.array(im)

    h_begin = int(h_scale / 2)
    # Оси
    na = cv2.arrowedLine(na, (3, h_begin), (w - 5, h_begin), (0, 0, 0), 4)
    na = cv2.arrowedLine(na, (distance, h - 50), (distance, 5), (0, 0, 0), 4)

    h_end = int(h_scale / 2 - (rows[-1][1]))
    na = cv2.line(na, (15, h_end), (45, h_end), (0, 0, 0), 4)

    distance += interval * 2
    for row in rows:

        h_end = int(h_scale / 2 - (row[1]))
        na = cv2.arrowedLine(na, (distance, h_begin), (distance, h_end),
                             (0, 0, 0), 4)
        distance += interval

    path = f'{MEDIA_ROOT}/files/models/{str(model_m.model.id)}/original_snod/modification/{str(model_m.id)}/images/{str(pair.id)}.png'
    Image.fromarray(na).save(path)

    # Делаем подписи
    img = Image.open(path)
    idraw = ImageDraw.Draw(img)
    path_font = f'{MEDIA_ROOT}/fonts/9041.ttf'
    font = ImageFont.truetype(path_font, size=18)

    distance = 30
    distance += interval * 2
    for row in rows:
        text = str((row[0]) + 1)
        if float(row[1]) > 0:
            idraw.text((distance, int(h_scale / 2 + 50)),
                       text,
                       font=font,
                       fill='#000000')
        elif float(row[1]) == 0:
            idraw.ellipse(
                [distance - 10, h_begin - 10, distance + 10, h_begin + 10],
                fill='#000000')
            idraw.text((distance, int(h_scale / 2 - 50)),
                       text,
                       font=font,
                       fill='#000000')
        else:
            idraw.text((distance, int(h_scale / 2 - 50)),
                       text,
                       font=font,
                       fill='#000000')
        distance += interval

    text = pair.option_1.name
    length = len(text) * 9
    idraw.text((w - 15 - length, h - 40),
               pair.option_2.name,
               font=font,
               fill='#000000')
    idraw.text((15, h - 40), text, font=font, fill='#000000')

    idraw.text((w - 45, h / 2), 'Ox', font=font, fill='#000000')
    idraw.text((60, 15), 'Oy', font=font, fill='#000000')

    # Подписываем риски
    h_end = int(h_scale / 2 - (rows[-1][1]))

    idraw.text((45, h_end), min_value, font=font, fill='#000000')

    img.save(path)