Skip to content

Commit 6ca7b7d

Browse files
committed
Image processing now uses PIL
- Removed scipy dependancy - Removed numpy dependancy
1 parent c7df148 commit 6ca7b7d

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

src/imgshow.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
11
#!/usr/bin/env python2
22
#{{{ Imports
33
from __future__ import print_function, division
4+
from PIL import Image as img, ImageOps as imgops
45
import sys
5-
import scipy.misc as img
6-
import numpy as np
7-
from math import floor, ceil
86
#}}}
97

108

119

1210

11+
#{{{ Reshape array
12+
def reshape(seq, size):
13+
result = []
14+
(height, width) = size
15+
for i, item in enumerate(seq):
16+
if not i < height*width:
17+
break
18+
if i%width == 0:
19+
result.append([])
20+
result[-1].append(item)
21+
return result
22+
#}}}
23+
24+
1325
#{{{ Resize image to a specific (terminal) width
14-
def resize(image, width):
15-
iheight = image.shape[0]
16-
iwidth = image.shape[1]
26+
def resize(image, size):
27+
(height, width) = size
28+
iheight = image.size[1]
29+
iwidth = image.size[0]
1730

1831
twidth = width
1932

@@ -23,15 +36,15 @@ def resize(image, width):
2336
nwidth = int(nwidth)
2437
nheight = int(nheight)
2538

26-
return img.imresize(image, (nheight, nwidth))
39+
return image.resize((nwidth, nheight))
2740
#}}}
2841

2942

3043
#{{{ Map each pixel based on a function
3144
def imgmap(image, fn):
32-
height = image.shape[0]
33-
width = image.shape[1]
34-
result = np.reshape(np.repeat(fn(image[0][0]), height*width), (height, width))
45+
height = len(image)
46+
width = len(image[0])
47+
result = reshape([None for _ in range(height*width)], (height, width))
3548
for y in range(height):
3649
for x in range(width):
3750
result[y][x] = fn(image[y][x])
@@ -41,8 +54,8 @@ def imgmap(image, fn):
4154

4255
#{{{ Print pixel matrix to the screen
4356
def render(image):
44-
height = image.shape[0]
45-
width = image.shape[1]
57+
height = len(image)
58+
width = len(image[0])
4659
for y in range(height):
4760
for x in range(width):
4861
print(image[y][x].encode('utf-8'), end='')
@@ -53,11 +66,15 @@ def render(image):
5366

5467

5568
#{{{ Run
56-
def run(data, width=80, charset=' .,-:;!*=$#@'):
69+
def run(image, width=80, charset=' .,-:;!*=$#@'):
5770
normalize = lambda image: imgmap(image, lambda pixel: pixel/255)
5871
charmap = lambda image: imgmap(image, lambda pixel: charset[int(pixel * len(charset) - (1 if pixel>=1.0 else 0))])
5972

60-
render(charmap(normalize(resize(data, width))))
73+
image = resize(image, (None, width))
74+
image= imgops.grayscale(image)
75+
pixels = reshape(list(image.getdata()), (image.size[1], image.size[0]))
76+
77+
render(charmap(normalize(pixels)))
6178

6279
return True
6380
#}}}
@@ -96,12 +113,12 @@ def main(argv=None):
96113

97114
if argc >= 2:
98115
try:
99-
data = img.imread(argv[1], True)
116+
image = img.open(argv[1])
100117
except:
101118
print('%s: error: could not open %s' % (argv[0], argv[1]))
102119
return 1
103120

104-
run(data, width, chars)
121+
run(image, width, chars)
105122
return 0
106123

107124

0 commit comments

Comments
 (0)