那时的我,对于未来有很多遐想:写小说、写时评、写诗歌... 总而言之,就是成为一个文字工作者
现在我确实成为了一个文字工作者,只不过写的是代码...
干巴巴的随笔看起来没什么意思,需要有一张配图,对当天的阅读、写作进行概括性总结。这张图是统一的模板,只是内容不同,如果每张图都要用ps来处理,太繁琐了。作为一个以懒惰为美德的程序员,肯定是想着用程序自动生成图片。
# -*- coding:utf-8 -*-from PIL import Image, ImageDraw, ImageFontimport time# 安装库:pip install Pillowheader = '001'title = '日思录第001篇'books = ['中国史纲五十讲', '再见拖延症', '心流']writes = ['日思录第001篇', 'python给图片加文字']summary = '习惯在一个任务开始之前,先给自己设立一个看起来不太可能达到的完美标准,并因为这个标准而迟迟无法动手,那你可能也是一个完美主义者'n = 18summary_list = [summary[i:i + n] for i in range(0, len(summary), n)]# 图片名称img = './test.png' # 图片模板new_img = 'text.png' # 生成的图片compress_img = 'compress.png' # 压缩后的图片# 设置字体样式font_type = '/System/Library/Fonts/STHeiti Light.ttc'font_medium_type = '/System/Library/Fonts/STHeiti Medium.ttc'header_font = ImageFont.truetype(font_medium_type, 55)title_font = ImageFont.truetype(font_medium_type, 45)font = ImageFont.truetype(font_type, 24)color = "#000000"# 打开图片image = Image.open(img)draw = ImageDraw.Draw(image)width, height = image.size# header头header_x = 130header_y = 690draw.text((header_x, height - header_y), u'%s' % header, color, header_font)# 标题title_x = header_xtitle_y = header_y - 80draw.text((title_x, height - title_y), u'%s' % title, color, title_font)# 当前时间cur_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())cur_time_x = 590cur_time_y = title_y - 25cur_time_font = ImageFont.truetype(font_type, 25)draw.text((cur_time_x, height - cur_time_y), u'%s' % cur_time, color, cur_time_font)# 阅读book_x = title_x + 5book_start_y = title_y - 190book_y = 0book_line = 50for num, book in enumerate(books): y = book_start_y - num * book_line book_num = num + 1 draw.text((book_x, height - y), u'%s. %s' % (book_num, book), color, font)# 写作write_x = book_xwrite_y = title_y - 450write_line = 40for num, write in enumerate(writes): write_num = num + 1 y = write_y - num * write_line draw.text((write_x, height - y), u'%s. %s' % (write_num, write), color, font)# 哲思summary_x = book_x + 460summary_y = book_start_ysummary_line = 35for num, summary in enumerate(summary_list): y = summary_y - num * summary_line draw.text((summary_x, height - y), u'%s' % summary, color, font)# 生成图片image.save(new_img, 'png')# 压缩图片sImg = Image.open(new_img)w, h = sImg.sizewidth = int(w / 2)height = int(h / 2)dImg = sImg.resize((width, height), Image.ANTIALIAS)dImg.save(compress_img)