예제 #1
0
def function(txt_path,font_path,prefix):
    path_0 = os.path.split(os.path.realpath(__file__))[0]
    path_0 = path.replace("\\", "/") + "/"
    strs = read_txt(txt_path).replace("。"," ").\
        replace(","," ").replace(";"," ").replace(":",":").\
        replace(";"," ").replace("• ","").replace("➢"," ").replace("◼","")
    template = Template(
        background=Image.new(mode="1", size=(9060, 12793), color=1),
        font_size=280,
        font=ImageFont.truetype(font_path),
        line_spacing=385,
        fill=0,  # 字体“颜色”
        left_margin=0,
        top_margin=850,
        right_margin=0,
        bottom_margin=500,
        word_spacing=15,
        line_spacing_sigma=0,  # 行间距随机扰动
        end_chars=",。",  # 防止特定字符因排版算法的自动换行而出现在行首
        font_size_sigma=10,  # 字体大小随机扰动
        word_spacing_sigma=30,  # 字间距随机扰动
    )
    images = handwrite(strs, template)
    for i, im in enumerate(images):
        assert isinstance(im, Image.Image)
        # im.show()
        im.save("cache/{}.png".format(i))
        temp = prefix + "({}).png".format(i + 1)
        blend_two_images(path_0 + "cache/{}.png".format(i), path_0 + "backgrounds/background.png", path_0 + "png_outcome/" + temp)
        reduce_resolution(path_0 + "png_outcome/" + temp,4.152153987167736)
    clear_cache()
예제 #2
0
def transform(file, text, flag, font_size, line_spacing, left_margin,
              top_margin, right_margin, bottom_margin):
    im = Image.open(BytesIO(file))
    if flag:
        im = im.transpose(Image.TRANSPOSE)
    template = Template(
        background=im,
        font_size=font_size,
        font=ImageFont.truetype("image/font/陈静的字.ttf"),
        line_spacing=line_spacing,
        fill=1,  # 字体“颜色”
        left_margin=left_margin,
        top_margin=top_margin,
        right_margin=right_margin,
        bottom_margin=bottom_margin,
        word_spacing=1,
        line_spacing_sigma=3,  # 行间距随机扰动
        font_size_sigma=2,  # 字体大小随机扰动
        word_spacing_sigma=5,  # 字间距随机扰动
        end_chars=",。",  # 防止特定字符因排版算法的自动换行而出现在行首
        perturb_x_sigma=2,  # 笔画横向偏移随机扰动
        perturb_y_sigma=2,  # 笔画纵向偏移随机扰动
        perturb_theta_sigma=0.05,  # 笔画旋转偏移随机扰动
    )
    images = handwrite(text, template)
    image = []
    for i, im in enumerate(images):
        assert isinstance(im, Image.Image)
        # im.show()

        im.save('image/test/{}.jpg'.format(i))
예제 #3
0
def startswitch():
    global font_size,line_spacing,word_spacing,line_spacing_sigma,font_size_sigma,perturb_y_sigma, perturb_x_sigma_show,perturb_theta_sigma
    template = Template(
        background=Image.new(mode="1", size=(2479, 3508), color=1),
        font_size=font_size,
        line_spacing=line_spacing,  # 行间距
        word_spacing=word_spacing,  # 字间距
        line_spacing_sigma=line_spacing_sigma,  # 行间距随机扰动
        word_spacing_sigma=word_spacing_sigma,  # 字间距随机扰动
        font_size_sigma=font_size_sigma,  # 字体大小随机扰动
        perturb_theta_sigma=perturb_theta_sigma,  # 笔画旋转偏移随机扰动
        perturb_x_sigma=perturb_x_sigma,  # 笔画横向偏移随机扰动
        perturb_y_sigma=perturb_y_sigma,  # 笔画纵向偏移随机扰动
        font=ImageFont.truetype(afont),
    )
    if font_size > line_spacing:
        tkinter.messagebox.showwarning(title = "stupid" , message = "字体大小必须小于行间距,你以为?")
    else:
        with open(path, "r", encoding='UTF-8') as f:
            text = f.read()
            images = handwrite(text, template)
            for i, im in enumerate(images):
                assert isinstance(im, Image.Image)
                isExists = os.path.exists("images")
                if not isExists:
                    os.mkdir("images")
                im.show()
                n = random.random()
                im.save("images/{}.jpg".format(n))
        os.system("start explorer images")
