Advertisement
Guest User

roumenuv pixel kontroler

a guest
Jul 20th, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.12 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4.  
  5. import urllib2
  6. from PIL import Image
  7. import sys
  8. import datetime
  9. import math
  10.  
  11. if len(sys.argv) < 4:
  12.     print "params: image, left_x, top_y"
  13.  
  14. colors = [
  15.     (255, 255, 255),
  16.     (228, 228, 228),
  17.     (136, 136, 136),
  18.     (34, 34, 34),
  19.     (255, 167, 209),
  20.     (229, 0, 0),
  21.     (229, 149, 0),
  22.     (160, 106, 66),
  23.     (229, 217, 0),
  24.     (148, 224, 68),
  25.     (2, 190, 1),
  26.     (0, 211, 221),
  27.     (0, 131, 199),
  28.     (0, 0, 234),
  29.     (207, 110, 228),
  30.     (130, 0, 128),
  31. ]
  32.  
  33.  
  34.  
  35.  
  36. BLOCK_SIZE = 64
  37. RADIUS = 7
  38. BLOCKS = 1
  39. SIZE = BLOCKS * (BLOCK_SIZE * RADIUS + BLOCK_SIZE + BLOCK_SIZE * RADIUS)
  40. OFFSET = (SIZE - BLOCK_SIZE) / 2
  41.  
  42. tmpl_img = Image.open(sys.argv[1])
  43. tmpl_img_data = tmpl_img.getdata()
  44. x = int(sys.argv[2])
  45. y = int(sys.argv[3])
  46.  
  47.  
  48. #64_64 block coordinates
  49. x64 = x/64 + RADIUS
  50. y64 = y/64 + RADIUS
  51.  
  52. x_off = x % 64
  53. y_off = y % 64
  54.  
  55. print x64
  56. print y64
  57.  
  58. print x_off
  59. print y_off
  60.  
  61. try:
  62.     img = Image.new('RGB', (SIZE, SIZE), (255, 255, 255))
  63.     pix = img.load()
  64.     for center_x in [x64]:
  65.         for center_y in [y64]:
  66.             bmp_filename = str(center_x) + "." + str(center_y) + ".bmp"
  67.             print "Downloading", bmp_filename
  68.             request = urllib2.Request(
  69.                 "http://pixelcanvas.io/api/bigchunk/" + bmp_filename,
  70.                 None,
  71.                 {'User-agent':'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)'}
  72.                 )
  73.             remote_bmp = urllib2.urlopen(request)
  74.             raw_data = remote_bmp.read()#.ljust(460800,"\0")
  75.             current_byte = 0
  76.             for bigchunk_y in range(center_y-RADIUS,center_y+RADIUS+1):
  77.                 for bigchunk_x in range(center_x-RADIUS,center_x+RADIUS+1):
  78.                     for block_y in range(BLOCK_SIZE):
  79.                         current_y = (bigchunk_y -center_y) * BLOCK_SIZE + block_y
  80.                         for block_x in range(0,BLOCK_SIZE,2):
  81.                             current_x = (bigchunk_x - center_x) * BLOCK_SIZE + block_x
  82.                             pix[current_x + OFFSET, current_y + OFFSET] = colors[ord(raw_data[current_byte]) >> 4]
  83.                             pix[current_x+1  + OFFSET, current_y + OFFSET] = colors[ord(raw_data[current_byte]) & 0x0F]
  84.                             current_byte += 1
  85.     print "Canvas successfully downloaded!"
  86.     now = datetime.datetime.utcnow().strftime("%Y%m%d%H%MUTC")
  87.     area = (x_off, y_off, tmpl_img.width+x_off, tmpl_img.height+y_off)
  88.     cnvs_img = img.crop(area)
  89.     #cnvs_img.save(now + '.png')
  90. except Exception:
  91.     print "Error downloading canvas!"
  92.     raise
  93. cnvs_img_data = cnvs_img.getdata()
  94. cnt = 0
  95. for j in range(cnvs_img.height):
  96.     for i in range(cnvs_img.width):
  97.         if tmpl_img_data[j*cnvs_img.width + i][3] > 0:
  98.             min_distance = 256**3
  99.             for color_index, color in enumerate(colors):
  100.                 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)
  101.                 if distance < min_distance:
  102.                     min_distance = distance
  103.                     best_color = color_index
  104.             if (colors[best_color] <> cnvs_img_data[j*cnvs_img.width + i]):
  105.                 print "nesouhlasi pixel: "+repr(i+x)+", "+repr(j+y)
  106.                 cnt += 1
  107.                 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])
  108. print "Nesouhlasi "+repr(cnt)+" pixelu"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement