Skip to content

Commit

Permalink
add random crop effect
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanster authored and 褚尉卿 committed Oct 3, 2018
1 parent 0a0d0a0 commit 62c7736
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,18 @@ new config file and use it by `--config_file` option), here are some examples:
|------------|----|
|Origin(Font size 25)|![origin](./imgs/effects/origin.jpg)|
|Perspective Transform|![perspective](./imgs/effects/perspective_transform.jpg)|
|Random Crop|![rand_crop](./imgs/effects/random_crop.jpg)|
|Curve|![curve](./imgs/effects/curve.jpg)|
|Emboss|![emboss](./imgs/effects/emboss.jpg)|
|Light border|![light border](./imgs/effects/light_border.jpg)|
|Dark border|![dark border](./imgs/effects/dark_border.jpg)|
|Random char space big|![random char space big](./imgs/effects/random_space_big.jpg)|
|Random char space small|![random char space small](./imgs/effects/random_space_small.jpg)|
|Reverse color|![reverse color](./imgs/effects/reverse.jpg)|
|Blur|![blur](./imgs/effects/blur.jpg)|
|Middle line|![middle line](./imgs/effects/line_middle.jpg)|
|Table line|![table line](./imgs/effects/line_table.jpg)|
|Under line|![under line](./imgs/effects/line_under.jpg)|
|Emboss|![emboss](./imgs/effects/emboss.jpg)|
|Reverse color|![reverse color](./imgs/effects/reverse.jpg)|
|Blur|![blur](./imgs/effects/blur.jpg)|

3. Run `main.py` file.

Expand Down
16 changes: 14 additions & 2 deletions configs/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ curve:
min: 1 # sin 函数的幅值范围
max: 5

# random crop text height
crop:
enable: false
fraction: 0.5

# top and bottom will applied equally
top:
min: 5
max: 10 # in pixel, this value should small than img_height
bottom:
min: 5
max: 10 # in pixel, this value should small than img_height

# Use image in bg_dir as background for text
img_bg:
Expand All @@ -44,9 +56,9 @@ text_border:
# https://docs.opencv.org/3.4/df/da0/group__photo__clone.html#ga2bf426e4c93a6b1f21705513dfeca49d
# https://www.cs.virginia.edu/~connelly/class/2014/comp_photo/proj2/poisson.pdf
# Use opencv seamlessClone() to draw text on background
# This will make text image looks more natural
# For some background image, this will make text image looks more real
seamless_clone:
enable: true
enable: false
fraction: 0.5

perspective_transform:
Expand Down
Binary file added imgs/effects/random_crop.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 29 additions & 1 deletion textrenderer/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ def gen_img(self, img_index):
# to make sure we can crop full word image after apply perspective
bg = self.gen_bg(width=word_size[0] * 8, height=word_size[1] * 8)
word_img, text_box_pnts, word_color = self.draw_text_on_bg(word, font, bg)

self.dmsg("After draw_text_on_bg")

if apply(self.cfg.crop):
text_box_pnts = self.apply_crop(text_box_pnts, self.cfg.crop)

if apply(self.cfg.line):
word_img, text_box_pnts = self.liner.apply(word_img, text_box_pnts, word_color)
self.dmsg("After draw line")
Expand Down Expand Up @@ -573,3 +575,29 @@ def apply_emboss(self, word_img):

def apply_sharp(self, word_img):
return cv2.filter2D(word_img, -1, self.sharp_kernel)

def apply_crop(self, text_box_pnts, crop_cfg):
"""
Random crop text box height top or bottom, we don't need image information in this step, only change box pnts
:param text_box_pnts: bbox of text [left-top, right-top, right-bottom, left-bottom]
:param crop_cfg:
:return:
croped_text_box_pnts
"""
height = abs(text_box_pnts[0][1] - text_box_pnts[3][1])
scale = float(height) / float(self.out_height)

croped_text_box_pnts = text_box_pnts

if prob(0.5):
top_crop = int(random.randint(crop_cfg.top.min, crop_cfg.top.max) * scale)
self.dmsg("top crop %d" % top_crop)
croped_text_box_pnts[0][1] += top_crop
croped_text_box_pnts[1][1] += top_crop
else:
bottom_crop = int(random.randint(crop_cfg.bottom.min, crop_cfg.bottom.max) * scale)
self.dmsg("bottom crop %d " % bottom_crop)
croped_text_box_pnts[2][1] -= bottom_crop
croped_text_box_pnts[3][1] -= bottom_crop

return croped_text_box_pnts

0 comments on commit 62c7736

Please sign in to comment.