From 49da060e3ac5e54758f28b78779c2016d98d4044 Mon Sep 17 00:00:00 2001 From: Cedric Porter Date: Wed, 18 Jul 2012 17:02:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E5=AD=97=E7=AC=A6=E5=88=86=E5=89=B2?= =?UTF-8?q?=E7=9A=84=E4=BB=A3=E7=A0=81=E5=90=88=E5=B9=B6=E8=BF=87=E6=9D=A5?= =?UTF-8?q?=E4=BA=86=EF=BC=8C=E6=94=BE=E5=9C=A8Tools=E4=B8=8B=E9=9D=A2?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + EffectLab/Effect.py | 6 ++-- TestEffectLab.py | 37 +++++++++++++++++----- Tools/.gitignore | 1 + Tools/GeneratorCharacters.py | 61 ++++++++++++++++++++++++++++++++++++ 5 files changed, 95 insertions(+), 11 deletions(-) create mode 100644 Tools/.gitignore create mode 100755 Tools/GeneratorCharacters.py diff --git a/.gitignore b/.gitignore index 92fe640..2f75dc3 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ *~ .ropeproject/ *.jpg +Images/ diff --git a/EffectLab/Effect.py b/EffectLab/Effect.py index e107287..b73ac73 100755 --- a/EffectLab/Effect.py +++ b/EffectLab/Effect.py @@ -71,7 +71,7 @@ def filter(self, img): found += 1 if found > 0: - psum = map(operator.div, psum, (antialias * antialias, ) * len(psum)) + psum = map(operator.div, psum, (found, ) * len(psum)) new_img.putpixel((x, y), tuple(psum)) return new_img @@ -140,7 +140,7 @@ def filter(self, img): found += 1 if found > 0: - psum = map(operator.div, psum, (antialias * antialias, ) * len(psum)) + psum = map(operator.div, psum, (found, ) * len(psum)) new_img.putpixel((x, y), tuple(psum)) return new_img @@ -191,7 +191,7 @@ def filter(self, img): found += 1 if found > 0: - psum = map(operator.div, psum, (self.antialias * self.antialias, ) * len(psum)) + psum = map(operator.div, psum, (found, ) * len(psum)) new_img.putpixel((i, j), tuple(psum)) return new_img diff --git a/TestEffectLab.py b/TestEffectLab.py index de1486b..4fe2c6b 100755 --- a/TestEffectLab.py +++ b/TestEffectLab.py @@ -6,9 +6,12 @@ import os, time, string, random import autoreload +import ImageChops from math import sqrt, sin, cos, tan, atan2 from EffectLab.Effect import * +Effect.empty_color = (255, 255, 255, 255) + def merge_origin_and_new(img, effect): '''Merge origin and new Image processed by function effect in one Image ''' @@ -27,17 +30,21 @@ def merge_origin_and_new(img, effect): return out +class Character(object): + def __init__(self, img): + self.width, self.height = img.size + self.img = img def main(): print 'Started' effects = [ RadianFormulaEffect(lambda r, phi: (r ** 2, phi), 4), - LensWarpEffect(lambda x, y: (sin(x * math.pi / 2), sin(y * math.pi / 2))), - RadianFormulaEffect(lambda r, phi: (r ** 1.5 * math.cos(r), phi)), - GlobalWaveEffect(), RadianSqrtEffect(), + GlobalWaveEffect(), LensWarpEffect(lambda x, y: (sign(x) * x ** 2, sign(y) * y ** 2)), + LensWarpEffect(lambda x, y: (sin(x * math.pi / 2), sin(y * math.pi / 2))), + RadianFormulaEffect(lambda r, phi: (r ** 1.5 * math.cos(r), phi)), LocalWarpEffect((130, 120), (130, 50), 100), ] @@ -46,15 +53,29 @@ def main(): # else: # img = Image.new("RGBA", (300, 300), (255, 255, 255, 255)) + characters = string.letters + string.digits + char_img = {} + for ch in characters: + img = Image.open('Images/%s.png' % ch) + char_img[ch] = Character(img) + text = ''.join(random.choice(string.letters) for i in xrange(4)) - img = Image.new("RGB", (100, 40), (255, 255, 255)) + img = Image.new("RGBA", (100, 40), (255, 255, 255, 255)) font = ImageFont.truetype("UbuntuMono-R.ttf", 33) - draw = ImageDraw.Draw(img) - draw.setfont(font) + # draw = ImageDraw.Draw(img) + # draw.setfont(font) + # draw.text((10, 0), text, (0, 0, 0)) - draw.text((10, 0), text, (0, 0, 0)) - del draw + last = random.choice(characters) + offset = 15 + for i in range(5): + ch = random.choice(characters) + img.paste(char_img[ch].img, + (offset, 0), + ImageChops.invert(char_img[ch].img.split()[1])) + last = ch + offset += char_img[last].width - 1 for index, effect in enumerate(effects): merge_origin_and_new(img, effect).save('%d.jpg' % index, quality=90) diff --git a/Tools/.gitignore b/Tools/.gitignore new file mode 100644 index 0000000..aab52d9 --- /dev/null +++ b/Tools/.gitignore @@ -0,0 +1 @@ +*.png \ No newline at end of file diff --git a/Tools/GeneratorCharacters.py b/Tools/GeneratorCharacters.py new file mode 100755 index 0000000..1d0712f --- /dev/null +++ b/Tools/GeneratorCharacters.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python +# author: Hua Liang [ Stupid ET ] +# email: et@everet.org +# website: http://EverET.org +# +# This is a tools that generate each image of character + +import random, string, md5, time +import Image, ImageDraw, ImageFont +import StringIO, string + +def get_vertical_map(img): + img = img.convert('L') + # segmentation + m = [0] * width + for x in range(width): + for y in range(height): + if img.getpixel((x, y)) != 255: + m[x] += 1 + return m + +def get_character_region(iterator): + '''get character region + ''' + state = "empty" + + i, lbd, rbd = 0, -1, -1 + while True: + data = iterator.next() + if state == "empty": + if data != 0: + # enter + state = "collect" + lbd = i + elif state == "collect": + if data == 0: + # leave + rbd = i + state = "empty" + yield (lbd, rbd) + i += 1 + +width, height = 1200, 40 + +characters = string.letters + string.digits + +# draw img +img = Image.new("RGBA", (width, height), (255, 255, 255, 0)) +font = ImageFont.truetype("../UbuntuMono-R.ttf", 33) +draw = ImageDraw.Draw(img) +draw.setfont(font) +draw.text((10, 0), characters, (0, 0, 0, 255)) +del draw + +m = get_vertical_map(img) + +for index, bd in enumerate(get_character_region(iter(m))): + ch = characters[index] + + img.crop((bd[0], 0, bd[1], height)).save('../Images/%s.png' % ch) +