예제 #4
0
파일: GUI.py 프로젝트: jlx001/Windows
def nb(text, size, han):
    template = Template(background=Image.new(mode="1",
                                             size=(1024, 2048),
                                             color=1),
                        font_size=size,
                        font=ImageFont.truetype("中国式手写风字体.ttf"),
                        word_spacing=han)
    images = handwrite(text, template)
    index = int(0)
    for im in images:
        assert isinstance(im, Image.Image)
        strr = "save" + str(index) + ".png"
        im.save(strr, "PNG")
        index = index + 1
예제 #5
0
def convert(text, font, path):
    template = Template(
        background=Image.new(mode='1', size=(900, 1000), color=1),
        font_size=96,
        font=ImageFont.truetype(font),
        line_spacing=120,
        fill=0,
        left_margin=100,
        top_margin=100,
        right_margin=100,
        bottom_margin=100,
        word_spacing=12,
        line_spacing_sigma=4,
        font_size_sigma=12,
        word_spacing_sigma=3,
        perturb_x_sigma=4,
        perturb_y_sigma=4,
        perturb_theta_sigma=0.05,
    )
    images = handwrite(text, template)
    for _, im in enumerate(images):
        im.save(path)
예제 #6
0
from PIL import Image, ImageFont

from handright import Template, handwrite

text = "我能吞下玻璃而不伤身体。"
template = Template(
    background=Image.new(mode="1", size=(1024, 2048), color=1),
    font_size=100,
    font=ImageFont.truetype("path/to/my/font.ttf"),
)
for image in handwrite(text, template):
    image.show()
예제 #7
0
from PIL import Image, ImageFont
from handright import Template, handwrite

if __name__ == '__main__':
    size = 120
    with open('text.txt', encoding='utf8') as f:
        text = f.read()
    template = Template(
        background=Image.open(r'bg.png'),
        font_size=size,
        font=ImageFont.truetype(r"hxk2.ttf"),
        line_spacing=int(1.5 * size),
        fill=0,
        left_margin=int(0.5 * size),
        top_margin=int(size * 1.2),
        right_margin=int(0.5 * size),
        bottom_margin=int(size * 1.2),
        word_spacing=0,
        line_spacing_sigma=0.06 * size,
        font_size_sigma=0.05 * size,
        word_spacing_sigma=0.03 * size,
        end_chars=',.?!,。?!、‘“;:',
        perturb_x_sigma=0.02 * size,
        perturb_y_sigma=0.02 * size,
        perturb_theta_sigma=0.0005 * size,
    )
    with Pool() as p:
        images = handwrite(text, template, mapper=p.map)
        for i, im in enumerate(images):
            im.save("output/{}.jpg".format(i))
예제 #8
0
     print("text.txt无内容,请将要手写的内容写入text.txt并保!存!")
     print("请自行关闭退出")
     counter = 1
     while counter <= 10:
         counter = counter + 1
         counter = counter - 1
 else:
     template = Template(
         background=Image.new(mode="1", size=(3000, 4243), color=1),
         font_size=100,
         font=ImageFont.truetype("handwrite.ttf"),
         line_spacing=110,
         fill=0,  # 字体“颜色”
         left_margin=100,
         top_margin=100,
         right_margin=100,
         bottom_margin=100,
         word_spacing=-15,
         line_spacing_sigma=2,  # 行间距随机扰动
         font_size_sigma=2,  # 字体大小随机扰动
         word_spacing_sigma=2,  # 字间距随机扰动
         end_chars=",。.,abcdefghigklmnopqrstuvwxyz",  # 防止特定字符因排版算法的自动换行而出现在行首
         perturb_x_sigma=2,  # 笔画横向偏移随机扰动
         perturb_y_sigma=2,  # 笔画纵向偏移随机扰动
         perturb_theta_sigma=0.05,  # 笔画旋转偏移随机扰动
     )
     images = handwrite(text, template)
     for i, im in enumerate(images):
         assert isinstance(im, Image.Image)
         im.show()
         im.save("{}.png".format(i))
