如何将 Font Awesome 转成 PNG 图标 详细教程 含源代码_html/css_WEB-ITnose
程序员文章站
2024-01-22 20:49:22
...
最近因为项目上需要用到这个字体图标,但是它里面的许多也不能完全满足项目需要,因此就考虑将这个图标导出,然后自己再添加一些其他图标使用
搜索了些解决方案,如:http://www.oschina.net/translate/how-to-convert-font-awesome-to-png-icons,但是上面的步骤过于简单,一个新手完全不会使用,比如Python自己就一点都没接触过,因此自己走了许多弯路。
本文一方面记录以便以后自己再次使用,最重要的是将我自己的每个步骤详细的展现给有需要的大家,希望对大家有帮助。
好 还请留个好评
本文涉及所有资源可以到我的百度云下载:
font-awesome-to-png
第一步 下载图标字体源文件:
包含以下文件:
第二步 安装python-2.7.9(我电脑是64位的,截图也是64位,实际上只能安装32位,64位图标处理库(第二部会安装以处理图片)找不到安装路径,安装过程是一样的)
第三步 安装图形处理库
安装完成后:菜单》全部程序》 Python 2.7发现已经安装成功
第四部 你下载文件里面有个 目录font-awesome-to-png 里面有将会使用的 脚本文件和字体文件(也是你第一步下载的文件)
来点其他参数
颜色变了,证明其他参数也是可靠的,大功告成。
源代码1 font-awesome-to-png.py:
#!/usr/bin/env python## font-awesome-to-png.py## Exports Font Awesome icons as PNG images.## Copyright (c) 2012-2014 Michal Wojciechowski (http://odyniec.net/)## Font Awesome - http://fortawesome.github.com/Font-Awesome#import sys, argparse, refrom os import path, access, R_OKfrom PIL import Image, ImageFont, ImageDraw# Support Unicode literals with both Python 2 and 3if sys.version > sys.stderr, ("Error: CSS file (%s) can't be opened" % (filename)) exit(1) is_icon = re.compile(u("\.fa-(.*):before,?")) for rule in stylesheet.rules: selector = rule.selector.as_css() for match in is_icon.finditer(selector): name = match.groups()[0] for declaration in rule.declarations: if declaration.name == u"content": val = declaration.value.as_css() if val.startswith('"') and val.endswith('"'): val = val[1:-1] new_icons[name] = uchr(int(val[1:], 16)) return new_iconsif __name__ == '__main__': parser = argparse.ArgumentParser( description="Exports Font Awesome icons as PNG images.") parser.add_argument("icon", type=str, nargs="+", help="The name(s) of the icon(s) to export (or \"ALL\" for all icons)") parser.add_argument("--color", type=str, default="black", help="Color (HTML color code or name, default: black)") parser.add_argument("--filename", type=str, help="The name of the output file (it must end with \".png\"). If " + "all files are exported, it is used as a prefix.") parser.add_argument("--font", type=str, default="fontawesome-webfont.ttf", help="Font file to use (default: fontawesome-webfont.ttf)") parser.add_argument("--css", type=str, default="", action=LoadCSSAction, help="Path to the CSS file defining icon names (instead of the " + "predefined list)") parser.add_argument("--list", nargs=0, action=ListAction, help="List available icon names and exit") parser.add_argument("--list-update", nargs=0, action=ListUpdateAction, help=argparse.SUPPRESS) parser.add_argument("--size", type=int, default=16, help="Icon size in pixels (default: 16)") args = parser.parse_args() icon = args.icon size = args.size font = args.font color = args.color if args.font: if not path.isfile(args.font) or not access(args.font, R_OK): print >> sys.stderr, ("Error: Font file (%s) can't be opened" % (args.font)) exit(1) if args.icon == [ "ALL" ]: # Export all icons selected_icons = sorted(icons.keys()) else: selected_icons = [] # Icon name was given for icon in args.icon: # Strip the "icon-" prefix, if present if icon.startswith("icon-"): icon = icon[5:] if icon in icons: selected_icons.append(icon) else: print >> sys.stderr, "Error: Unknown icon name (%s)" % (icon) sys.exit(1) for icon in selected_icons: if len(selected_icons) > 1: # Exporting multiple icons -- treat the filename option as name prefix filename = (args.filename or "") + icon + ".png" else: # Exporting one icon if args.filename: filename = args.filename else: filename = icon + ".png" print("Exporting icon \"%s\" as %s (%ix%i pixels)" % (icon, filename, size, size)) export_icon(icon, size, filename, font, color)
帮助文档 README.md:
Font Awesome to PNG===================This program allows you to extract the awesome[Font Awesome] (http://fortawesome.github.com/Font-Awesome/) icons as PNG imagesof specified size.### Usage font-awesome-to-png.py [-h] [--color COLOR] [--filename FILENAME] [--font FONT] [--css CSS] [--list] [--size SIZE] icon [icon ...] positional arguments: icon The name(s) of the icon(s) to export (or "ALL" for all icons) optional arguments: --color COLOR Color (HTML color code or name, default: black) --filename FILENAME The name of the output file (it must end with ".png"). If all files are exported, it is used as a prefix. --font FONT Font file to use (default: fontawesome-webfont.ttf) --css CSS Path to the CSS file defining icon names (instead of the predefined list) --list List available icon names and exit --size SIZE Icon size in pixels (default: 16) hidden optional arguments: --list-update List available icon names and codes in format suitable for updating the program source.To use the program, you need the Font Awesome TTF file, which is available in[Font Awesome Github repository] (https://github.com/FortAwesome/Font-Awesome).The internal icon list is matched to Font Awesome 4.1.0. To use a later/differentversion, use font-awesome.css from the Font Awesome GitHub repository.### ExamplesExport the "play" and "stop" icons as 24x24 pixels images: font-awesome-to-png.py --size 24 play stopExport the asterisk icon as 32x32 pixels image, in blue: font-awesome-to-png.py --size 32 --color blue asteriskExport all icons as 16x16 pixels images: font-awesome-to-png.py ALL
上一篇: Mysql-MHA高可用实验测试
下一篇: Oracle特殊情况下数字四舍五入问题