Пушыстый (_winnie) wrote,
Пушыстый
_winnie

Всё-таки научился делать Пц-букву на автомате из любого кириллического шрифта.

Результат:




upd: Ыц! Ыц! Ыц!


Скрипт:


#-*- encoding: UTF-8 -*-

import Image, ImageDraw, ImageFont, ImageChops
import sys

def MakePeCe(ttf_font_path):
    font_size = 400

    n = 800
    img_sha, img_tcha, img_pe, img_tcha_minus_sha, img_pe_shift = [Image.new("L", (n, n), 0) for _ in range(5)]
    font = ImageFont.truetype(ttf_font_path, font_size)

    def PutLetter(img, letter):
        dr = ImageDraw.ImageDraw(img)
        dr.text((100, 100), letter, font = font, fill=0xff)

    PutLetter(img_sha, u'Ш')
    PutLetter(img_tcha, u'Щ')
    PutLetter(img_pe, u'П')

    img_tcha_minus_sha = ImageChops.subtract(img_tcha, img_sha)

    def LastPixel(img):
        _, y1, _, y2 = img.getbbox()
        ym = (y1+y2)/2
        median_line = img.crop((0, ym, img.size[1], ym+1))
        return median_line.getbbox()[2]

    img_pe_shift.paste(img_pe, (LastPixel(img_sha) - LastPixel(img_pe), 0))


    result = ImageChops.add(img_pe_shift, img_tcha_minus_sha)
    result = result.point(lambda x : 0 if (x < 200) else x)
    x0, y0, x1, y1 = result.getbbox()
    result = result.crop((x0 - 32, y0 - 32, x1 + 32, y1 + 32))

    w, h = result.size
    result = result.resize((w/8, h/8), Image.ANTIALIAS)
    return ImageChops.invert(result)

def MergeImgs(imgs):
    sizes = zip(*[i.size for i in imgs])
    width = sum(sizes[0]) + len(imgs) + 1
    height = max(sizes[1]) + 2
    result = Image.new("L", (width, height))
    for i, img in enumerate(imgs):
        result.paste(img, (1 + i + sum(sizes[0][0:i]), 1))
    return result

imgs = [MakePeCe(font + '.ttf') for font in ['georgiai', 'comic', 'pala', 'times', 'cour', ]]
MergeImgs(imgs).show()


Суть алгоритма: По разнице между Ш и Щ нахожу хвостик, затем добавляю этот хвостик к букве П.
Tags: imaging, release, soft-dev
Subscribe
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 15 comments