Font Awesome 字体为您提供可缩放矢量图标,它可以被定制大小、颜色、阴影以及任何可以用CSS的样式。正因为是矢量图标,所以可以转换成任意大小尺寸的图标给我们的APP使用。
环境:Windows7,python-2.7.9.msi,fontawesome-webfont.ttf,PIL-1.1.7.win32-py2.7
将下面代码保存为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, re from os import path, access, R_OK from PIL import Image, ImageFont, ImageDraw # Support Unicode literals with both Python 2 and 3 if sys.version < '3': import codecs def u(x): return codecs.unicode_escape_decode(x)[0] def uchr(x): return unichr(x) else: def u(x): return x def uchr(x): return chr(x) # Mapping of icon names to character codes icons = { "adjust": u("\uf042"), "adn": u("\uf170"), "align_center": u("\uf037"), "align_justify": u("\uf039"), "align_left": u("\uf036"), "align_right": u("\uf038"), "ambulance": u("\uf0f9"), "anchor": u("\uf13d"), "android": u("\uf17b"), "angle_double_down": u("\uf103"), "angle_double_left": u("\uf100"), "angle_double_right": u("\uf101"), "angle_double_up": u("\uf102"), "angle_down": u("\uf107"), "angle_left": u("\uf104"), "angle_right": u("\uf105"), "angle_up": u("\uf106"), "apple": u("\uf179"), "archive": u("\uf187"), "arrow_circle_down": u("\uf0ab"), "arrow_circle_left": u("\uf0a8"), "arrow_circle_o_down": u("\uf01a"), "arrow_circle_o_left": u("\uf190"), "arrow_circle_o_right": u("\uf18e"), "arrow_circle_o_up": u("\uf01b"), "arrow_circle_right": u("\uf0a9"), "arrow_circle_up": u("\uf0aa"), "arrow_down": u("\uf063"), "arrow_left": u("\uf060"), "arrow_right": u("\uf061"), "arrow_up": u("\uf062"), "arrows": u("\uf047"), "arrows_alt": u("\uf0b2"), "arrows_h": u("\uf07e"), "arrows_v": u("\uf07d"), "asterisk": u("\uf069"), "automobile": u("\uf1b9"), "backward": u("\uf04a"), "ban": u("\uf05e"), "bank": u("\uf19c"), "bar_chart_o": u("\uf080"), "barcode": u("\uf02a"), "bars": u("\uf0c9"), "beer": u("\uf0fc"), "behance": u("\uf1b4"), "behance_square": u("\uf1b5"), "bell": u("\uf0f3"), "bell_o": u("\uf0a2"), "bitbucket": u("\uf171"), "bitbucket_square": u("\uf172"), "bitcoin": u("\uf15a"), "bold": u("\uf032"), "bolt": u("\uf0e7"), "bomb": u("\uf1e2"), "book": u("\uf02d"), "bookmark": u("\uf02e"), "bookmark_o": u("\uf097"), "briefcase": u("\uf0b1"), "btc": u("\uf15a"), "bug": u("\uf188"), "building": u("\uf1ad"), "building_o": u("\uf0f7"), "bullhorn": u("\uf0a1"), "bullseye": u("\uf140"), "cab": u("\uf1ba"), "calendar": u("\uf073"), "calendar_o": u("\uf133"), "camera": u("\uf030"), "camera_retro": u("\uf083"), "car": u("\uf1b9"), "caret_down": u("\uf0d7"), "caret_left": u("\uf0d9"), "caret_right": u("\uf0da"), "caret_square_o_down": u("\uf150"), "caret_square_o_left": u("\uf191"), "caret_square_o_right": u("\uf152"), "caret_square_o_up": u("\uf151"), "caret_up": u("\uf0d8"), "certificate": u("\uf0a3"), "chain": u("\uf0c1"), "chain_broken": u("\uf127"), "check": u("\uf00c"), "check_circle": u("\uf058"), "check_circle_o": u("\uf05d"), "check_square": u("\uf14a"), "check_square_o": u("\uf046"), "chevron_circle_down": u("\uf13a"), "chevron_circle_left": u("\uf137"), "chevron_circle_right": u("\uf138"), "chevron_circle_up": u("\uf139"), "chevron_down": u("\uf078"), "chevron_left": u("\uf053"), "chevron_right": u("\uf054"), "chevron_up": u("\uf077"), "child": u("\uf1ae"), "circle": u("\uf111"), "circle_o": u("\uf10c"), "circle_o_notch": u("\uf1ce"), "circle_thin": u("\uf1db"), "clipboard": u("\uf0ea"), "clock_o": u("\uf017"), "cloud": u("\uf0c2"), "cloud_download": u("\uf0ed"), "cloud_upload": u("\uf0ee"), "cny": u("\uf157"), "code": u("\uf121"), "code_fork": u("\uf126"), "codepen": u("\uf1cb"), "coffee": u("\uf0f4"), "cog": u("\uf013"), "cogs": u("\uf085"), "columns": u("\uf0db"), "comment": u("\uf075"), "comment_o": u("\uf0e5"), "comments": u("\uf086"), "comments_o": u("\uf0e6"), "compass": u("\uf14e"), "compress": u("\uf066"), "copy": u("\uf0c5"), "credit_card": u("\uf09d"), "crop": u("\uf125"), "crosshairs": u("\uf05b"), "css3": u("\uf13c"), "cube": u("\uf1b2"), "cubes": u("\uf1b3"), "cut": u("\uf0c4"), "cutlery": u("\uf0f5"), "dashboard": u("\uf0e4"), "database": u("\uf1c0"), "dedent": u("\uf03b"), "delicious": u("\uf1a5"), "desktop": u("\uf108"), "deviantart": u("\uf1bd"), "digg": u("\uf1a6"), "dollar": u("\uf155"), "dot_circle_o": u("\uf192"), "download": u("\uf019"), "dribbble": u("\uf17d"), "dropbox": u("\uf16b"), "drupal": u("\uf1a9"), "edit": u("\uf044"), "eject": u("\uf052"), "ellipsis_h": u("\uf141"), "ellipsis_v": u("\uf142"), "empire": u("\uf1d1"), "envelope": u("\uf0e0"), "envelope_o": u("\uf003"), "envelope_square": u("\uf199"), "eraser": u("\uf12d"), "eur": u("\uf153"), "euro": u("\uf153"), "exchange": u("\uf0ec"), "exclamation": u("\uf12a"), "exclamation_circle": u("\uf06a"), "exclamation_triangle": u("\uf071"), "expand": u("\uf065"), "external_link": u("\uf08e"), "external_link_square": u("\uf14c"), "eye": u("\uf06e"), "eye_slash": u("\uf070"), "facebook": u("\uf09a"), "facebook_square": u("\uf082"), "fast_backward": u("\uf049"), "fast_forward": u("\uf050"), "fax": u("\uf1ac"), "female": u("\uf182"), "fighter_jet": u("\uf0fb"), "file": u("\uf15b"), "file_archive_o": u("\uf1c6"), "file_audio_o": u("\uf1c7"), "file_code_o": u("\uf1c9"), "file_excel_o": u("\uf1c3"), "file_image_o": u("\uf1c5"), "file_movie_o": u("\uf1c8"), "file_o": u("\uf016"), "file_pdf_o": u("\uf1c1"), "file_photo_o": u("\uf1c5"), "file_picture_o": u("\uf1c5"), "file_powerpoint_o": u("\uf1c4"), "file_sound_o": u("\uf1c7"), "file_text": u("\uf15c"), "file_text_o": u("\uf0f6"), "file_video_o": u("\uf1c8"), "file_word_o": u("\uf1c2"), "file_zip_o": u("\uf1c6"), "files_o": u("\uf0c5"), "film": u("\uf008"), "filter": u("\uf0b0"), "fire": u("\uf06d"), "fire_extinguisher": u("\uf134"), "flag": u("\uf024"), "flag_checkered": u("\uf11e"), "flag_o": u("\uf11d"), "flash": u("\uf0e7"), "flask": u("\uf0c3"), "flickr": u("\uf16e"), "floppy_o": u("\uf0c7"), "folder": u("\uf07b"), "folder_o": u("\uf114"), "folder_open": u("\uf07c"), "folder_open_o": u("\uf115"), "font": u("\uf031"), "forward": u("\uf04e"), "foursquare": u("\uf180"), "frown_o": u("\uf119"), "gamepad": u("\uf11b"), "gavel": u("\uf0e3"), "gbp": u("\uf154"), "ge": u("\uf1d1"), "gear": u("\uf013"), "gears": u("\uf085"), "gift": u("\uf06b"), "git": u("\uf1d3"), "git_square": u("\uf1d2"), "github": u("\uf09b"), "github_alt": u("\uf113"), "github_square": u("\uf092"), "gittip": u("\uf184"), "glass": u("\uf000"), "globe": u("\uf0ac"), "google": u("\uf1a0"), "google_plus": u("\uf0d5"), "google_plus_square": u("\uf0d4"), "graduation_cap": u("\uf19d"), "group": u("\uf0c0"), "h_square": u("\uf0fd"), "hacker_news": u("\uf1d4"), "hand_o_down": u("\uf0a7"), "hand_o_left": u("\uf0a5"), "hand_o_right": u("\uf0a4"), "hand_o_up": u("\uf0a6"), "hdd_o": u("\uf0a0"), "header": u("\uf1dc"), "headphones": u("\uf025"), "heart": u("\uf004"), "heart_o": u("\uf08a"), "history": u("\uf1da"), "home": u("\uf015"), "hospital_o": u("\uf0f8"), "html5": u("\uf13b"), "image": u("\uf03e"), "inbox": u("\uf01c"), "indent": u("\uf03c"), "info": u("\uf129"), "info_circle": u("\uf05a"), "inr": u("\uf156"), "instagram": u("\uf16d"), "institution": u("\uf19c"), "italic": u("\uf033"), "joomla": u("\uf1aa"), "jpy": u("\uf157"), "jsfiddle": u("\uf1cc"), "key": u("\uf084"), "keyboard_o": u("\uf11c"), "krw": u("\uf159"), "language": u("\uf1ab"), "laptop": u("\uf109"), "leaf": u("\uf06c"), "legal": u("\uf0e3"), "lemon_o": u("\uf094"), "level_down": u("\uf149"), "level_up": u("\uf148"), "life_bouy": u("\uf1cd"), "life_ring": u("\uf1cd"), "life_saver": u("\uf1cd"), "lightbulb_o": u("\uf0eb"), "link": u("\uf0c1"), "linkedin": u("\uf0e1"), "linkedin_square": u("\uf08c"), "linux": u("\uf17c"), "list": u("\uf03a"), "list_alt": u("\uf022"), "list_ol": u("\uf0cb"), "list_ul": u("\uf0ca"), "location_arrow": u("\uf124"), "lock": u("\uf023"), "long_arrow_down": u("\uf175"), "long_arrow_left": u("\uf177"), "long_arrow_right": u("\uf178"), "long_arrow_up": u("\uf176"), "magic": u("\uf0d0"), "magnet": u("\uf076"), "mail_forward": u("\uf064"), "mail_reply": u("\uf112"), "mail_reply_all": u("\uf122"), "male": u("\uf183"), "map_marker": u("\uf041"), "maxcdn": u("\uf136"), "medkit": u("\uf0fa"), "meh_o": u("\uf11a"), "microphone": u("\uf130"), "microphone_slash": u("\uf131"), "minus": u("\uf068"), "minus_circle": u("\uf056"), "minus_square": u("\uf146"), "minus_square_o": u("\uf147"), "mobile": u("\uf10b"), "mobile_phone": u("\uf10b"), "money": u("\uf0d6"), "moon_o": u("\uf186"), "mortar_board": u("\uf19d"), "music": u("\uf001"), "navicon": u("\uf0c9"), "openid": u("\uf19b"), "outdent": u("\uf03b"), "pagelines": u("\uf18c"), "paper_plane": u("\uf1d8"), "paper_plane_o": u("\uf1d9"), "paperclip": u("\uf0c6"), "paragraph": u("\uf1dd"), "paste": u("\uf0ea"), "pause": u("\uf04c"), "paw": u("\uf1b0"), "pencil": u("\uf040"), "pencil_square": u("\uf14b"), "pencil_square_o": u("\uf044"), "phone": u("\uf095"), "phone_square": u("\uf098"), "photo": u("\uf03e"), "picture_o": u("\uf03e"), "pied_piper": u("\uf1a7"), "pied_piper_alt": u("\uf1a8"), "pied_piper_square": u("\uf1a7"), "pinterest": u("\uf0d2"), "pinterest_square": u("\uf0d3"), "plane": u("\uf072"), "play": u("\uf04b"), "play_circle": u("\uf144"), "play_circle_o": u("\uf01d"), "plus": u("\uf067"), "plus_circle": u("\uf055"), "plus_square": u("\uf0fe"), "plus_square_o": u("\uf196"), "power_off": u("\uf011"), "print": u("\uf02f"), "puzzle_piece": u("\uf12e"), "qq": u("\uf1d6"), "qrcode": u("\uf029"), "question": u("\uf128"), "question_circle": u("\uf059"), "quote_left": u("\uf10d"), "quote_right": u("\uf10e"), "ra": u("\uf1d0"), "random": u("\uf074"), "rebel": u("\uf1d0"), "recycle": u("\uf1b8"), "reddit": u("\uf1a1"), "reddit_square": u("\uf1a2"), "refresh": u("\uf021"), "renren": u("\uf18b"), "reorder": u("\uf0c9"), "repeat": u("\uf01e"), "reply": u("\uf112"), "reply_all": u("\uf122"), "retweet": u("\uf079"), "rmb": u("\uf157"), "road": u("\uf018"), "rocket": u("\uf135"), "rotate_left": u("\uf0e2"), "rotate_right": u("\uf01e"), "rouble": u("\uf158"), "rss": u("\uf09e"), "rss_square": u("\uf143"), "rub": u("\uf158"), "ruble": u("\uf158"), "rupee": u("\uf156"), "save": u("\uf0c7"), "scissors": u("\uf0c4"), "search": u("\uf002"), "search_minus": u("\uf010"), "search_plus": u("\uf00e"), "send": u("\uf1d8"), "send_o": u("\uf1d9"), "share": u("\uf064"), "share_alt": u("\uf1e0"), "share_alt_square": u("\uf1e1"), "share_square": u("\uf14d"), "share_square_o": u("\uf045"), "shield": u("\uf132"), "shopping_cart": u("\uf07a"), "sign_in": u("\uf090"), "sign_out": u("\uf08b"), "signal": u("\uf012"), "sitemap": u("\uf0e8"), "skype": u("\uf17e"), "slack": u("\uf198"), "sliders": u("\uf1de"), "smile_o": u("\uf118"), "sort": u("\uf0dc"), "sort_alpha_asc": u("\uf15d"), "sort_alpha_desc": u("\uf15e"), "sort_amount_asc": u("\uf160"), "sort_amount_desc": u("\uf161"), "sort_asc": u("\uf0de"), "sort_desc": u("\uf0dd"), "sort_down": u("\uf0dd"), "sort_numeric_asc": u("\uf162"), "sort_numeric_desc": u("\uf163"), "sort_up": u("\uf0de"), "soundcloud": u("\uf1be"), "space_shuttle": u("\uf197"), "spinner": u("\uf110"), "spoon": u("\uf1b1"), "spotify": u("\uf1bc"), "square": u("\uf0c8"), "square_o": u("\uf096"), "stack_exchange": u("\uf18d"), "stack_overflow": u("\uf16c"), "star": u("\uf005"), "star_half": u("\uf089"), "star_half_empty": u("\uf123"), "star_half_full": u("\uf123"), "star_half_o": u("\uf123"), "star_o": u("\uf006"), "steam": u("\uf1b6"), "steam_square": u("\uf1b7"), "step_backward": u("\uf048"), "step_forward": u("\uf051"), "stethoscope": u("\uf0f1"), "stop": u("\uf04d"), "strikethrough": u("\uf0cc"), "stumbleupon": u("\uf1a4"), "stumbleupon_circle": u("\uf1a3"), "subscript": u("\uf12c"), "suitcase": u("\uf0f2"), "sun_o": u("\uf185"), "superscript": u("\uf12b"), "support": u("\uf1cd"), "table": u("\uf0ce"), "tablet": u("\uf10a"), "tachometer": u("\uf0e4"), "tag": u("\uf02b"), "tags": u("\uf02c"), "tasks": u("\uf0ae"), "taxi": u("\uf1ba"), "tencent_weibo": u("\uf1d5"), "terminal": u("\uf120"), "text_height": u("\uf034"), "text_width": u("\uf035"), "th": u("\uf00a"), "th_large": u("\uf009"), "th_list": u("\uf00b"), "thumb_tack": u("\uf08d"), "thumbs_down": u("\uf165"), "thumbs_o_down": u("\uf088"), "thumbs_o_up": u("\uf087"), "thumbs_up": u("\uf164"), "ticket": u("\uf145"), "times": u("\uf00d"), "times_circle": u("\uf057"), "times_circle_o": u("\uf05c"), "tint": u("\uf043"), "toggle_down": u("\uf150"), "toggle_left": u("\uf191"), "toggle_right": u("\uf152"), "toggle_up": u("\uf151"), "trash_o": u("\uf014"), "tree": u("\uf1bb"), "trello": u("\uf181"), "trophy": u("\uf091"), "truck": u("\uf0d1"), "try": u("\uf195"), "tumblr": u("\uf173"), "tumblr_square": u("\uf174"), "turkish_lira": u("\uf195"), "twitter": u("\uf099"), "twitter_square": u("\uf081"), "umbrella": u("\uf0e9"), "underline": u("\uf0cd"), "undo": u("\uf0e2"), "university": u("\uf19c"), "unlink": u("\uf127"), "unlock": u("\uf09c"), "unlock_alt": u("\uf13e"), "unsorted": u("\uf0dc"), "upload": u("\uf093"), "usd": u("\uf155"), "user": u("\uf007"), "user_md": u("\uf0f0"), "users": u("\uf0c0"), "video_camera": u("\uf03d"), "vimeo_square": u("\uf194"), "vine": u("\uf1ca"), "vk": u("\uf189"), "volume_down": u("\uf027"), "volume_off": u("\uf026"), "volume_up": u("\uf028"), "warning": u("\uf071"), "wechat": u("\uf1d7"), "weibo": u("\uf18a"), "weixin": u("\uf1d7"), "wheelchair": u("\uf193"), "windows": u("\uf17a"), "won": u("\uf159"), "wordpress": u("\uf19a"), "wrench": u("\uf0ad"), "xing": u("\uf168"), "xing_square": u("\uf169"), "yahoo": u("\uf19e"), "yen": u("\uf157"), "youtube": u("\uf167"), "youtube_play": u("\uf16a"), "youtube_square": u("\uf166"), } class ListAction(argparse.Action): def __call__(self, parser, namespace, values, option_string=None): for icon in sorted(icons.keys()): print(icon) exit(0) class ListUpdateAction(argparse.Action): def __call__(self, parser, namespace, values, option_string=None): print("icons = {") for icon in sorted(icons.keys()): print(u' "%s": u("\\u%x"),' % (icon, ord(icons[icon]))) print("}") exit(0) def export_icon(icon, size, filename, font, color): image = Image.new("RGBA", (size, size), color=(0,0,0,0)) draw = ImageDraw.Draw(image) # Initialize font font = ImageFont.truetype(font, size) # Determine the dimensions of the icon width,height = draw.textsize(icons[icon], font=font) draw.text(((size - width) / 2, (size - height) / 2), icons[icon], font=font, fill=color) # Get bounding box bbox = image.getbbox() # Create an alpha mask imagemask = Image.new("L", (size, size), 0) drawmask = ImageDraw.Draw(imagemask) # Draw the icon on the mask drawmask.text(((size - width) / 2, (size - height) / 2), icons[icon], font=font, fill=255) # Create a solid color image and apply the mask iconimage = Image.new("RGBA", (size,size), color) iconimage.putalpha(imagemask) if bbox: iconimage = iconimage.crop(bbox) borderw = int((size - (bbox[2] - bbox[0])) / 2) borderh = int((size - (bbox[3] - bbox[1])) / 2) # Create output image outimage = Image.new("RGBA", (size, size), (0,0,0,0)) outimage.paste(iconimage, (borderw,borderh)) # Save file outimage.save(filename) class LoadCSSAction(argparse.Action): def __call__(self, parser, namespace, values, option_string=None): global icons icons = LoadCSSAction._load_css(values) @staticmethod def _load_css(filename): import tinycss new_icons = {} parser = tinycss.make_parser("page3") try: stylesheet = parser.parse_stylesheet_file(filename) except IOError: print >> 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_icons if __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)
然后建立一个bat文件,名字任意,内容如下
font-awesome-to-png.py --color white --size 64 ALL
将fontawesome-webfont.ttf,.py,.bat三个文件放在同一目录下,然后运行.bat文件,就可以生成一堆我们需要的图标。
收藏的用户(0) X
正在加载信息~
推荐阅读
最新回复 (0)
站点信息
- 文章2299
- 用户1336
- 访客10618802
每日一句
You leave, or I go with you.
你留下,或者我跟你走。——《海角七号》
你留下,或者我跟你走。——《海角七号》
新会员