简体   繁体   English

如何减少 ImageDraw.text 中使用的 colors 的数量

[英]How do I reduce the number of colors used in ImageDraw.text

I have a folder with.ttf and.otf fonts and would like to write them on my ImageDraw object but with NO shading.我有一个带有 .ttf 和 .otf fonts 的文件夹,并想将它们写在我的 ImageDraw object 上,但没有阴影。 A single RGB only.仅单个 RGB。 I have tried bitmap fonts but they A) don't look nice and B) use more than one color anyway.我已经尝试过 bitmap fonts 但它们 A)看起来不太好,B)无论如何使用不止一种颜色。

I have read that there is a library for converting.bdf to.pil.我读过有一个用于将.bdf 转换为.pil 的库。 If I convert arial.ttf to arial.bdf and then to arial.pil, will this be what I'm looking for?如果我将 arial.ttf 转换为 arial.bdf 然后再转换为 arial.pil,这会是我要找的吗? The text will almost always be dropped onto a background--so should I consider writing the text first on a blank canvas, do a color reduction, and then paste that canvas onto my background?文本几乎总是被放到背景上——所以我是否应该考虑先在空白 canvas 上写文本,进行颜色减少,然后将 canvas 粘贴到我的背景上?

I have previously made this program using Java and it writes text very nicely on my bitmaps.我之前使用 Java 制作了这个程序,它在我的位图上写得非常好。 One color, symmetrical, etc. Image below.一种颜色,对称等。下图。

Java字体渲染

Below are the two attempts with python.以下是 python 的两次尝试。 The blockier one is a bitmap font, the other is regular arial.ttf.比较大的一个是 bitmap 字体,另一个是常规的 arial.ttf。

Python 宋体位图

蟒蛇宋体

Here is my code:这是我的代码:

def personalize(self):

    names = self.personalize_entry.get("1.0", 'end-1c').split('\n')
    num_names = len(names)
    num_grids = math.ceil(num_names/20)
    answer = ask_grid_background()
    separator = Image.new('RGB', (473, 1), color_dict['P'])
    background = Image.new('RGB', (473, 821), color_dict['.'])

    if answer:
        showinfo("Bitmap", "Give me the design.")
        file_path = filedialog.askopenfilename()
        filename = path_leaf(file_path)
        filename = filename[:-4]
        __, __, center = read(file_path)
        if center == 0:
            messagebox.showinfo("Hmmm", f"I couldn't find a center...are you sure this is a basic set up?")
            return False
        img = Image.open(file_path)
        size_num = img.size
        section = img.crop((5, (size_num[1] - 55 - center), 478, (size_num[1] - center - 15)))
        background.paste(separator, (0, 0))
        for i in range(20):
            background.paste(section, (0, (41 * i + 1)))
            background.paste(separator, (0, (41 * i) + 41))
    else:
        background.paste(separator, (0, 0))
        for i in range(20):
            # background.paste(section,(0,(41*i+1)))
            background.paste(separator, (0, (41 * i) + 41))

    draw = ImageDraw.Draw(background)
    fnt = ImageFont.truetype("Fonts/PIXEAB__.ttf",36)
    draw.text((10, 10), names[0], font=fnt, fill=(0, 0, 0))
    background.show()

ImageDraw has an undocumented member fontmode , which can be set to '1' (cf. Pillow's image modes ) to turn off the anti-aliasing of the rendered text. ImageDraw有一个未记录的成员fontmode ,可以将其设置为'1' (参见 Pillow 的图像模式)以关闭渲染文本的抗锯齿。

Let's compare common rendered text, draw.fontmode is implicitly set to 'L' :让我们比较一下常见的渲染文本, draw.fontmode被隐式设置为'L'

from PIL import Image, ImageDraw, ImageFont

image = Image.new('RGB', (800, 200), (255, 255, 255))
draw = ImageDraw.Draw(image)
font = ImageFont.truetype('arial.ttf', 150)
draw.text((10, 10), 'Hello World', font=font, fill=(0, 0, 0))
image.save('image.png')

带抗锯齿

Now, let's explicitly set draw.fontmode = '1' :现在,让我们明确设置draw.fontmode = '1'

from PIL import Image, ImageDraw, ImageFont

image = Image.new('RGB', (800, 200), (255, 255, 255))
draw = ImageDraw.Draw(image)
draw.fontmode = '1'
font = ImageFont.truetype('arial.ttf', 150)
draw.text((10, 10), 'Hello World', font=font, fill=(0, 0, 0))
image.save('image.png')

没有抗锯齿

Et voilà – no anti-aliasing, all pixels are solid black.等等——没有抗锯齿,所有像素都是纯黑色的。

----------------------------------------
System information
----------------------------------------
Platform:      Windows-10-10.0.16299-SP0
Python:        3.9.1
PyCharm:       2021.1
Pillow:        8.2.0
----------------------------------------

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM