Skip to content

Commit

Permalink
将字符分割的代码合并过来了,放在Tools下面。
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricporter committed Jul 18, 2012
1 parent 6d28fa6 commit 49da060
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
*~
.ropeproject/
*.jpg
Images/
6 changes: 3 additions & 3 deletions EffectLab/Effect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
37 changes: 29 additions & 8 deletions TestEffectLab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
'''
Expand All @@ -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),
]

Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions Tools/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.png
61 changes: 61 additions & 0 deletions Tools/GeneratorCharacters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env python
# author: Hua Liang [ Stupid ET ]
# email: [email protected]
# 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)

0 comments on commit 49da060

Please sign in to comment.