예제 #9
0
text = """
      我想用AI改变

        金融投资
"""

template = Template(
    background=Image.new(mode="1", size=(3000, 2000), color=1),
    font_size=150,
    font=ImageFont.truetype("Font.ttf"),
    line_spacing=150,
    fill=0,  # 字体“颜色”
    left_margin=100,
    top_margin=100,
    right_margin=100,
    bottom_margin=100,
    word_spacing=0,
    line_spacing_sigma=1,  # 行间距随机扰动
    font_size_sigma=1,  # 字体大小随机扰动
    word_spacing_sigma=1,  # 字间距随机扰动
    end_chars=",。",  # 防止特定字符因排版算法的自动换行而出现在行首
    perturb_x_sigma=1,  # 笔画横向偏移随机扰动
    perturb_y_sigma=1,  # 笔画纵向偏移随机扰动
    perturb_theta_sigma=0.005,  # 笔画旋转偏移随机扰动
)
images = handwrite(text, template)
for i, im in enumerate(images):
    assert isinstance(im, Image.Image)
    im.show()
    im.save("path/to/my/output/{}.webp".format(i))
예제 #10
0
    text = f.read()
    print(text)

length = 1445
width = 2060
line_num = 22
left = 140
right = 140
top = 162-10
bottom = 133+10
multiple = 6

template = Template(
    background=Image.new(mode="1", size=(length*multiple, width*multiple), color=1),
    font_size=(width-top-bottom)/line_num*0.7*multiple,
    font=ImageFont.truetype("./Fonts/陈旭东字体.ttf"),
    word_spacing=-5*multiple,  # 字符间距
    line_spacing=(width-top-bottom)/line_num*multiple,
    left_margin=left*multiple,
    top_margin=top*multiple,
    right_margin=right*multiple,
    bottom_margin=bottom*multiple
)
images = handwrite(text, template)
i = 23
for im in images:
    assert isinstance(im, Image.Image)
    im.save('./生成/' + str(i) + '.jpg')
    print(str(i)+".jpg generated")
    i += 1
예제 #11
0
    def run(self):
        if not self.checkOutputPath():  # 如果输出检查发生了错误 就停止
            return False
        if self.fontPathBox.currentText() == '':
            QMessageBox.information(
                self, '字体问题',
                '选择的字体为空,运行停止。请将 ttf 格式的字体放到本软件根目录的 fonts 文件夹中,再重新启动软件。')
            return False
        if self.useBackgroundImage:
            imageName = self.backgroundBox.currentText()  # 图片文件名
            if imageName == '':
                QMessageBox.warning(
                    self, '背景问题',
                    '你选择了使用图片背景,但背景图片文件为空,运行停止。请将 jpg、png 格式的图片放到本软件根目录的 backgrounds 文件夹中,再重新启动软件。'
                )
                return False
        if (self.backgroundSizeBoxX.text() == ''
                or self.backgroundSizeBoxY.text()
                == '') or (int(self.backgroundSizeBoxX.text()) == 0
                           or int(self.backgroundSizeBoxY.text()) == 0):
            QMessageBox.warning(self, '背景问题', '背景图片大小错误,请检查')
            return False
        inputText = self.inputBox.toPlainText()
        if inputText == '':
            print('没有输入文字')
            return False

        # 背景图片
        imageSizeX = int(self.backgroundSizeBoxX.text())
        imageSizeY = int(self.backgroundSizeBoxY.text())
        if not self.useBackgroundImage:
            background = Image.new(mode="RGB",
                                   size=(imageSizeX, imageSizeY),
                                   color=(255, 255, 255))
        else:
            imagePath = os.path.abspath('./backgrounds/' + imageName)
            try:
                background = Image.open(imagePath, 'r')
            except:
                QMessageBox.warning(self, '背景问题', '所选背景图片不是可打开的图片文件')
                return False
            width, height = background.size
            background = background.resize(
                (width * imageSizeX, height * imageSizeY),
                resample=Image.LANCZOS)

        # 字体文件
        fontName = self.fontPathBox.currentText()
        fontPath = os.path.abspath('./fonts/' + fontName)
        try:
            font = ImageFont.truetype(fontPath)
        except:
            QMessageBox.warning(self, '字体问题', '所选字体不是可打开的 ttf 字体文件')
            return False

        fontSize = self.fontSizeBox.value()
        fontColor = self.fontColorBox.color
        fontColor = (fontColor.red(), fontColor.green(), fontColor.blue())
        lineSpacing = self.lineSpacingBox.value()
        leftMargin = self.leftMarginBox.value()
        topMargin = self.topMarginBox.value()
        rightMargin = self.rightMarginBox.value()
        bottomMargin = self.bottomMarginBox.value()
        wordSpacing = self.wordSpacingBox.value()
        lineSpacingSigma = self.lindSpacingSigmaBox.value()
        fontSizeSigma = self.fontSizeSigmaBox.value()
        wordSpacingSigma = self.wordSpacingSigmaBox.value()
        endChars = self.endCharsBox.text()
        perturbXSigma = self.perturbXSigmaBox.value()
        perturbYSigma = self.perturbYSigmaBox.value()
        perturbThetaSigma = self.perturbThetaSigmaBox.value()
        showImage = self.showImageBox.isChecked()

        template = Template(
            background=background,
            font_size=fontSize,
            font=font,
            line_spacing=lineSpacing,
            fill=fontColor,  # 字体“颜色”
            left_margin=leftMargin,
            top_margin=topMargin,
            right_margin=rightMargin,
            bottom_margin=bottomMargin,
            word_spacing=wordSpacing,
            line_spacing_sigma=lineSpacingSigma,  # 行间距随机扰动
            font_size_sigma=fontSizeSigma,  # 字体大小随机扰动
            word_spacing_sigma=wordSpacingSigma,  # 字间距随机扰动
            end_chars=endChars,  # 防止特定字符因排版算法的自动换行而出现在行首
            perturb_x_sigma=perturbXSigma,  # 笔画横向偏移随机扰动
            perturb_y_sigma=perturbYSigma,  # 笔画纵向偏移随机扰动
            perturb_theta_sigma=perturbThetaSigma,  # 笔画旋转偏移随机扰动
        )

        window = ConsoleWindow(self.parent())
        thread = GenerateImagesThread()
        window.thread = thread
        thread.text = inputText
        thread.template = template
        thread.outputPath = self.outputPath
        thread.showImage = showImage
        thread.signal.connect(window.consolePrintBox.print)
        thread.start()
예제 #12
0
from PIL import Image, ImageFont

from handright import Template, handwrite

text = "啊啊啊啊巴巴爸爸啛啛喳喳顶顶顶顶柔柔弱弱共和国刚刚\n\r 顶顶顶顶灌灌灌灌哈哈哈哈斤斤计较坎坎坷坷啦啦啦啦噢噢噢噢噗噗噗噗噗"
template = Template(background=Image.new(mode="1", size=(3300, 1000), color=1),
                    font=ImageFont.truetype("C:\\font\\MiNiJianJiaShu-1.ttf",
                                            size=150),
                    word_spacing=2,
                    line_spacing_sigma=1,
                    font_size_sigma=2,
                    word_spacing_sigma=0.8,
                    perturb_x_sigma=3,
                    perturb_y_sigma=3,
                    perturb_theta_sigma=0.1)
images = handwrite(text, template)
for im in images:
    assert isinstance(im, Image.Image)
    im.save("C:\\font\\3.png")
    #im.show()
예제 #13
0
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 16 19:33:58 2020

