Advertisement
here2share

# bicubic.py

Sep 3rd, 2023
889
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.47 KB | None | 0 0
  1. # bicubic.py
  2.  
  3. def bicubic_interpolation_rgb(c0, c1, c2, c3):
  4.     # Initialize the result as a 5x5 gradient of RGB colors
  5.     result = [[(0, 0, 0)] * 5 for _ in range(5)]
  6.  
  7.     for j in range(5):
  8.         for i in range(5):
  9.             rgb = []
  10.             for channel in range(3):  # Loop through the RGB channels (0: Red, 1: Green, 2: Blue)
  11.                 # Calculate the bicubic interpolation for each channel
  12.                 pixel_value = bicubic(
  13.                     c0[channel],
  14.                     c1[channel],
  15.                     c2[channel],
  16.                     c3[channel],
  17.                     i, j
  18.                 )
  19.                 rgb.append(pixel_value)
  20.             # Store the interpolated value as an RGB tuple in the result
  21.             result[j][i] = tuple(rgb)
  22.  
  23.     return result  # Return the 5x5 gradient of RGB colors
  24.  
  25. # Bicubic interpolation function
  26. def bicubic(c0, c1, c2, c3, x, y):
  27.     # Implement bicubic interpolation here
  28.     a = c0 + (c1 - c0) * (0.5 * x)
  29.     b = c0 + (c2 - c0) * (0.5 * y)
  30.     c = (a + b) / 2
  31.     d = (c + (c3 - c) * (0.5 * ((x + y) / 4)))
  32.     z = (max(4, x + y) - 4)
  33.     pixel_value = int(d * 2 if z else c)
  34.     return pixel_value
  35.  
  36. # Example usage:
  37. c0 = (0, 255, 255)
  38. c1 = (0, 0, 255)
  39. c2 = (255, 255, 0)
  40. c3 = (0, 0, 0)
  41.  
  42. # Perform bicubic interpolation for the 5x5 gradient of RGB colors
  43. interpolated_gradient = bicubic_interpolation_rgb(c0, c1, c2, c3)
  44.  
  45. # Print the interpolated gradient
  46. for row in interpolated_gradient:
  47.     print(row)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement