|
| 1 | +#!/usr/bin/env python |
| 2 | +#-*- coding: utf-8 -*- |
| 3 | + |
| 4 | +import string |
| 5 | +import random |
| 6 | +from PIL import Image, ImageDraw, ImageFont, ImageFilter |
| 7 | + |
| 8 | +def create_image(image_size = (300, 100), |
| 9 | + background_color = (255, 255, 255), |
| 10 | + font_type = "arialbd.ttf", |
| 11 | + font_size = 50, |
| 12 | + text_num = 4, |
| 13 | + point_chance = 50): |
| 14 | + |
| 15 | + im = Image.new("RGB", image_size, background_color) |
| 16 | + draw = ImageDraw.Draw(im) |
| 17 | + image_width, image_height = image_size |
| 18 | + |
| 19 | + def create_text(): |
| 20 | + text_font = ImageFont.truetype(font_type, font_size) |
| 21 | + font_width, font_height = text_font.getsize("A") |
| 22 | + for i in range(text_num): |
| 23 | + text = random.choice(string.ascii_uppercase) |
| 24 | + text_loc = ((image_width - font_width) / text_num * (i + 0.5), (image_height - font_height) / 2.3) |
| 25 | + draw.text(text_loc, text, font = text_font, fill = (random.randint(0, 255) / 2, random.randint(0, 255) / 2, random.randint(0, 255) / 2)) |
| 26 | + |
| 27 | + def create_points(): |
| 28 | + for w in range(image_width): |
| 29 | + for h in range(image_height): |
| 30 | + tmp = random.randint(0, 100) |
| 31 | + if tmp > point_chance: |
| 32 | + draw.point((w, h), fill = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))) |
| 33 | + |
| 34 | + create_text() |
| 35 | + create_points() |
| 36 | + im = im.filter(ImageFilter.BLUR) |
| 37 | + |
| 38 | + return im |
| 39 | + |
| 40 | +if __name__ == "__main__": |
| 41 | + im = create_image() |
| 42 | + im.show() |
0 commit comments