Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import urllib2
- from PIL import Image
- import sys
- import datetime
- import math
- if len(sys.argv) < 4:
- print "params: image, left_x, top_y"
- colors = [
- (255, 255, 255),
- (228, 228, 228),
- (136, 136, 136),
- (34, 34, 34),
- (255, 167, 209),
- (229, 0, 0),
- (229, 149, 0),
- (160, 106, 66),
- (229, 217, 0),
- (148, 224, 68),
- (2, 190, 1),
- (0, 211, 221),
- (0, 131, 199),
- (0, 0, 234),
- (207, 110, 228),
- (130, 0, 128),
- ]
- BLOCK_SIZE = 64
- RADIUS = 7
- BLOCKS = 1
- SIZE = BLOCKS * (BLOCK_SIZE * RADIUS + BLOCK_SIZE + BLOCK_SIZE * RADIUS)
- OFFSET = (SIZE - BLOCK_SIZE) / 2
- tmpl_img = Image.open(sys.argv[1])
- tmpl_img_data = tmpl_img.getdata()
- x = int(sys.argv[2])
- y = int(sys.argv[3])
- #64_64 block coordinates
- x64 = x/64 + RADIUS
- y64 = y/64 + RADIUS
- x_off = x % 64
- y_off = y % 64
- print x64
- print y64
- print x_off
- print y_off
- try:
- img = Image.new('RGB', (SIZE, SIZE), (255, 255, 255))
- pix = img.load()
- for center_x in [x64]:
- for center_y in [y64]:
- bmp_filename = str(center_x) + "." + str(center_y) + ".bmp"
- print "Downloading", bmp_filename
- request = urllib2.Request(
- "http://pixelcanvas.io/api/bigchunk/" + bmp_filename,
- None,
- {'User-agent':'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)'}
- )
- remote_bmp = urllib2.urlopen(request)
- raw_data = remote_bmp.read()#.ljust(460800,"\0")
- current_byte = 0
- for bigchunk_y in range(center_y-RADIUS,center_y+RADIUS+1):
- for bigchunk_x in range(center_x-RADIUS,center_x+RADIUS+1):
- for block_y in range(BLOCK_SIZE):
- current_y = (bigchunk_y -center_y) * BLOCK_SIZE + block_y
- for block_x in range(0,BLOCK_SIZE,2):
- current_x = (bigchunk_x - center_x) * BLOCK_SIZE + block_x
- pix[current_x + OFFSET, current_y + OFFSET] = colors[ord(raw_data[current_byte]) >> 4]
- pix[current_x+1 + OFFSET, current_y + OFFSET] = colors[ord(raw_data[current_byte]) & 0x0F]
- current_byte += 1
- print "Canvas successfully downloaded!"
- now = datetime.datetime.utcnow().strftime("%Y%m%d%H%MUTC")
- area = (x_off, y_off, tmpl_img.width+x_off, tmpl_img.height+y_off)
- cnvs_img = img.crop(area)
- #cnvs_img.save(now + '.png')
- except Exception:
- print "Error downloading canvas!"
- raise
- cnvs_img_data = cnvs_img.getdata()
- cnt = 0
- for j in range(cnvs_img.height):
- for i in range(cnvs_img.width):
- if tmpl_img_data[j*cnvs_img.width + i][3] > 0:
- min_distance = 256**3
- for color_index, color in enumerate(colors):
- distance = math.sqrt((tmpl_img_data[j*cnvs_img.width + i][0] - color[0])**2 + (tmpl_img_data[j*cnvs_img.width + i][1] - color[1])**2 + (tmpl_img_data[j*cnvs_img.width + i][2] - color[2])**2)
- if distance < min_distance:
- min_distance = distance
- best_color = color_index
- if (colors[best_color] <> cnvs_img_data[j*cnvs_img.width + i]):
- print "nesouhlasi pixel: "+repr(i+x)+", "+repr(j+y)
- cnt += 1
- print "Ma byt: "+repr(colors[best_color])+"("+repr(tmpl_img_data[j*cnvs_img.width + i])+") je: "+repr(cnvs_img_data[j*cnvs_img.width + i])
- print "Nesouhlasi "+repr(cnt)+" pixelu"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement