USER
in this code first imshow and second imshow don't print same results even thought they should, can you spot the error: image = Image.open("generated_images/seed0000.png")
width, height = image.size
top = plt.colormaps['tab20'].resampled(20)
mid = plt.colormaps['Set1'].resampled(6)
bottom = plt.colormaps['Pastel1'].resampled(9)
newcolors = np.vstack((top(np.linspace(0, 1, 20)),
mid(np.linspace(0, 1, 6)),
bottom(np.linspace(0,1,9))))
newcmp = ListedColormap(newcolors, name='kaylacolors')
pixels = np.array(image.convert("RGB"))
colormap = newcmp.colors[:, :3] * 255
mapdata_img = Image.new('RGB', (23, 80))
for height in range (96,416,4):
for width in range(210,302,4):
vote_dict = {}
for y in range(height,height+4):
for x in range(width, width+4):
# Get the RGB values of the pixel at (x, y)
pixel_color = pixels[y, x]
closest_color_index = distance.cdist([pixel_color], colormap).argmin()
closest_color = colormap[closest_color_index]
closest_color_tuple = tuple(closest_color)
vote_dict[closest_color_tuple] = vote_dict.get(closest_color_tuple, 0) + 1
# Do something with the closest color
#print(f"Pixel at ({x}, {y}) - Closest color: {closest_color}")
max_voted_pixel = max(vote_dict, key=vote_dict.get)
mapdata_img.putpixel(((width-210)//4, (height-96)//4), (int(max_voted_pixel[0]), int(max_voted_pixel[1]), int(max_voted_pixel[2])))
plt.imshow(mapdata_img)
map_data = np.zeros((80,30), dtype=np.uint8)
mapdata_img_pix = np.array(mapdata_img.convert("RGB"), dtype=np.float64)
for i in range(23):
for j in range(80):
# get the value of the pixel from the numpy array
pixel_value = mapdata_img_pix[j, i]
# find the index of the value in the colormap
closest_color_index = distance.cdist([pixel_value], colormap).argmin()
# set the corresponding uint8 value in the uint8 array
map_data[j][i] = closest_color_index
np.set_printoptions(threshold=np.inf)
plt.imshow(map_data,cmap=newcmp)
plt.axis('off')
plt.show()
if False:
with open("my_array", "wb") as f:
# write the raw binary data
for row in mapdata_img_pix:
for value in row:
print(value)
# write a single byte to the file
f.write(struct.pack("<B", value))
# write a newline character to the file after each row
f.write(b"\n")
# Do something with the RGB values
# print(f"Pixel at ({x}, {y}) - R: {r}, G: {g}, B: {b}")ASSISTANT
Based on the code, the issue is with the size of the numpy array `map_data`.
In the line `map_data = np.zeros((80,30), dtype=np.uint8)`, the size of the array is defined as `(80,30)` which is different from the size of the image `(23,80)`. Therefore, when you try to display the image using `plt.imshow(map_data,cmap=newcmp)`, the dimensions of the array and the dimensions of the colormap do not match, leading to different results for the first `imshow` and the second `imshow`.
To fix the issue, you need to define the size of the numpy array `map_data` the same as the size of the image. Here's the corrected code:
```python
import numpy as np
from PIL import Image
from matplotlib import pyplot as plt
from matplotlib.colors import ListedColormap
from scipy.spatial import distance
import struct
image = Image.open("generated_images/seed0000.png")
width, height = image.size
top = plt.colormaps['tab20'].resampled(20)
mid = plt.colormaps['Set1'].resampled(6)
bottom = plt.colormaps['Pastel1'].resampled(9)
newcolors = np.vstack((top(np.linspace(0, 1, 20)),
mid(np.linspace(0, 1, 6)),
bottom(np.linspace(0,1,9))))
newcmp = ListedColormap(newcolors, name='kaylacolors')
pixels = np.array(image.convert("RGB"))
colormap = newcmp.colors[:, :3] * 255
mapdata_img = Image.new('RGB', (23, 80))
for height in range (96,416,4):
for width in range(210,302,4):
vote_dict = {}
for y in range(height,height+4):
for x in range(width, width+4):
# Get the RGB values of the pixel at (x, y)
pixel_color = pixels[y, x]
closest_color_index = distance.cdist([pixel_color], colormap).argmin()
closest_color = colormap[closest_color_index]
closest_color_tuple = tuple(closest_color)
vote_dict[closest_color_tuple] = vote_dict.get(closest_color_tuple, 0) + 1
# Do something with the closest color
#print(f"Pixel at ({x}, {y}) - Closest color: {closest_color}")
max_voted_pixel = max(vote_dict, key=vote_dict.get)
mapdata_img.putpixel(((width-210)//4, (height-96)//4), (int(max_voted_pixel[0]), int(max_voted_pixel[1]), int(max_voted_pixel[2])))
plt.imshow(mapdata_img)
map_data = np.zeros((80, 23), dtype=np.uint8) # Fix size of map_data
mapdata_img_pix = np.array(mapdata_img.convert("RGB"), dtype=np.float64)
for i in range(23):
for j in range(80):
# get the value of the pixel from the numpy array
pixel_value = mapdata_img_pix[j, i]
# find the index of the value in the colormap
closest_color_index = distance.cdist([pixel_value], colormap).argmin()
# set the corresponding uint8 value in the uint8 array
map_data[j][i] = closest_color_index
np.set_printoptions(threshold=np.inf)
plt.imshow(map_data,cmap=newcmp)
plt.axis('off')
plt.show()
if False:
with open("my_array", "wb") as f:
# write the raw binary data
for row in mapdata_img_pix:
for value in row:
print(value)
# write a single byte to the file
f.write(struct.pack("<B", value))
# write a newline character to the file after each row
f.write(b"\n")
```
In this corrected code, the `map_data` array is defined as `np.zeros((80,23), dtype=np.uint8)` to match the size of the image.