Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PIL import Image
- # PIL accesses images in Cartesian co-ordinates, so it is Image[columns, rows]
- # img = Image.new( 'RGB', (1280, 720), "black") # create a new black image
- # img = Image.open("rsz_2lemibrat.jpg")
- img = Image.open("lena.jpg")
- pixels = img.load() # create the pixel map
- # https://stackoverflow.com/questions/6667201/how-to-define-a-two-dimensional-array-in-python
- # Creates a list containing 32 lists, each of 32 items, all set to 0
- # -----------------------------------------------------------------------------
- # Image dimensions
- img_width = img.size[0]
- img_height = img.size[1]
- # kernel size, basically dimensions of the "pixel"
- k_width, k_height = 2, 2
- # Auxilliary variables used to check whether the window has reached its
- # size, the size of the kernel
- k_i, k_j = 0, 0
- # Lets start from position 0,0
- x_curr, y_curr = 0, 0
- # Used as a temporary kernel, used over and over again.
- kernel_tmp = {}
- # This wont allow me to track the dimensions of kernels, take care of this in v2
- kernels = {}
- # After the image is "kernelized", each kernel has its position [xpos][pos]
- # so this is basically dictionary of dictionaries?
- # Well... that escalated quickly.
- kernel_xpos, kernel_ypos = 0, 0
- # kernel = [[0 for x in range(w)] for y in range(h)]
- # ---------------------------------------------------------------
- def averageKernelRGB(pixels, x_curr, x_upper, y_curr, y_upper):
- "Calculates average R, G, B values over given kernel"
- kernel_average = {}
- # ???????????????????????????????????????
- # Ispostavlja se da ove moram da dodefinisem u funkciji ponovo
- # Postoji li nacin da ih on u funkciji vidi kao GLOBALNE, kao C sto ih vidi?
- # Ocigledno da su definisane iznad svega...
- k_i, k_j = 0, 0
- k_R, k_G, k_B = 0, 0, 0
- for i in range(x_curr, x_upper):
- for j in range (y_curr, y_upper):
- # kernel_average[k_i, k_j] = pixels[i, j]
- k_R += pixels[i,j][0]
- k_G += pixels[i,j][1]
- k_B += pixels[i,j][2]
- k_j += 1
- # print('k_j = ' + str(k_j) + ' a vrednost RGB = ' + str(pixels[i, j]))
- if k_j == k_height:
- k_j = 0
- k_i += 1
- # print('k_i = ' + str(k_i) + ' a vrednost RGB = ' + str(pixels[i, j]))
- if k_i == k_width:
- k_i = 0
- # So, the dimension of the kernel is actually upper X value - current X value
- # Same thing for Y coordinate. This should work for all image resolutions.
- pixel_count = (y_upper - y_curr) * (x_upper - x_curr)
- avrg_R = k_R / pixel_count
- avrg_G = k_G / pixel_count
- avrg_B = k_B / pixel_count
- for i in range(x_curr, x_upper):
- for j in range (y_curr, y_upper):
- pixels[i, j] = (avrg_R, avrg_G, avrg_B)
- return;
- # ---------------------------------------------------------------
- print('Image resolution: ' + str(img.size)) # Prints (250, 250)
- # Loop from 0 to img.size[0] with k_width step
- for x_curr in range(0, img.size[0], k_width):
- # for x in range(img.size[0]):
- x_upper = x_curr + k_width
- if x_upper > img_width:
- # If we're going to exceed img_width, then increment just by what's left.
- # HERE, THIS WILL WORK PROPERLY JUST FOR SQUARE IMAGES
- x_upper = img_width - x_curr
- for y_curr in range(0, img.size[1], k_height):
- y_upper = y_curr + k_height
- if y_upper > img_height:
- # If we're going to exceed img_height, then increment just by what's left.
- # HERE, THIS WILL WORK PROPERLY JUST FOR SQUARE IMAGES
- y_upper = img_height - y_curr
- # Moved this part to the function
- # --------------------------------------
- # for i in range(x_curr, x_upper):
- #
- # for j in range (y_curr, y_upper):
- # kernel_tmp[k_i, k_j] = pixels[i, j]
- #
- # k_j += 1
- # # print('k_j = ' + str(k_j) + ' a vrednost RGB = ' + str(pixels[i, j]))
- # if k_j == k_height:
- # k_j = 0
- #
- # k_i += 1
- # # print('k_i = ' + str(k_i) + ' a vrednost RGB = ' + str(pixels[i, j]))
- # if k_i == k_width:
- # k_i = 0
- # Python's parameters are sent by reference by default, so I just saved
- # some time and manipulated original pixels.
- # kernel_tmp values is a current kernel filled with averaged colors.
- kernel_tmp = averageKernelRGB(pixels, x_curr, x_upper, y_curr, y_upper)
- # --------------------------------------
- # Each time i and j loops are executed, one kernel_tmp is
- # filled and ready for use.
- kernels[kernel_xpos, kernel_ypos] = kernel_tmp
- kernel_ypos += 1
- kernel_xpos += 1
- # We need to reset it to count from the start
- kernel_ypos = 0
- print('X_POS = ' + str(kernel_xpos) + ' Y_POS = ' + str(kernel_ypos))
- img.show()
- # print(kernel_tmp)
- # print('x = ' + str(x_curr) + ' y = ' + str(y_curr))
- #x_curr = x_curr + k_width
- # print(kernels[56,34])
- # Prints out keys. Debug purposes.
- # for key in kernels.keys():
- # print(key)
- # print(kernels[16,54])
- # print(kernels[16,54][2,4])
- # print(kernels[16,54][2,4][2])
- # Lets save the code below because its part of my first Python script ever...
- # Lets save the code below because its part of my first Python script ever...
- # # Lets calculate sum of all R, G and B components in all kernel-captured pixels
- # k_R, k_G, k_B = 0, 0, 0
- # for i in range(0, k_width):
- # for j in range (0, k_height):
- # # k_R += kernel_tmp[i,j][0]
- # # k_G += kernel_tmp[i,j][1]
- # # k_B += kernel_tmp[i,j][2]
- # k_R += kernels[50,63][i,j][0]
- # k_G += kernels[50,63][i,j][1]
- # k_B += kernels[50,63][i,j][2]
- #
- # # Hehe, nisi dovoljno mocan ako ne mozes ovo ha ha ha
- # # k_R, k_G, k_B /= 32, 32, 32
- # k_R /= k_width * k_height
- # k_G /= k_width * k_height
- # k_B /= k_width * k_height
- # print('Srednja vrijednost: R = ' + str(k_R) + ' G = ' + str(k_G) + ' B = ' + str(k_B))
- #
- # # Start all over
- # x_curr, y_curr = 0, 0
- # for i in range(x_curr, x_curr + k_width):
- # for j in range (y_curr, y_curr + k_height):
- # pixels[i, j] = (k_R, k_G, k_B)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement