情人节必备表白代码

效果图

放大后满满的"我爱你"!

holiday.jpg

代码

pip3 install Pillow		#安装依赖包Pillow
from PIL import Image, ImageDraw, ImageFont
import sys #判断参数个数,可以不要
import os  #判断文件是否存在,可以不要
 
image_path = "test.jpg"
font_path = "TEST.TTF"
text = "我爱你"
font_size = 10
 
 
###########这几个判断用来支持命令行参数#######
if len(sys.argv)>1:
    path = sys.argv[1]
    if os.path.exists(path):
        image_path = path
        
if len(sys.argv)>2:
    path = sys.argv[2]
    if os.path.exists(path):
        font_path = path
        
if len(sys.argv)>3:
    text = sys.argv[3]
    
if len(sys.argv)>4:
    if sys.argv[4].isdigit():
        font_size = int(sys.argv[4])
#############################################
 
def generator_new_image(image_path, font_path, text, font_size):
    img_origin = Image.open(image_path)
    img_array = img_origin.load()
    img_new = Image.new("RGB", img_origin.size, (0,0,0))
    draw = ImageDraw.Draw(img_new)
    font = ImageFont.truetype(font_path, font_size)
    
    index = 0
    for y in range(0, img_origin.size[1], font_size):
        for x in range(0,img_origin.size[0], font_size):
            index = index % len(text)
            draw.text((x,y), text[index], font=font, fill=img_array[x,y], direction=None)
            index = index + 1
            
    img_new.convert("RGB").save("love.jpg")
 
generator_new_image(image_path, font_path, text, font_size)

教程

image_path = “test.jpg” #原图片(你暗恋的对象)
font_path = “TEST.TTF” #字体(点击这里可以下载)
text = “我爱你” #表白的文字

发布了99 篇原创文章 · 获赞 44 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/NetRookieX/article/details/104359250