I initially created a subarray from the initial array for a greyscale image from this: Deleting consecutive numbers from a numpy array and Remove following duplicates in a numpy array
But now I want to do the same for a coloured image and I'm really confused. I've been working on it for days and simply cannot make sense of how I can approach it.
The problem is the squares are different sizes and I want a pixel for each square represented with the same colour.
Coloured image:
My code for greyscale image:
from PIL import Image import numpy as np name1 = raw_input("What is the name of the .png file you want to open? ") filename1 = "%s.png" % name1 img = Image.open(filename1).convert('L') # convert image to 8-bit grayscale WIDTH, HEIGHT = img.size a = list(img.getdata()) # convert image data to a list of integers # convert that to 2D list (list of lists of integers) a = np.array ([a[offset:offset+WIDTH] for offset in range(0, WIDTH*HEIGHT, WIDTH)]) print " " print "Intial array from image:" #print as array print " " print a rows_mask = np.insert(np.diff(a[:, 0]).astype(np.bool), 0, True) columns_mask = np.insert(np.diff(a[0]).astype(np.bool), 0, True) b = a[np.ix_(rows_mask, columns_mask)] print " " print "Subarray from Image:" #print as array print " " print b #img = Image.fromarray(b, mode='L') print " " print "Subarray from Image (clearer format):" #print as array print " " for row in b: #print as a table like format print(' '.join('{:3}'.format(value) for value in row)) #img.save("chocolate.png") #print np.mean(b) #finding mean
For example for this image:
Input array example:
From a = list(img.getdata())
, this is the input I get from the image.
[(115, 45, 135), (115, 45, 135), (115, 45, 135), (115, 45, 135), (115, 45, 135), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (115, 45, 135), (115, 45, 135), (115, 45, 135), (115, 45, 135), (115, 45, 135), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (115, 45, 135), (115, 45, 135), (115, 45, 135), (115, 45, 135), (115, 45, 135), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (115, 45, 135), (115, 45, 135), (115, 45, 135), (115, 45, 135), (115, 45, 135), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (115, 45, 135), (115, 45, 135), (115, 45, 135), (115, 45, 135), (115, 45, 135), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95)]
The numpy input using a = np.array ([a[offset:offset+WIDTH] for offset in range(0, WIDTH*HEIGHT, WIDTH)])
:
[[[115 45 135] [115 45 135] [115 45 135] [115 45 135] [115 45 135] [245 245 35] [245 245 35] [245 245 35] [245 245 35] [245 245 35] [245 245 35]] [[115 45 135] [115 45 135] [115 45 135] [115 45 135] [115 45 135] [245 245 35] [245 245 35] [245 245 35] [245 245 35] [245 245 35] [245 245 35]] [[115 45 135] [115 45 135] [115 45 135] [115 45 135] [115 45 135] [245 245 35] [245 245 35] [245 245 35] [245 245 35] [245 245 35] [245 245 35]] [[115 45 135] [115 45 135] [115 45 135] [115 45 135] [115 45 135] [245 245 35] [245 245 35] [245 245 35] [245 245 35] [245 245 35] [245 245 35]] [[115 45 135] [115 45 135] [115 45 135] [115 45 135] [115 45 135] [245 245 35] [245 245 35] [245 245 35] [245 245 35] [245 245 35] [245 245 35]] [[ 55 235 195] [ 55 235 195] [ 55 235 195] [ 55 235 195] [ 55 235 195] [245 245 95] [245 245 95] [245 245 95] [245 245 95] [245 245 95] [245 245 95]] [[ 55 235 195] [ 55 235 195] [ 55 235 195] [ 55 235 195] [ 55 235 195] [245 245 95] [245 245 95] [245 245 95] [245 245 95] [245 245 95] [245 245 95]] [[ 55 235 195] [ 55 235 195] [ 55 235 195] [ 55 235 195] [ 55 235 195] [245 245 95] [245 245 95] [245 245 95] [245 245 95] [245 245 95] [245 245 95]] [[ 55 235 195] [ 55 235 195] [ 55 235 195] [ 55 235 195] [ 55 235 195] [245 245 95] [245 245 95] [245 245 95] [245 245 95] [245 245 95] [245 245 95]] [[ 55 235 195] [ 55 235 195] [ 55 235 195] [ 55 235 195] [ 55 235 195] [245 245 95] [245 245 95] [245 245 95] [245 245 95] [245 245 95] [245 245 95]] [[ 55 235 195] [ 55 235 195] [ 55 235 195] [ 55 235 195] [ 55 235 195] [245 245 95] [245 245 95] [245 245 95] [245 245 95] [245 245 95] [245 245 95]]]
Output desired:
[[[115 45 135] [245 245 35]] [ 55 235 195] [245 245 95]]]