Advertisement
Guest User

Gradient scaling

a guest
May 26th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.96 KB | None | 0 0
  1. from PIL import Image
  2.  
  3.  
  4. def get_grad_value_of_channel(first_color, second_color, index, length):
  5.     return (first_color * (length - 1 - index) + second_color * index) / (length - 1)
  6.  
  7.  
  8. def get_grad_value_of_color(first_color, second_color, index, length):
  9.     return int(get_grad_value_of_channel(first_color[0], second_color[0], index, length)),\
  10.            int(get_grad_value_of_channel(first_color[1], second_color[1], index, length)),\
  11.            int(get_grad_value_of_channel(first_color[2], second_color[2], index, length)),
  12.  
  13.  
  14. def main():
  15.     filename = "/home/melon/Desktop/pic.png"
  16.     original_image = Image.open(filename)
  17.     original_pixels = original_image.load()
  18.     original_image.show()
  19.     x_scale = 5
  20.     y_scale = 5
  21.     scaled_image = Image.new('RGB', ((original_image.width - 1) * x_scale, (original_image.height - 1) * y_scale), "black")
  22.     scaled_pixels = scaled_image.load()
  23.     for x in range(original_image.width - 1):
  24.         for y in range(original_image.height - 1):
  25.             left_side_colors = []
  26.             for dy in range(y_scale + 1):
  27.                 left_side_colors.append(get_grad_value_of_color(
  28.                     original_pixels[x, y],
  29.                     original_pixels[x, y + 1],
  30.                     dy,
  31.                     y_scale + 1
  32.                 ))
  33.             right_side_colors = []
  34.             for dy in range(y_scale + 1):
  35.                 right_side_colors.append(get_grad_value_of_color(
  36.                     original_pixels[x + 1, y],
  37.                     original_pixels[x + 1, y + 1],
  38.                     dy,
  39.                     y_scale + 1
  40.                 ))
  41.             for dx in range(x_scale):
  42.                 for dy in range(y_scale):
  43.                     color = get_grad_value_of_color(left_side_colors[dy], right_side_colors[dy], dx, x_scale + 1)
  44.                     scaled_pixels[x * x_scale + dx, y * y_scale + dy] = color
  45.     scaled_image.show()
  46.  
  47.  
  48. if __name__ == '__main__':
  49.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement