Python自制PornHub风格的LOGO

栏目: IT技术 · 发布时间: 3年前

内容简介:PornHub又名P站,网站的流量在全球也是TOP10级别,和淘宝、百度等站点同一个level,他们家网站的LOGO也显得个性鲜明。如何用Python画一个呢?

PornHub又名P站,网站的流量在全球也是TOP10级别,和淘宝、百度等站点同一个level,他们家网站的LOGO也显得个性鲜明。

Python自制PornHub风格的LOGO

如何用 Python 画一个呢?

画图需要用到一个库叫Pillow,它是专门用来出来图像的,功能非常强大,修图功能能做的事,都可以用Pillow来实现。

分析下这张图的结构

它有左右两部分组成,左边是黑底白字,右边是黄底黑字。这两部分包裹在一个黑色矩形框中。

思路清楚了后,就可以动手实现

先实现左图

def left_img(text):
    # 指定字体和字体大小
    font = ImageFont.truetype('ArialEnUnicodeBold.ttf', FONT_SIZE)
    font_width, font_height = font.getsize(text)
    offset_y = font.font.getsize(text)[1][1]
    blank_height = font_height * 2
    right_blank = int(font_width / len(text) * 0.25)
    img_height = font_height + offset_y + blank_height * 2
    image_width = font_width + right_blank
    # 设置图片宽高
    image_size = image_width, img_height
    image = Image.new('RGBA', image_size, BG_COLOR)
    draw = ImageDraw.Draw(image)
    # 画图
    draw.text((0, blank_height), text, fill=LEFT_TEXT_COLOR, font=font)
    image.save("left.png")
    return image

运行后生成的图片是这样的

Python自制PornHub风格的LOGO

右图也是类似的操作方式,只需要修改背景色和文字颜色

def create_right_img(text: str, font_size: int):
    radii = RIGHT_PART_RADII
    font = ImageFont.truetype('ArialEnUnicodeBold.ttf', font_size)
    font_width, font_height = font.getsize(text)
    offset_y = font.font.getsize(text)[1][1]
    blank_height = font_height
    left_blank = int(font_width / len(text) * 0.25)
    image_width = font_width + 2 * left_blank
    image_height = font_height + offset_y + blank_height * 2
    image = Image.new('RGBA', (image_width, image_height), BOX_COLOR)
    draw = ImageDraw.Draw(image)
    draw.text((left_blank, blank_height), text, fill=RIGHT_TEXT_COLOR, font=font)

    # 圆
    magnify_time = 10
    magnified_radii = radii * magnify_time
    circle = Image.new('L', (magnified_radii * 2, magnified_radii * 2), 0)  # 创建一个黑色背景的画布
    draw = ImageDraw.Draw(circle)
    draw.ellipse((0, 0, magnified_radii * 2, magnified_radii * 2), fill=255)  # 画白色圆形

    # 画4个角(将整圆分离为4个部分)
    magnified_alpha_width = image_width * magnify_time
    magnified_alpha_height = image_height * magnify_time
    alpha = Image.new('L', (magnified_alpha_width, magnified_alpha_height), 255)
    alpha.paste(circle.crop((0, 0, magnified_radii, magnified_radii)), (0, 0))  # 左上角
    alpha.paste(circle.crop((magnified_radii, 0, magnified_radii * 2, magnified_radii)),
                (magnified_alpha_width - magnified_radii, 0))  # 右上角
    alpha.paste(circle.crop((magnified_radii, magnified_radii, magnified_radii * 2, magnified_radii * 2)),
                (magnified_alpha_width - magnified_radii, magnified_alpha_height - magnified_radii))  # 右下角
    alpha.paste(circle.crop((0, magnified_radii, magnified_radii, magnified_radii * 2)),
                (0, magnified_alpha_height - magnified_radii))  # 左下角
    alpha = alpha.resize((image_width, image_height), Image.ANTIALIAS)
    image.putalpha(alpha)
    image.save("right.png")
    return image

效果图

Python自制PornHub风格的LOGO

其实这个图是有4个圆角的。

最后一步就是将两张图合并到一张黑底背景的矩形框就好了。

def create(left_text, right_text):
    left_img = create_left_img(left_text)
    right_img = create_right_img(right_text, FONT_SIZE)
    blank = 30
    bg_img_width = left_img.width + right_img.width + blank * 2
    bg_img_height = left_img.height
    bg_img = Image.new('RGBA', (bg_img_width, bg_img_height), BG_COLOR)
    bg_img.paste(left_img, (blank, 0))
    bg_img.paste(right_img, (blank + left_img.width, int((bg_img_height - right_img.height) / 2)), mask=right_img)
    bg_img.save(f'{left_text}{right_text}.png')

最终的效果图:

Python自制PornHub风格的LOGO

想生成其它文字风格只需要换个词就可以,如果GitHub套用P站风格就是这样的

Python自制PornHub风格的LOGO

有问题可以扫描二维码和我交流

关注公众号「Python之禅」,回复「1024」免费获取Python资源

Python自制PornHub风格的LOGO

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Linux Programming Interface

The Linux Programming Interface

Michael Kerrisk / No Starch Press / 2010-11-6 / GBP 79.99

The Linux Programming Interface describes the Linux API (application programming interface)-the system calls, library functions, and other low-level interfaces that are used, directly or indirectly, b......一起来看看 《The Linux Programming Interface》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具