コード例 #1
0
def generate_hashes(apps, schema_editor):
    if settings.DEBUG:
        return False
    base_avatar_model = apps.get_model('avatar', 'BaseAvatar')
    for base_avatar in base_avatar_model.objects.all():
        try:
            if not base_avatar.png:
                output_fmt = 'png'
                tmpfile_io = convert_img(base_avatar.svg, input_fmt='svg', output_fmt='png')
                if base_avatar.profile:
                    png_name = base_avatar.profile.handle
                if tmpfile_io:
                    converted_avatar = ContentFile(tmpfile_io.getvalue())
                    converted_avatar.name = f'{png_name}.{output_fmt}'
                    base_avatar.png = converted_avatar
                    base_avatar.save()
            base_avatar.hash = dhash(Image.open(base_avatar.png))
            base_avatar.save()
        except Exception as e:
            logger.error('Could not generate hash for avatar pk (%s), error (%s)', base_avatar.pk, e)
コード例 #2
0
def generate_collection_thumbnail(collection, width, heigth):
    MARGIN = int(width / 30)
    MID_MARGIN = int(width / 90)
    BG = (13, 2, 59)
    DISPLAY_GRANTS_LIMIT = 4
    PROFILE_WIDTH = PROFILE_HEIGHT = int(width / 3.5)
    GRANT_WIDTH = int(width / 2) - MARGIN - MID_MARGIN
    GRANT_HEIGHT = int(heigth / 2) - MARGIN - MID_MARGIN
    IMAGE_BOX = (width, heigth)
    LOGO_SIZE_DIFF = int(GRANT_WIDTH / 5)
    HALF_LOGO_SIZE_DIFF = int(LOGO_SIZE_DIFF / 2)
    PROFILE_BOX = (PROFILE_WIDTH - LOGO_SIZE_DIFF,
                   PROFILE_HEIGHT - LOGO_SIZE_DIFF)
    GRANT_BOX = (GRANT_WIDTH, GRANT_HEIGHT)
    media_url = '' if 'media' not in MEDIA_URL else BASE_URL[:-1]

    grants = collection.grants.all()

    logos = []
    for grant in grants:
        if grant.logo:
            if len(logos) > DISPLAY_GRANTS_LIMIT:
                break
            grant_url = f'{media_url}{grant.logo.url}'
            print(f'Trying to get: ${grant_url}')
            fd = urllib.request.urlopen(grant_url)
            logos.append(fd)
        else:
            static_file = f'assets/v2/images/grants/logos/{grant.id % 3}.png'
            logos.append(static_file)

    for logo in range(len(logos), 4):
        logos.append(None)

    thumbail = Image.new('RGBA', IMAGE_BOX, color=BG)
    avatar_url = f'{media_url}{collection.profile.avatar_url}'
    fd = urllib.request.urlopen(avatar_url)

    # Make rounder profile avatar img
    mask = Image.new('L', PROFILE_BOX, 0)
    draw = ImageDraw.Draw(mask)
    draw.ellipse((0, 0) + PROFILE_BOX, fill=255)
    profile_thumbnail = Image.open(fd)

    profile_thumbnail.thumbnail(PROFILE_BOX, Image.ANTIALIAS)
    profile_circle = ImageOps.fit(profile_thumbnail,
                                  mask.size,
                                  centering=(0.5, 0.5))

    try:
        applied_mask = profile_circle.copy()
        applied_mask.putalpha(mask)
        profile_circle.paste(applied_mask, (0, 0), profile_circle)
    except ValueError:
        profile_circle.putalpha(mask)

    CORNERS = [
        [MARGIN, MARGIN],  # Top left grant
        [width - GRANT_WIDTH - MARGIN, MARGIN],  # Top right grant
        [MARGIN, heigth - GRANT_HEIGHT - MARGIN],  # bottom left grant
        [width - GRANT_WIDTH - MARGIN,
         heigth - GRANT_HEIGHT - MARGIN]  # bottom right grant
    ]

    for index in range(4):
        if logos[index] is None:
            grant_bg = Image.new('RGBA', GRANT_BOX, color='white')
            thumbail.paste(grant_bg, CORNERS[index], grant_bg)
            continue

        if type(logos[index]) is not str and re.match(r'.*\.svg',
                                                      logos[index].url):
            grant_img = convert_img(logos[index])
            grant_thumbail = Image.open(grant_img)
        else:
            try:
                grant_thumbail = Image.open(logos[index])
            except ValueError:
                grant_thumbail = Image.open(logos[index]).convert("RGBA")

        grant_thumbail.thumbnail(GRANT_BOX, Image.ANTIALIAS)

        grant_bg = Image.new('RGBA', GRANT_BOX, color='white')

        try:
            grant_bg.paste(
                grant_thumbail,
                (int(GRANT_WIDTH / 2 - grant_thumbail.size[0] / 2),
                 int(GRANT_HEIGHT / 2 - grant_thumbail.size[1] / 2)),
                grant_thumbail)
        except ValueError:
            grant_bg.paste(
                grant_thumbail,
                (int(GRANT_WIDTH / 2 - grant_thumbail.size[0] / 2),
                 int(GRANT_HEIGHT / 2 - grant_thumbail.size[1] / 2)))

        thumbail.paste(grant_bg, CORNERS[index], grant_bg)

    draw_on_thumbnail = ImageDraw.Draw(thumbail)
    draw_on_thumbnail.ellipse([(int(width / 2 - PROFILE_WIDTH / 2),
                                int(heigth / 2 - PROFILE_HEIGHT / 2)),
                               (int(width / 2 + PROFILE_WIDTH / 2),
                                int(heigth / 2 + PROFILE_HEIGHT / 2))],
                              fill="#0D013B")

    try:
        thumbail.paste(
            profile_circle,
            (int(width / 2 - PROFILE_WIDTH / 2) + HALF_LOGO_SIZE_DIFF,
             int(heigth / 2 - PROFILE_HEIGHT / 2) + HALF_LOGO_SIZE_DIFF),
            profile_circle)
    except ValueError:
        thumbail.paste(
            profile_circle,
            (int(width / 2 - PROFILE_WIDTH / 2) + HALF_LOGO_SIZE_DIFF,
             int(heigth / 2 - PROFILE_HEIGHT / 2) + HALF_LOGO_SIZE_DIFF))

    return thumbail