Advertisement
Guest User

Untitled

a guest
Feb 27th, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.48 KB | None | 0 0
  1. import Image
  2. import struct
  3.  
  4. if __name__ == '__main__':
  5.     width = 1280
  6.     height = 720 - 2
  7.  
  8.     img_grey = Image.new('L', (width, height))
  9.     pix_grey = img_grey.load()
  10.  
  11.     img_color = Image.new('RGB', (width, height))
  12.     pix_color = img_color.load()
  13.  
  14.     print "start calc"
  15.     with open("pic.bin", "rb") as fin:
  16.         for y in range(2):
  17.             for x in range(width):
  18.                 fin.read(1)
  19.  
  20.         for y in range(height):
  21.             for x in range(width):
  22.                 v, = struct.unpack('<B', fin.read(1))
  23.                 pix_grey[x, y] = (v)
  24.  
  25.     for y in range(1,height-1):
  26.         for x in range(1,width-1):
  27.             ind = (x-1) % 2 + (y % 2) * 2;
  28.             '''
  29.            if ind == 0:  # red
  30.                red = pix_grey[x, y]
  31.                green = 128
  32.                blue = 128
  33.            elif ind == 1:  # green on RG row
  34.                red = 128
  35.                green = pix_grey[x, y]
  36.                blue = 128
  37.            elif ind == 2:  # green on GB row
  38.                red = 128
  39.                green = pix_grey[x, y]
  40.                blue = 128
  41.            elif ind == 3:  # blue
  42.                red = 128
  43.                green = 128
  44.                blue = pix_grey[x, y]
  45.            '''
  46.             if ind == 0:  # red
  47.                 red = pix_grey[x, y];
  48.                 green = (pix_grey[x, y - 1] + pix_grey[x - 1, y] + pix_grey[x + 1, y] + pix_grey[x, y + 1]) / 4;
  49.                 blue = (pix_grey[x - 1, y - 1] + pix_grey[x + 1, y - 1] + pix_grey[x - 1, y + 1] + pix_grey[x + 1, y + 1]) / 4;
  50.             elif ind == 1:  # green on RG row
  51.                 red = (pix_grey[x - 1, y] + pix_grey[x + 1, y]) / 2;
  52.                 green = pix_grey[x, y];
  53.                 blue = (pix_grey[x, y - 1] + pix_grey[x, y + 1]) / 2;
  54.             elif ind == 2:  # green on GB row
  55.                 red = (pix_grey[x, y - 1] + pix_grey[x, y + 1]) / 2;
  56.                 green = pix_grey[x, y];
  57.                 blue = (pix_grey[x - 1, y] + pix_grey[x + 1, y]) / 2;
  58.             elif ind == 3:  # blue
  59.                 red = (pix_grey[x - 1, y - 1] + pix_grey[x + 1, y - 1] + pix_grey[x - 1, y + 1] + pix_grey[x + 1, y + 1]) / 4;
  60.                 green = (pix_grey[x, y - 1] + pix_grey[x - 1, y] + pix_grey[x + 1, y] + pix_grey[x, y + 1]) / 4;
  61.                 blue = pix_grey[x, y];
  62.  
  63.             pix_color[x, y] = (red, green, blue)
  64.  
  65.     print "save result"
  66.     img_grey.save("01_grey.png")
  67.     img_color.save("02_color.png")
  68.     print "done"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement