python Meme Generator 2

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Meme Generator 2相关的知识,希望对你有一定的参考价值。

import Image
import ImageChops
import ImageDraw
import ImageFont
import ImageFilter
import clipboard

def draw_caption(img, text, outline=2, top=False):
	text_img = Image.new('RGBA', img.size, (0, 0, 0, 0))
	draw = ImageDraw.Draw(text_img)
	w = img.size[0]
	s = 100
	while w >= (img.size[0] - 20):
		font = ImageFont.truetype('HelveticaNeue-CondensedBlack', s)
		w, h = draw.textsize(text, font=font)
		s -= 1
		if s <= 12: break
	text_y = 0 if top else img.size[1] - h
	draw.text((10, text_y), text, font=font, fill='black')
	kernel = [0, 1, 2, 1, 0,
	          1, 2, 4, 2, 1,
	          2, 4, 0, 4, 1,
	          1, 2, 4, 2, 1,
	          0, 1, 2, 1, 0]
	myfilter = ImageFilter.Kernel((5, 5), kernel, scale = 0.25 * sum(kernel))
	for i in xrange(outline):
		print 'Processing image... ' + str(int(float(i)/outline * 100)) + '%'
		text_img = text_img.filter(myfilter)
	print 'Processing done.'
	draw = ImageDraw.Draw(text_img)
	draw.text((10, text_y), text, font = font, fill = 'white')
	mask_img = ImageChops.invert(text_img)
	result_img = Image.composite(img, text_img, mask_img)
	return result_img

if __name__ == '__main__':
	print 'Loading image from clipboard...'
	img = clipboard.get_image()
	if img is None:
		print 'No image in clipboard, using default image instead...'
		img = Image.open('Test_Mandrill')
	img.show()
	print 'Enter the top caption (press return for none):'
	caption_top = unicode(raw_input(), 'utf-8')
	caption_top = caption_top.upper()
	if caption_top != '':
		img = draw_caption(img, caption_top, outline=3, top=True)
	print 'Enter the bottom caption (press return for none):'
	caption_btm = unicode(raw_input(), 'utf-8')
	caption_btm = caption_btm.upper()
	if caption_btm != '':
		img = draw_caption(img, caption_btm, outline=3, top=False)
	img.show()
	# If you want to copy the result to the clipboard automatically,
	# uncomment the following line:
	#clipboard.set_image(img.convert('RGBA'))
	# You can also copy an image from the console output or save it
	# to your camera roll by touching and holding it.

以上是关于python Meme Generator 2的主要内容,如果未能解决你的问题,请参考以下文章

你如何在python 3.x中使用或运算?

Meme 命令 discord.py

无法转换类型“Meme!”的值到预期的参数类型'@noescape (Meme) throws -> Bool'

meme suite —— Motif分析百宝箱(二)

CF1076C Meme Problem

Educational Codeforces Round 80 (Rated for Div. 2)(BYet Another Meme Problem)