コード例 #1
0
ファイル: models.py プロジェクト: mygirl8893/gitcoinco-web
    def create_from_config(self, svg_name='avatar'):
        """Create an avatar SVG from the configuration.

        TODO:
            * Deprecate in favor of request param based view using templates.

        """
        payload = self.config
        icon_width = self.ICON_SIZE[0]
        icon_height = self.ICON_SIZE[1]

        components = [
            icon_width, icon_height,
            Line([(0, icon_height / 2), (icon_width, icon_height / 2)],
                 width=f'{icon_height}px',
                 color=f"#{payload.get('Background')}")
        ]

        for k, v in payload.items():
            if k not in [
                    'Background', 'ClothingColor', 'HairColor', 'SkinTone'
            ]:
                components.append(
                    build_avatar_component(
                        f"{v.get('component_type')}/{v.get('svg_asset')}",
                        self.ICON_SIZE))

        with NamedTemporaryFile(mode='w+', suffix='.svg') as tmp:
            avatar = Figure(*components)
            avatar.save(tmp.name)
            with open(tmp.name) as file:
                if self.profile_set.exists():
                    svg_name = self.profile_set.last().handle
                self.svg.save(f"{svg_name}.svg", File(file), save=True)
コード例 #2
0
ファイル: utils.py プロジェクト: svipal/web
def build_avatar_svg(svg_path='avatar.svg',
                     line_color='#781623',
                     icon_size=None,
                     payload=None,
                     temp=False):
    from .models import BaseAvatar
    icon_size = icon_size or BaseAvatar.ICON_SIZE
    icon_width = icon_size[0]
    icon_height = icon_size[1]

    if payload is None:
        # Sample payload
        payload = {
            'background_color': line_color,
            'icon_size': BaseAvatar.ICON_SIZE,
            'avatar_size': None,
            'skin_tone': '#3F2918',
            'ears': {
                'item_type': '0',
            },
            'clothing': {
                'primary_color': '#18C708',
                'item_type': 'cardigan',
            },
            'head': {
                'item_type': '0',
            },
            'hair': {
                'primary_color': '#29F998',
                'item_type': '0',
            },
            'mouth': '0',
            'nose': '0',
            'eyes': '0',
            'wallpaper': None
        }

    # Build the list of avatar components
    components = [
        icon_width,
        icon_height,
        Line([(0, icon_height / 2), (icon_width, icon_height / 2)],
             width=f'{icon_height}px',
             color=payload.get('background_color')),
    ]

    customizable_components = ['clothing', 'ears', 'head', 'hair']
    flat_components = ['eyes', 'mouth', 'nose', 'wallpaper']
    multi_components = ['accessories']

    for component in customizable_components:
        if component in payload:
            primary_color = payload.get(
                component, {}).get('primary_color') or payload.get('skin_tone')
            components.append(
                build_temporary_avatar_component(
                    component_category=component,
                    component_type=payload.get(component,
                                               {}).get('item_type',
                                                       'cardigan'),
                    primary_color=primary_color,
                ))

    for component in flat_components:
        if component in payload:
            components.append(
                build_avatar_component(
                    f"{component.title()}/{payload.get(component, '0')}.svg",
                    icon_size))

    for component in multi_components:
        if component in payload:
            components.append(
                build_avatar_component(
                    f"{component.title()}/{payload.get(component)}"))

    final_avatar = Figure(*components)

    if temp:
        return final_avatar
    result_path = f'{COMPONENT_BASE}{svg_path}'
    final_avatar.save(result_path)
    return result_path