Guys I'm new to python and I'm stuck on this. Would really appreciate it if you could help me out.
I've got an image that has a number of colours in each pixel. They all denote a number.
The image was constructed using this range from 0 to 15625 pixels. Each of the pixels in the range from 0 to 15625 has a different colour and the above image was constructed using that.
image range (it's massive so you might need to download it to see the image)
What I'm trying to do is convert the RGB values from the range such as the first pixel value (5,5,5) to 1 and the next pixel value in the range to 2 and so on. Therefore, each pixel in the image above could correspond to a value.
It's similar to this question but I don't think it does what I want to do. How to Convert all pixel values of an image to a certain range -python
This is the code I used to create the range
#!/usr/local/bin/python3
from PIL import Image
import numpy as np
# Create array to hold output image
result=np.zeros([1,25*25*25,3],dtype=np.uint8)
#change the values above i.e. 51*51*51 done by (upperbound-lowerbound)/step i.e (255-0)/5
j=0
for r in range(5,255,10):
for g in range(5,255,10):
for b in range(5,255,10):
result[0,j]= (r,g,b)
j+=1
# Convert output array to image and save
im=Image.fromarray(result)
im.save("perfect1.png")
This is the code to find the RGB values of each pixel in the range
from PIL import Image
i = Image.open('perfect1.png')
pixels = i.load() # this is not a list, nor is it list()'able
width, height = i.size
all_pixels = []
for x in range(width):
for y in range(height):
cpixel = pixels[x, y]
all_pixels.append(cpixel)
print all_pixels
This is the code for creating a sub array with no extra pixel values as each "pixel" value in the image has a number of pixels enclosed. a = array of the image values
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)]