入力として合理的なロスレス形式の白黒画像が与えられた場合、可能な限り入力画像に近いASCIIアートを出力します。
ルール
- 改行とASCIIバイト32-127のみを使用できます。
- 入力画像はトリミングされ、画像の周囲に余分な空白がないようにします。
- 提出は、5分以内にスコアリングコーパス全体を完了することができる必要があります。
- 生のテキストのみが許容されます。リッチテキスト形式はありません。
- スコアリングで使用されるフォントは20ポイントのLinux Libertineです。
- 出力テキストファイルは、以下で説明するように画像に変換される場合、入力画像と同じサイズである必要があり、いずれかのサイズで30ピクセル以内に収まります。
得点
これらの画像はスコアリングに使用されます:
ここから画像のzipファイルをダウンロードできます。
このコーパス用に提出を最適化するべきではありません。むしろ、同様の寸法の任意の8つの白黒画像に対して機能するはずです。投稿がこれらの特定の画像に最適化されていると思われる場合、コーパス内の画像を変更する権利を留保します。
スコアリングは次のスクリプトを介して実行されます。
#!/usr/bin/env python
from __future__ import print_function
from __future__ import division
# modified from http://stackoverflow.com/a/29775654/2508324
# requires Linux Libertine fonts - get them at https://sourceforge.net/projects/linuxlibertine/files/linuxlibertine/5.3.0/
# requires dssim - get it at https://github.com/pornel/dssim
import PIL
import PIL.Image
import PIL.ImageFont
import PIL.ImageOps
import PIL.ImageDraw
import pathlib
import os
import subprocess
import sys
PIXEL_ON = 0 # PIL color to use for "on"
PIXEL_OFF = 255 # PIL color to use for "off"
def dssim_score(src_path, image_path):
out = subprocess.check_output(['dssim', src_path, image_path])
return float(out.split()[0])
def text_image(text_path):
"""Convert text file to a grayscale image with black characters on a white background.
arguments:
text_path - the content of this file will be converted to an image
"""
grayscale = 'L'
# parse the file into lines
with open(str(text_path)) as text_file: # can throw FileNotFoundError
lines = tuple(l.rstrip() for l in text_file.readlines())
# choose a font (you can see more detail in my library on github)
large_font = 20 # get better resolution with larger size
if os.name == 'posix':
font_path = '/usr/share/fonts/linux-libertine/LinLibertineO.otf'
else:
font_path = 'LinLibertine_DRah.ttf'
try:
font = PIL.ImageFont.truetype(font_path, size=large_font)
except IOError:
print('Could not use Libertine font, exiting...')
exit()
# make the background image based on the combination of font and lines
pt2px = lambda pt: int(round(pt * 96.0 / 72)) # convert points to pixels
max_width_line = max(lines, key=lambda s: font.getsize(s)[0])
# max height is adjusted down because it's too large visually for spacing
test_string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
max_height = pt2px(font.getsize(test_string)[1])
max_width = pt2px(font.getsize(max_width_line)[0])
height = max_height * len(lines) # perfect or a little oversized
width = int(round(max_width + 40)) # a little oversized
image = PIL.Image.new(grayscale, (width, height), color=PIXEL_OFF)
draw = PIL.ImageDraw.Draw(image)
# draw each line of text
vertical_position = 5
horizontal_position = 5
line_spacing = int(round(max_height * 0.8)) # reduced spacing seems better
for line in lines:
draw.text((horizontal_position, vertical_position),
line, fill=PIXEL_ON, font=font)
vertical_position += line_spacing
# crop the text
c_box = PIL.ImageOps.invert(image).getbbox()
image = image.crop(c_box)
return image
if __name__ == '__main__':
compare_dir = pathlib.PurePath(sys.argv[1])
corpus_dir = pathlib.PurePath(sys.argv[2])
images = []
scores = []
for txtfile in os.listdir(str(compare_dir)):
fname = pathlib.PurePath(sys.argv[1]).joinpath(txtfile)
if fname.suffix != '.txt':
continue
imgpath = fname.with_suffix('.png')
corpname = corpus_dir.joinpath(imgpath.name)
img = text_image(str(fname))
corpimg = PIL.Image.open(str(corpname))
img = img.resize(corpimg.size, PIL.Image.LANCZOS)
corpimg.close()
img.save(str(imgpath), 'png')
img.close()
images.append(str(imgpath))
score = dssim_score(str(corpname), str(imgpath))
print('{}: {}'.format(corpname, score))
scores.append(score)
print('Score: {}'.format(sum(scores)/len(scores)))
スコアリングプロセス:
- 各コーパスイメージに対して送信を実行し、コーパス
.txt
ファイルと同じステムを持つファイルに結果を出力します(手動で実行)。 - 20ポイントのフォントを使用して、各テキストファイルをPNG画像に変換し、空白を切り取ります。
- Lanczosリサンプリングを使用して、結果画像のサイズを元の画像のサイズに変更します。
- を使用して、各テキスト画像を元の画像と比較し
dssim
ます。 - 各テキストファイルのdssimスコアを出力します。
- 平均スコアを出力します。
構造的類似性(dssim
スコアを計算するメトリック)は、人間の視覚と画像内のオブジェクトの識別に基づくメトリックです。わかりやすく言うと、2つの画像が人間に似ている場合、(おそらく)からのスコアが低くなりdssim
ます。
受賞作品は、平均スコアが最も低い作品になります。
.txt
ファイルに出力する」とはどういう意味ですか?プログラムはファイルにパイプされるテキストを出力する必要がありますか、それともファイルを直接出力する必要がありますか?