def magnify(img): out = Image(img.width, img.height, typecode='B') out.data[:] = img.data maxr = img.height/3 for y in xrange(img.height/2 - maxr, img.height/2 + maxr): for x in xrange(img.width/2 - maxr, img.width/2 + maxr): dx, dy = x - img.width/2, y - img.height/2 a = atan2(dy, dx) r = sqrt(dx ** 2 + dy ** 2) if r < maxr: nr = r*r / maxr nx, ny = nr*cos(a), nr*sin(a) out[x,y] = min(int(img[nx + img.width/2, ny + img.height/2]), 255) else: out[x,y] = img[x,y] return out
def magnify(img): out = Image(img.width, img.height, typecode='B') out.data[:] = img.data maxr = img.height / 3 for y in xrange(img.height / 2 - maxr, img.height / 2 + maxr): for x in xrange(img.width / 2 - maxr, img.width / 2 + maxr): dx, dy = x - img.width / 2, y - img.height / 2 a = atan2(dy, dx) r = sqrt(dx**2 + dy**2) if r < maxr: nr = r * r / maxr nx, ny = nr * cos(a), nr * sin(a) out[x, y] = min(int(img[nx + img.width / 2, ny + img.height / 2]), 255) else: out[x, y] = img[x, y] return out
def create_array_mask(): font = ImageFont.truetype("game_over.ttf", 100) mask = font.getmask("PyPy Rocks!") msk_x, msk_y = mask.size arraymask = Image(msk_x, msk_y, typecode='B') for x in range(msk_x): for y in range(msk_y): arraymask[x, y] = mask.getpixel((x, y)) return arraymask
class BilinImage(Image): def __getitem__(self, (x, y)): if isinstance(x, float) and isinstance(y, float): x0, x1 = int(floor(x)), int(ceil(x)) y0, y1 = int(floor(y)), int(ceil(y)) xoff, yoff = x - x0, y - y0 return (1.0-xoff)*(1.0-yoff) * self[x0, y0] + \ (1.0-xoff)*( yoff) * self[x0, y1] + \ ( xoff)*(1.0-yoff) * self[x1, y0] + \ ( xoff)*( yoff) * self[x1, y1] else: return Image.__getitem__(self, (x, y))
def sobel(self, horizontal=True, vertical=True): out = Image(self.width, self.height) for y in range(1, self.height - 1): for x in range(1, self.width - 1): if horizontal: dx = -1.0 * self[x - 1, y - 1] + 1.0 * self[x + 1, y - 1] + \ -2.0 * self[x - 1, y] + 2.0 * self[x + 1, y] + \ -1.0 * self[x - 1, y + 1] + 1.0 * self[x + 1, y + 1] else: dx = self[x, y] if vertical: dy = -1.0 * self[x - 1, y - 1] - 2.0 * self[x, y - 1] - 1.0 * self[x + 1, y - 1] + \ 1.0 * self[x - 1, y + 1] + 2.0 * self[x, y + 1] + 1.0 * self[x + 1, y + 1] else: dy = self[x, y] out[x, y] = min(int(sqrt(dx * dx + dy * dy) / 4.0), 255) return out
def mplayer(Image, fn='tv://', options=''): f = subprocess.Popen( 'mplayer -really-quiet -noframedrop ' + options + ' ' '-vo yuv4mpeg:file=/dev/stdout 2>/dev/null </dev/null ' + fn, universal_newlines=False, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, ).stdout hdr = f.readline() m = re.search('W(\d+) H(\d+)', str(hdr)) w, h = int(m.group(1)), int(m.group(2)) while True: hdr = f.readline() if hdr != b'FRAME\n': break data = array('B') data.fromfile(f, w * h) yield Image(w, h, data=list(data)) f.read(w * h // 2) # Color data
def fisheye(img, fraction=2, bilinear=False): if bilinear: img = BilinImage(img.width, img.height, data=img.data) else: img = NNImage(img.width, img.height, data=img.data) out = Image(img.width, img.height, data=img.data[:]) maxr = img.height / (fraction + 1) for y in range(int(img.height / 2 - maxr), int(img.height / 2 + maxr)): for x in range(int(img.width / 2 - maxr), int(img.width / 2 + maxr)): dx, dy = x - img.width / 2, y - img.height / 2 a = atan2(dy, dx) r = sqrt(dx**2 + dy**2) if r < maxr: nr = r * r / maxr nx, ny = nr * cos(a), nr * sin(a) out[x, y] = min( int(img[nx + img.width / 2, ny + img.height / 2]), 255) else: out[x, y] = img[x, y] return out
class NNImage(Image): def __getitem__(self, (x, y)): return Image.__getitem__(self, (int(x + 0.5), int(y + 0.5)))
y0, y1 = int(floor(y)), int(ceil(y)) xoff, yoff = x - x0, y - y0 return (1.0 - xoff) * (1.0 - yoff) * self[x0, y0] + \ (1.0 - xoff) * ( yoff) * self[x0, y1] + \ ( xoff) * (1.0 - yoff) * self[x1, y0] + \ ( xoff) * ( yoff) * self[x1, y1] else: return Image.__getitem__(self, (x, y)) if __name__ == '__main__': import sys if sys.implementation.name == "graalpython" or "test" in sys.argv: img = Image(5, 5, data=([11, 12, 13, 14, 15] + [21, 22, 23, 24, 25] + [31, 32, 33, 34, 35] + [41, 42, 43, 44, 45] + [51, 52, 53, 54, 55])) print(img.sobel().data) print(img.fisheye().data) print(img.fisheye(bilinear=True).data) else: import re, subprocess from time import time def mplayer(Image, fn='tv://', options=''): f = subprocess.Popen( 'mplayer -really-quiet -noframedrop ' + options + ' ' '-vo yuv4mpeg:file=/dev/stdout 2>/dev/null </dev/null ' + fn, universal_newlines=False, shell=True,