Last active
December 17, 2016 11:30
-
-
Save wsuzume/bd913d51d91126c3266ae0eae573bb45 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#coding: UTF-8 | |
import Image | |
import sys | |
def threshold(x,a): | |
if x > a: | |
return 1 | |
else: | |
return 0 | |
def gridconv(img,x,y,a,b,xlim,ylim): | |
i = x * a | |
j = y * b | |
buf1 = [] | |
for n in range(b): | |
buf0 = [] | |
if j+n >= ylim: | |
break | |
for m in range(a): | |
if i+m >= xlim: | |
break | |
buf0.append(threshold(sum(img.getpixel((i+m, j+n))), 400)) | |
buf1.append(buf0) | |
return buf1 | |
def foldgrid(xs): | |
s = 0 | |
for line in xs: | |
s += sum(line) | |
return s | |
def selectchar(x,i,flip): | |
if x == 0: | |
s0 = '感' | |
s1 = '想' | |
else: | |
s0 = '自' | |
s1 = '分' | |
if i%2 == 0 and flip == 0: | |
return s0 | |
elif i%2 == 0 and flip == 1: | |
return s1 | |
elif i%2 == 1 and flip == 0: | |
return s1 | |
else: | |
return s0 | |
def main(): | |
img = Image.open('str.png') | |
#img.show() | |
w, h = img.size | |
#pixel = img.getdata() | |
print w, h | |
a = w/75 | |
b = h/107 | |
print a, b, a*b | |
buf1 = [] | |
for j in range(107): | |
buf0 = [] | |
for i in range(75): | |
buf0.append(threshold(foldgrid(gridconv(img,i,j,a,b,w,h)), 10)) | |
buf1.append(buf0) | |
flip = 0 | |
s1 = [] | |
for line in buf1: | |
s0 = [] | |
for i in range(len(line)): | |
s0.append(selectchar(line[i], i, flip)) | |
s0.append('\n') | |
s1.append(s0) | |
if flip == 0: | |
flip = 1 | |
else: | |
flip = 0 | |
f = open('test.txt', 'a') | |
for line in s1: | |
f.write("".join(line)) | |
f.close() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment