Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.un~
data/input/*
data/output/*
__pycache__
28 changes: 21 additions & 7 deletions img2img_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import cv2
import numpy as np
from PIL import Image, ImageDraw, ImageOps
from utils import get_data
from utils import get_data, get_dominant_color


def get_args():
Expand All @@ -15,7 +15,7 @@ def get_args():
parser.add_argument("--output", type=str, default="data/output.jpg", help="Path to output text file")
parser.add_argument("--language", type=str, default="english")
parser.add_argument("--mode", type=str, default="standard")
parser.add_argument("--background", type=str, default="black", choices=["black", "white"],
parser.add_argument("--background", type=str, default="auto", choices=["auto", "black", "white"],
help="background's color")
parser.add_argument("--num_cols", type=int, default=300, help="number of character for output's width")
parser.add_argument("--scale", type=int, default=2, help="upsize output")
Expand All @@ -24,15 +24,27 @@ def get_args():


def main(opt):
if opt.background == "white":
bg_code = (255, 255, 255)
else:
bg_code = (0, 0, 0)
char_list, font, sample_character, scale = get_data(opt.language, opt.mode)
num_chars = len(char_list)
num_cols = opt.num_cols
image = cv2.imread(opt.input, cv2.IMREAD_COLOR)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

if opt.background == "auto":
dominant = get_dominant_color(image)
diff_white = 255 - dominant[0] + 255 - dominant[1] + 255 - dominant[2]
if(diff_white <= 381):
print("WHITE Background")
opt.background = "white"
else:
print("BLACK Blackground")
opt.background = "black"

if opt.background == "white":
bg_code = (255, 255, 255)
else:
bg_code = (0, 0, 0)

height, width, _ = image.shape
cell_width = width / opt.num_cols
cell_height = scale * cell_width
Expand All @@ -43,7 +55,8 @@ def main(opt):
cell_height = 12
num_cols = int(width / cell_width)
num_rows = int(height / cell_height)
char_width, char_height = font.getsize(sample_character)
font_bbox = font.getbbox(sample_character)
char_width, char_height = font_bbox[2] - font_bbox[0], font_bbox[3]
out_width = char_width * num_cols
out_height = scale * char_height * num_rows
out_image = Image.new("RGB", (out_width, out_height), bg_code)
Expand All @@ -63,6 +76,7 @@ def main(opt):
cropped_image = out_image.getbbox()
out_image = out_image.crop(cropped_image)
out_image.save(opt.output)
print("Finished %s" % opt.input)


if __name__ == '__main__':
Expand Down
23 changes: 23 additions & 0 deletions img2img_color_all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

# Run img2img_color.py in a loop over INPUT directory

INPUT_PATH="data/input"
OUTPUT_PATH="data/output"

INPUT=`ls $INPUT_PATH`
OUTPUT=`ls $OUTPUT_PATH`

for filename in $INPUT; do
# echo "$filename"

if [[ $OUTPUT != *"$filename"* ]]; then
echo "Converting $filename"
python3 img2img_color.py --input=$INPUT_PATH/$filename --output=$OUTPUT_PATH/$filename
fi
done



#ls data/input | awk '{ system("python3 img2img_color.py --input=data/input/" $1 " --output=data/output/" $1) }'
#ls data/input | grep "XX" | awk '{ system("echo Processing " $1 "; python3 img2img_color.py --input=data/input/" $1 " --output=data/output/" $1 " --background=white") }'
12 changes: 12 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import cv2
from PIL import Image, ImageFont, ImageDraw, ImageOps


Expand Down Expand Up @@ -117,3 +118,14 @@ def get_data(language, mode):
char_list = sort_chars(char_list, font, language)

return char_list, font, sample_character, scale

def get_dominant_color(img):
data = np.reshape(img, (-1,3))
data = np.float32(data)

criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
flags = cv2.KMEANS_RANDOM_CENTERS
compactness,labels,centers = cv2.kmeans(data,1,None,criteria,10,flags)

# print('Dominant color is: bgr({})'.format(centers[0].astype(np.int32)))
return tuple(centers[0].astype(np.int32))