Skip to content

Commit

Permalink
test case
Browse files Browse the repository at this point in the history
  • Loading branch information
AssKickerSurfaceBook committed Oct 14, 2018
1 parent bf56433 commit ac49cfe
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 7 deletions.
2 changes: 1 addition & 1 deletion config/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"p": 0.3,
"opt": {
"bolder": 0.3,
"plain": 0.3
"plain": 0.0
}
},

Expand Down
24 changes: 23 additions & 1 deletion interference.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,28 @@ def make_grid(img, x_num: tuple, y_num: tuple) -> list:
return grids


class Inversion(Interference):

def __init__(self):
"""
Inverse the input image
"""

def interfere(self, img):
"""
:param img: the input image, of which the mode should be `gray-scale`. An auto conversion will be
done otherwise.
:return: A gray-scale image
"""
channel = img.shape[2]
if channel == 3:
out = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
else:
out = img
return 255 - out


class RandomGaussianBlur(Interference):

def __init__(self, min_r, max_r, min_sigma, max_sigma):
Expand Down Expand Up @@ -166,7 +188,7 @@ def interfere(self, img):
# CV_INTER_LINEAR :雙線性插補(預設)
interpolation = cv.INTER_LINEAR
# interpolation:內插方式
img = cv.resize(img, (float(width * scale), float(height * scale)), interpolation=interpolation)
img = cv.resize(img, (int(width * scale), int(height * scale)), interpolation=interpolation)
return img, scale


Expand Down
11 changes: 10 additions & 1 deletion printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def print(self, width, height, text_gen: iter):
font_width, font_height = self.font.getsize(text)

# 获取文字的offset位置,获取字体的大小
offsetx, offsety = self.font.getoffset(text)
# offsetx, offsety = self.font.getoffset(text)

# 实现居中,确定begin_x, begin_y位置,就能实现字居中
begin_x, begin_y = (width - font_width) // 2, (height - font_height) // 2
Expand All @@ -36,3 +36,12 @@ def print(self, width, height, text_gen: iter):
draw.text((begin_x, begin_y), text, font=self.font, fill="black")
im = (np.array(img.getdata()).astype(np.uint8)).reshape(height, width)
yield im, text

def print_one(self, width, height, text):
im = Image.new("1", (width, height), color='black')
draw = ImageDraw.Draw(im)
text_width, text_height = self.font.getsize(text)
begin_x, begin_y = (width - text_width) // 2, (height - text_height) // 2
draw.text((begin_x, begin_y), text, font=self.font, fill="white")
img = (np.array(im.getdata()).astype(np.uint8)).reshape(height, width)
return img
25 changes: 25 additions & 0 deletions test/test_interference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from printer import Printer
import utils.uimg as uimg
import utils.utility as util


def init_img():
printer = Printer('fonts/fangsong_GB2312.ttf', 20)
img = printer.print_one(256, 64, "测试用例")
return img


if __name__ == '__main__':
config = util.read_config('config/template.json')
ops = config['ops']

im = init_img()
uimg.show(im)
operation, p = ops[0]
for idx in range(10):
out, val = operation.interfere(im)
print(out.shape)
uimg.show(out)
# uimg.save("%s/%d.jpg" % ("out", idx), out)


Empty file added utils/__init__.py
Empty file.
15 changes: 12 additions & 3 deletions utils/uimg.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from matplotlib import pyplot as plt


def save_sample(file_path: str, img):
def save(file_path: str, img):
"""
Do not use `cv2.imwrite` to save image, doesn't work as expected when it comes to Chinese file name
:param file_path:
Expand All @@ -13,11 +13,20 @@ def save_sample(file_path: str, img):
cv.imencode('.jpg', img)[1].tofile(file_path)


def read_img(file_path: str):
def read(file_path: str):
return cv.imdecode(np.fromfile(file_path, dtype=np.uint8), 0)


def show_img(img):
def show(img):
plt.imshow(img, cmap='gray', interpolation='bicubic')
plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis
plt.show()


def reverse(img):
"""
:param img: channels = 1, dtype = numpy.uint8
:return: val = 255 - val, pixel-wise
"""
img = 255 - img
return img
2 changes: 1 addition & 1 deletion utils/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def read_config(config_file: str) -> dict:
opt = operation['opt']
cls = None
if name == 'random_stroke':
cls = itf.RandomStroke(opt["bolder"], opt["plain"], 3)
cls = itf.RandomStroke(opt["bolder"], opt["plain"], 2)
elif name == 'random_resize':
cls = itf.RandomResize(opt['min_scale'], opt['max_scale'])
elif name == 'random_rotation':
Expand Down

0 comments on commit ac49cfe

Please sign in to comment.