@author: ZLT
"""

from PIL import Image, ImageFont

from handright import Template, handwrite

text = "我能吞下玻璃而不伤身体。"
template = Template(
    background=Image.new(mode="1", size=(1024, 2048), color=1),
    font_size=100,
    font=ImageFont.truetype(r"C:\Users\ZLT\AppData\Local\Microsoft\Windows\Fonts\李国夫手写体 常规.ttf"),
)
images = handwrite(text, template)
for im in images:
    assert isinstance(im, Image.Image)
    im.show()
예제 #14
0
파일: handwrite.py 프로젝트: 0x587/Tool
from PIL import Image, ImageFont

from handright import Template, handwrite

text = """
    You want to write.
"""

template = Template(
    background=Image.open(r'./background.jpg'),
    font_size=90,
    font=ImageFont.truetype(r"./hand.ttf"),
    line_spacing_sigma=2,
    word_spacing=4,
    left_margin=215,
    right_margin=200,
    top_margin=360,
    bottom_margin=260,
    line_spacing=135,
    perturb_x_sigma=4,
    perturb_y_sigma=4,
)
images = handwrite(text, template)
for i, im in enumerate(images):
    assert isinstance(im, Image.Image)
    im.show()
    im.save(r"./IMG{}.jpeg".format(i))
예제 #15
0
"""

imagex = Image.open("./pic/stationeryBackground.jpg")
width, height = imagex.size
imagex = imagex.resize((width * 2, height * 2), resample=Image.LANCZOS)

template = Template(
    background=imagex,
    font_size=140,
    font=ImageFont.truetype("./fonts/whx_2nd.ttf"),
    line_spacing=220,
    fill=0,  # 字体“颜色”
    left_margin=380,
    top_margin=370,
    right_margin=340,
    bottom_margin=340,
    word_spacing=12,
    line_spacing_sigma=7,  # 行间距随机扰动
    font_size_sigma=3,  # 字体大小随机扰动
    word_spacing_sigma=6,  # 字间距随机扰动
    end_chars=", 。",  # 防止特定字符因排版算法的自动换行而出现在行首
    perturb_x_sigma=2,  # 笔画横向偏移随机扰动
    perturb_y_sigma=2,  # 笔画纵向偏移随机扰动
    perturb_theta_sigma=0.05,  # 笔画旋转偏移随机扰动
)
images = handwrite(text, template)
for i, im in enumerate(images):
    assert isinstance(im, Image.Image)
    im.show()
    im.save("./output/{}.png".format(i))
예제 #16
0
        output_path = input()
        if os.path.isdir(output_path):
            break
        else:
            print('Output path "' + output_path + '" not exist!')

    template = Template(background=Image.new(mode="1",
                                             size=(2480, 3500),
                                             color=1),
                        font=ImageFont.truetype(font_file_path),
                        fill=0,
                        font_size=80,
                        font_size_sigma=2,
                        perturb_x_sigma=2,
                        perturb_y_sigma=2,
                        perturb_theta_sigma=0.05,
                        line_spacing=80,
                        line_spacing_sigma=3,
                        word_spacing=-30,
                        word_spacing_sigma=3,
                        left_margin=150,
                        top_margin=150,
                        right_margin=150,
                        bottom_margin=150,
                        end_chars=",。》?;:’”】}、!%),.>?;:]}!%)′″℃℉")
else:
    if os.path.isfile(sys.argv[1]):
        configuration_dict = json.load(open(sys.argv[1], encoding='utf-8'))
        text_file_path = configuration_dict['text_file_path']
        output_path = configuration_dict['output_path']
        background_color = configuration_dict['background_color']
예제 #17
0
# hard_writing_test.py
# coding: utf-8
from PIL import Image, ImageFont

from handright import Template, handwrite

text = """



"""
template = Template(
    background=Image.new(
        mode="1",
        size=(2480, 3508),
        color=1,
    ),
    line_spacing=100,
    left_margin=100,
    top_margin=100,
    right_margin=100,
    bottom_margin=100,
    word_spacing=10,
    font=ImageFont.truetype("00002.ttf", size=80),
)
images = handwrite(text, template)
for idx, im in enumerate(images):
    assert isinstance(im, Image.Image)
    # im.show()
    im.save("ex{:04d}.png".format(idx))