Advertisement
milanmetal

[Python] Pixelization v1 / Averaging method

Mar 3rd, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.24 KB | None | 0 0
  1. from PIL import Image
  2.  
  3. # PIL accesses images in Cartesian co-ordinates, so it is Image[columns, rows]
  4. # img = Image.new( 'RGB', (1280, 720), "black") # create a new black image
  5. # img = Image.open("rsz_2lemibrat.jpg")
  6.  
  7. img = Image.open("lena.jpg")
  8. pixels = img.load() # create the pixel map
  9.  
  10. # https://stackoverflow.com/questions/6667201/how-to-define-a-two-dimensional-array-in-python
  11. # Creates a list containing 32 lists, each of 32 items, all set to 0
  12. # -----------------------------------------------------------------------------
  13.  
  14. # Image dimensions
  15. img_width   = img.size[0]
  16. img_height  = img.size[1]
  17.  
  18. # kernel size, basically dimensions of the "pixel"
  19. k_width, k_height = 2, 2
  20.  
  21. # Auxilliary variables used to check whether the window has reached its
  22. # size, the size of the kernel
  23. k_i, k_j = 0, 0
  24.  
  25. # Lets start from position 0,0
  26. x_curr, y_curr = 0, 0
  27.  
  28. # Used as a temporary kernel, used over and over again.
  29. kernel_tmp = {}
  30.  
  31. # This wont allow me to track the dimensions of kernels, take care of this in v2
  32. kernels = {}
  33.  
  34. # After the image is "kernelized", each kernel has its position [xpos][pos]
  35. # so this is basically dictionary of dictionaries?
  36. # Well... that escalated quickly.
  37. kernel_xpos, kernel_ypos = 0, 0
  38.  
  39. # kernel = [[0 for x in range(w)] for y in range(h)]
  40.  
  41.  # ---------------------------------------------------------------
  42. def averageKernelRGB(pixels, x_curr, x_upper, y_curr, y_upper):
  43.     "Calculates average R, G, B values over given kernel"
  44.     kernel_average = {}
  45.  
  46.     # ???????????????????????????????????????
  47.     # Ispostavlja se da ove moram da dodefinisem u funkciji ponovo
  48.     # Postoji li nacin da ih on u funkciji vidi kao GLOBALNE, kao C sto ih vidi?
  49.     # Ocigledno da su definisane iznad svega...
  50.     k_i, k_j = 0, 0
  51.     k_R, k_G, k_B = 0, 0, 0
  52.     for i in range(x_curr, x_upper):
  53.        for j in range (y_curr, y_upper):
  54.            # kernel_average[k_i, k_j] = pixels[i, j]
  55.            k_R += pixels[i,j][0]
  56.            k_G += pixels[i,j][1]
  57.            k_B += pixels[i,j][2]
  58.  
  59.            k_j += 1
  60.            # print('k_j = ' + str(k_j) + ' a vrednost RGB = ' + str(pixels[i, j]))
  61.            if k_j == k_height:
  62.                k_j = 0
  63.  
  64.        k_i += 1
  65.        # print('k_i = ' + str(k_i) + ' a vrednost RGB = ' + str(pixels[i, j]))
  66.        if k_i == k_width:
  67.            k_i = 0
  68.  
  69.     # So, the dimension of the kernel is actually upper X value - current X value
  70.     # Same thing for Y coordinate. This should work for all image resolutions.
  71.     pixel_count = (y_upper - y_curr) * (x_upper - x_curr)
  72.  
  73.     avrg_R = k_R / pixel_count
  74.     avrg_G = k_G / pixel_count
  75.     avrg_B = k_B / pixel_count
  76.  
  77.     for i in range(x_curr, x_upper):
  78.        for j in range (y_curr, y_upper):
  79.            pixels[i, j] = (avrg_R, avrg_G, avrg_B)
  80.     return;
  81. # ---------------------------------------------------------------
  82.  
  83. print('Image resolution: ' + str(img.size))     # Prints (250, 250)
  84. # Loop from 0 to img.size[0] with k_width step
  85. for x_curr in range(0, img.size[0], k_width):
  86. # for x in range(img.size[0]):
  87.     x_upper = x_curr + k_width
  88.     if x_upper > img_width:
  89.         # If we're going to exceed img_width, then increment just by what's left.
  90.         # HERE, THIS WILL WORK PROPERLY JUST FOR SQUARE IMAGES
  91.         x_upper = img_width - x_curr
  92.  
  93.     for y_curr in range(0, img.size[1], k_height):
  94.  
  95.         y_upper = y_curr + k_height
  96.         if y_upper > img_height:
  97.             # If we're going to exceed img_height, then increment just by what's left.
  98.             # HERE, THIS WILL WORK PROPERLY JUST FOR SQUARE IMAGES
  99.             y_upper = img_height - y_curr
  100.  
  101.         # Moved this part to the function
  102.         # --------------------------------------
  103.         # for i in range(x_curr, x_upper):
  104.         #
  105.         #     for j in range (y_curr, y_upper):
  106.         #         kernel_tmp[k_i, k_j] = pixels[i, j]
  107.         #
  108.         #         k_j += 1
  109.         #         # print('k_j = ' + str(k_j) + ' a vrednost RGB = ' + str(pixels[i, j]))
  110.         #         if k_j == k_height:
  111.         #             k_j = 0
  112.         #
  113.         #     k_i += 1
  114.         #     # print('k_i = ' + str(k_i) + ' a vrednost RGB = ' + str(pixels[i, j]))
  115.         #     if k_i == k_width:
  116.         #         k_i = 0
  117.  
  118.         # Python's parameters are sent by reference by default, so I just saved
  119.         # some time and manipulated original pixels.
  120.         # kernel_tmp values is a current kernel filled with averaged colors.
  121.         kernel_tmp = averageKernelRGB(pixels, x_curr, x_upper, y_curr, y_upper)
  122.         # --------------------------------------
  123.  
  124.         # Each time i and j loops are executed, one kernel_tmp is
  125.         # filled and ready for use.
  126.         kernels[kernel_xpos, kernel_ypos] = kernel_tmp
  127.         kernel_ypos += 1
  128.     kernel_xpos += 1
  129.     # We need to reset it to count from the start
  130.     kernel_ypos = 0
  131.  
  132. print('X_POS = ' + str(kernel_xpos) + ' Y_POS = ' + str(kernel_ypos))
  133.  
  134. img.show()
  135.  
  136.  
  137. # print(kernel_tmp)
  138.         # print('x = ' + str(x_curr) + ' y = ' + str(y_curr))
  139.         #x_curr = x_curr + k_width
  140. # print(kernels[56,34])
  141. # Prints out keys. Debug purposes.
  142. # for key in kernels.keys():
  143. #   print(key)
  144. # print(kernels[16,54])
  145. # print(kernels[16,54][2,4])
  146. # print(kernels[16,54][2,4][2])
  147.  
  148. # Lets save the code below because its part of my first Python script ever...
  149. # Lets save the code below because its part of my first Python script ever...
  150.  
  151. # # Lets calculate sum of all R, G and B components in all kernel-captured pixels
  152. # k_R, k_G, k_B = 0, 0, 0
  153. # for i in range(0, k_width):
  154. #     for j in range (0, k_height):
  155. #         # k_R += kernel_tmp[i,j][0]
  156. #         # k_G += kernel_tmp[i,j][1]
  157. #         # k_B += kernel_tmp[i,j][2]
  158. #         k_R += kernels[50,63][i,j][0]
  159. #         k_G += kernels[50,63][i,j][1]
  160. #         k_B += kernels[50,63][i,j][2]
  161. #
  162. # # Hehe, nisi dovoljno mocan ako ne mozes ovo ha ha ha
  163. # # k_R, k_G, k_B /= 32, 32, 32
  164. # k_R /= k_width * k_height
  165. # k_G /= k_width * k_height
  166. # k_B /= k_width * k_height
  167. # print('Srednja vrijednost: R = ' + str(k_R) + ' G = ' + str(k_G) + ' B = ' + str(k_B))
  168. #
  169. # # Start all over
  170. # x_curr, y_curr = 0, 0
  171. # for i in range(x_curr, x_curr + k_width):
  172. #     for j in range (y_curr, y_curr + k_height):
  173. #         pixels[i, j] = (k_R, k_G, k_B)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement