00001
00002
00003
00004 from __future__ import division
00005
00006 from PIL import Image
00007
00008 from enhanced_grid import *
00009
00010 def to_byte(f):
00011 v = (int) (255 * f)
00012 if v < 0:
00013 v = 0
00014 elif v > 255:
00015 v = 255
00016
00017 return v
00018
00019
00020 def image_to_channels(image):
00021
00022 dims = image.size
00023
00024 grids = (
00025 Grid2D(dims, 0),
00026 Grid2D(dims, 0),
00027 Grid2D(dims, 0))
00028
00029 pix = image.load()
00030
00031 for index in grids[0].index_iter():
00032 pixel = pix[index]
00033
00034 for channel in range(3):
00035 grids[channel][index] = pixel[channel] / 255
00036
00037 return grids
00038
00039 def channels_to_image(channels):
00040
00041 image = Image.new('RGBA', channels[0].dims)
00042 pix = image.load()
00043
00044 for index in channels[0].index_iter():
00045 pix[index] = (
00046 to_byte(channels[0][index]),
00047 to_byte(channels[1][index]),
00048 to_byte(channels[2][index]))
00049
00050 return image
00051
00052 def multiply(grid, factor):
00053 for index in grid.index_iter():
00054 grid[index] = grid[index] * factor
00055
00056 def diffuse_super_saturation_window(src_grid, dest_grid, point, windowSize):
00057 if src_grid[point] > 1.0:
00058 excess = src_grid[point] - 1.0
00059 else:
00060 excess = 0
00061
00062 window_size = 2.0
00063
00064 excess_per_cell = excess / (window_size * window_size)
00065
00066 dest_grid[point] = src_grid[point] - excess
00067
00068 for index in src_grid.square_index_iter(point, windowSize):
00069 if(index != point):
00070 dest_grid[index] = dest_grid[index] + excess_per_cell
00071
00072 def diffuse_super_saturation(channels):
00073
00074 dims = channels[0].dims
00075
00076 for channel in channels:
00077 multiply(channel, 2.0)
00078
00079 dest_channels = (
00080 Grid2D(dims, 0),
00081 Grid2D(dims, 0),
00082 Grid2D(dims, 0))
00083
00084 for channel , dest_channel in zip(channels, dest_channels):
00085 for index in channel.index_iter():
00086 diffuse_super_saturation_window(channel, dest_channel, index, 1)
00087
00088 return channels_to_image(dest_channels)
00089
00090
00091
00092
00093 def demo():
00094 image = Image.open('screenshot_1.png')
00095 channels = image_to_channels(image)
00096 new_image = diffuse_super_saturation(channels)
00097 new_image.save('screenshot_1_ss.png')
00098
00099 demo()
00100