如何使用Python生成带有大写字母图像的高质量Windows图标?

发布于 2024-11-30 10:01:11 字数 207 浏览 3 评论 0 原文

我想要获得 26 个文件(对于初学者来说):A.ico、B.ico、... Z.ico,它们由 16x16 256 色图像和 32x32 256 色图像组成,其中文本是黑色的,字体是……比如 Calibri,尺寸是最适合正方形的。如果可能的话,我想使用 Python 图像库来做到这一点。

我知道我可能可以通过其他方式获取图标,但我想学习更好地使用 PIL,并希望将其用于手头的任务。

I want to get 26 files (for starters): A.ico, B.ico, ... Z.ico, where they are composed of 16x16 256-color image, and a 32x32 256-color image, where the color of the text is black, and the font is ... say Calibri, and the size - whatever fits best into the square. I would like to do this using Python Image Library if possible.

I know that I can probably get my icons through other means, but I would like to learn to use the PIL better, and would like to use it for the task at hand.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白龙吟 2024-12-07 10:01:11

从一张大的空白图像开始,然后在其中心绘制字符。找到字符的边缘并从图像中提取包含所有字符的正方形。使用thumbnail 函数和ANTIALIAS 选项将其缩小到所需的 16x16 或 32x32 大小。然后将颜色数量减少到 256 种:如何使用 PIL 减少调色板

Start with a large blank image and draw the character on the center of it. Find the edges of the character and extract a square from the image that includes all of the character. Use the thumbnail function with the ANTIALIAS option to reduce it to the 16x16 or 32x32 size required. Then reduce the number of colors to 256: How to reduce color palette with PIL

江湖彼岸 2024-12-07 10:01:11

这是基于@Mark Ransom 的回答。谢谢你,马克!
这对我有用,尽管“黑化”功能仍然不完善。
我仍然需要弄清楚如何在不使用 icotool< 的情况下创建 .ico 文件/a> 对于 Linux。

# This script generates icon files from the two images.
# Uses Python 2.6.5, uses the Python Imaging Library

import Image
import ImageDraw
import ImageFont

letters = [chr(i + ord('A')) for i in range(26)]
default_huge =  ImageFont.load_default()
large_size = 1000
lim = large_size + 1

# Apparently I can use the same size for the font.
calibri_huge = ImageFont.truetype("calibri.ttf", large_size)

def crop_letter(img):
    minx, maxx, miny, maxy = lim, -lim, lim, -lim
    for x in range(large_size):
        for y in range(large_size):
            if sum(img.getpixel((x, y))) == 3 * 255: continue
            # Else, found a black pixel
            minx = min(minx, x)
            maxx = max(maxx, x)
            miny = min(miny, y)
            maxy = max(maxy, y)
    return img.crop(box = (minx, miny, maxx, maxy))

# This works for me 95% of the time
def blackify(color):
    return 255 * (color > 240)

for letter in letters:
    # A bit wasteful, but I have plenty of RAM.
    img = Image.new("RGB", (large_size, large_size), "white")
    draw = ImageDraw.Draw(img)
    draw.text((0,0), letter, font = calibri_huge, fill = "black")

    img32 = crop_letter(img)
    img16 = img32.copy()

    img32.thumbnail((32, 32), Image.ANTIALIAS)
    img16.thumbnail((16, 16), Image.ANTIALIAS)

    img32 = Image.eval(img32, blackify)
    img16 = Image.eval(img16, blackify)

##  Not needed
##    # Apparently this is all it takes to get 256 colors.
##    img32 = img32.convert('P')
##    img16 = img16.convert('P')

    img32.save('icons3/{0}32x32.bmp'.format(letter))
    img16.save('icons3/{0}16x16.bmp'.format(letter))
#    break

print('DONE!')

This is based on the answer by @Mark Ransom. Thank you, Mark!
This worked for me, though the 'blackify' function is imperfect still.
I still need to figure out how to create an .ico file without using icotool for Linux.

# This script generates icon files from the two images.
# Uses Python 2.6.5, uses the Python Imaging Library

import Image
import ImageDraw
import ImageFont

letters = [chr(i + ord('A')) for i in range(26)]
default_huge =  ImageFont.load_default()
large_size = 1000
lim = large_size + 1

# Apparently I can use the same size for the font.
calibri_huge = ImageFont.truetype("calibri.ttf", large_size)

def crop_letter(img):
    minx, maxx, miny, maxy = lim, -lim, lim, -lim
    for x in range(large_size):
        for y in range(large_size):
            if sum(img.getpixel((x, y))) == 3 * 255: continue
            # Else, found a black pixel
            minx = min(minx, x)
            maxx = max(maxx, x)
            miny = min(miny, y)
            maxy = max(maxy, y)
    return img.crop(box = (minx, miny, maxx, maxy))

# This works for me 95% of the time
def blackify(color):
    return 255 * (color > 240)

for letter in letters:
    # A bit wasteful, but I have plenty of RAM.
    img = Image.new("RGB", (large_size, large_size), "white")
    draw = ImageDraw.Draw(img)
    draw.text((0,0), letter, font = calibri_huge, fill = "black")

    img32 = crop_letter(img)
    img16 = img32.copy()

    img32.thumbnail((32, 32), Image.ANTIALIAS)
    img16.thumbnail((16, 16), Image.ANTIALIAS)

    img32 = Image.eval(img32, blackify)
    img16 = Image.eval(img16, blackify)

##  Not needed
##    # Apparently this is all it takes to get 256 colors.
##    img32 = img32.convert('P')
##    img16 = img16.convert('P')

    img32.save('icons3/{0}32x32.bmp'.format(letter))
    img16.save('icons3/{0}16x16.bmp'.format(letter))
#    break

print('DONE!')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文