Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: hacking-thursday/chinese-char-art
base: 1a5c70f6c4
...
head fork: hacking-thursday/chinese-char-art
compare: 016d4ce780
  • 2 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
View
BIN  img/Lenna.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
0  py/__init__.py
No changes.
View
65 py/img.py
@@ -0,0 +1,65 @@
+#!/usr/bin/env python
+# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
+# -*- coding: utf8 -*-
+#
+# Author 2012 Hsin-Yi Chen
+import Image
+import ImageFilter
+
+def split_img(img, n):
+ ret = []
+ n_x = img.size[0] / n
+ n_y = img.size[1] / n
+ dbg('n of x: {0}, y: {1}'.format(n_x, n_y))
+
+ # left is x
+ # upper is y
+ left = 0
+ for i in range(0, n_x):
+ upper = 0
+ for j in range(0, n_y):
+ box = (left, upper, left+n, upper+n)
+ #dbg('{}, {}, {}, {}'.format(left, upper, left+n, upper+n))
+ region = img.crop(box)
+ ret.append(ChrPixel(region))
+ upper += n
+ left += n
+ return ret
+
+def dbg(msg):
+ print msg
+
+class ChrPixel(object):
+
+ def __init__(self, img):
+ # split to 9 pixel
+ self._img = img
+
+ def pixel(self):
+ return self._img.getpixel((0,0)) == 255 and 1 or 0
+
+class ImageEncoder(object):
+
+ def __init__(self, font_size=4):
+ self.font_size = font_size
+
+ def read(self, fname):
+ # covert to gray
+ self._img = Image.open(fname).convert('L')
+ dbg('SIZE: {0}x{1}, BOXS: {2}'.format(self._img.size[0],
+ self._img.size[1],
+ self._img.getbbox()))
+
+ # for each pixel, if pixel > 150: white
+ # else: black
+ for i in range(self._img.size[0]):
+ for j in range(self._img.size[1]):
+ rgb = self._img.getpixel((i,j)) > 150 and 255 or 0
+ self._img.putpixel((i,j), rgb)
+
+ def blocks(self):
+ # split to block by font size
+ # n is pixels is included in a block
+ # left is x
+ # upper is y
+ return split_img(self._img, 9)
View
28 test/test_all.py
@@ -0,0 +1,28 @@
+#!/usr/bin/env python
+# -*- encoding=utf8 -*-
+#
+# Author 2011 Hsin-Yi Chen
+import os
+import unittest
+
+from py import img as myimg
+
+class UtilsTestCase(unittest.TestCase):
+
+ def setUp(self):
+ self.filename = os.path.join('img', 'Lenna.jpg')
+ self.imgpr = myimg.ImageEncoder()
+ self.imgpr.read(self.filename)
+
+ def test_blocks(self):
+ """test transform keyword arguments's name to command's option name.
+ """
+ print self.imgpr.blocks()[0].pixel()
+
+def suite():
+ suite = unittest.TestSuite()
+ suite.addTest(unittest.makeSuite(UtilsTestCase, 'test'))
+ return suite
+
+if __name__ == '__main__':
+ unittest.main(defaultTest='suite')

No commit comments for this range

Something went wrong with that request. Please try again.