Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PIL import Image
- def get_grad_value_of_channel(first_color, second_color, index, length):
- return (first_color * (length - 1 - index) + second_color * index) / (length - 1)
- def get_grad_value_of_color(first_color, second_color, index, length):
- return int(get_grad_value_of_channel(first_color[0], second_color[0], index, length)),\
- int(get_grad_value_of_channel(first_color[1], second_color[1], index, length)),\
- int(get_grad_value_of_channel(first_color[2], second_color[2], index, length)),
- def main():
- filename = "/home/melon/Desktop/pic.png"
- original_image = Image.open(filename)
- original_pixels = original_image.load()
- original_image.show()
- x_scale = 5
- y_scale = 5
- scaled_image = Image.new('RGB', ((original_image.width - 1) * x_scale, (original_image.height - 1) * y_scale), "black")
- scaled_pixels = scaled_image.load()
- for x in range(original_image.width - 1):
- for y in range(original_image.height - 1):
- left_side_colors = []
- for dy in range(y_scale + 1):
- left_side_colors.append(get_grad_value_of_color(
- original_pixels[x, y],
- original_pixels[x, y + 1],
- dy,
- y_scale + 1
- ))
- right_side_colors = []
- for dy in range(y_scale + 1):
- right_side_colors.append(get_grad_value_of_color(
- original_pixels[x + 1, y],
- original_pixels[x + 1, y + 1],
- dy,
- y_scale + 1
- ))
- for dx in range(x_scale):
- for dy in range(y_scale):
- color = get_grad_value_of_color(left_side_colors[dy], right_side_colors[dy], dx, x_scale + 1)
- scaled_pixels[x * x_scale + dx, y * y_scale + dy] = color
- scaled_image.show()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement