Fix next_level image glitch: reset camera scroll and improve image color quantization
This commit is contained in:
@@ -1,26 +1,34 @@
|
||||
from PIL import Image
|
||||
import numpy as np
|
||||
from scipy.cluster.vq import kmeans, vq
|
||||
|
||||
img = Image.open('assets/next_level.png').convert('L') # Convert to grayscale
|
||||
img = img.quantize(colors=4) # Quantize to 4 colors
|
||||
img = img.convert('RGB')
|
||||
# Force colors to exactly GB shades
|
||||
palette = [(224, 248, 208), (136, 192, 112), (52, 104, 86), (8, 24, 32)]
|
||||
# Actually the game uses generic colors or grayscale. Let's use 4 grayscale values:
|
||||
gb_palette = [(255,255,255), (170,170,170), (85,85,85), (0,0,0)]
|
||||
# Open original and convert to grayscale
|
||||
img = Image.open('assets/next_level_original.png').convert('L')
|
||||
pixels = np.array(img, dtype=float)
|
||||
|
||||
new_img = Image.new('RGB', img.size)
|
||||
for y in range(img.height):
|
||||
for x in range(img.width):
|
||||
p = img.getpixel((x, y))
|
||||
# Find closest color in gb_palette
|
||||
best_c = gb_palette[0]
|
||||
min_dist = 1000000
|
||||
for c in gb_palette:
|
||||
dist = (p[0]-c[0])**2 + (p[1]-c[1])**2 + (p[2]-c[2])**2
|
||||
if dist < min_dist:
|
||||
min_dist = dist
|
||||
best_c = c
|
||||
new_img.putpixel((x, y), best_c)
|
||||
# Flatten for clustering
|
||||
flat_pixels = pixels.flatten()
|
||||
|
||||
# Find 4 clusters
|
||||
centroids, _ = kmeans(flat_pixels, 4)
|
||||
|
||||
# Sort centroids by brightness (dark to light)
|
||||
sorted_centroids = np.sort(centroids)
|
||||
|
||||
# Map centroids to GB colors
|
||||
# GB palette: 0=Black, 1=Dark Gray, 2=Light Gray, 3=White
|
||||
gb_colors = np.array([0, 85, 170, 255])
|
||||
|
||||
# Find the closest centroid for each pixel
|
||||
# Assign the corresponding GB color
|
||||
quantized_pixels = np.zeros_like(flat_pixels, dtype=np.uint8)
|
||||
for i, p in enumerate(flat_pixels):
|
||||
dist = np.abs(sorted_centroids - p)
|
||||
closest_idx = np.argmin(dist)
|
||||
quantized_pixels[i] = gb_colors[closest_idx]
|
||||
|
||||
# Reshape back to image
|
||||
quantized_img_data = quantized_pixels.reshape(pixels.shape)
|
||||
new_img = Image.fromarray(quantized_img_data, mode='L').convert('RGB')
|
||||
new_img.save('assets/next_level.png')
|
||||
print("Processed next_level.png")
|
||||
print("Processed next_level.png with k-means clustering!")
|
||||
|
||||
Reference in New Issue
Block a user