Here are 4 additional unique downscaling methods that might produce interesting pixel art-like results:
python Copy code
def downscale_median(img, canvas_size):
"""
Uses median value in each region instead of mean.
Good for reducing noise and preserving sharp edges.
"""
height, width, _ = img.shape
canvas_width, canvas_height = canvas_size
scale_h = height / canvas_height
scale_w = width / canvas_width
downscaled = np.zeros((canvas_height, canvas_width, 3), dtype=np.uint8)
for i in range(canvas_height):
for j in range(canvas_width):
x_start = int(i * scale_h)
y_start = int(j * scale_w)
x_end = int((i + 1) * scale_h)
y_end = int((j + 1) * scale_w)
region = img[x_start:x_end, y_start:y_end]
downscaled[i, j] = np.median(region.reshape(-1, 3), axis=0).astype(np.uint8)
return downscaled
def downscale_max_color(img, canvas_size):
"""
Takes the most frequent color in each region.
Creates a more quantized, blocky pixel art look.
"""
height, width, _ = img.shape
canvas_width, canvas_height = canvas_size
scale_h = height / canvas_height
scale_w = width / canvas_width
downscaled = np.zeros((canvas_height, canvas_width, 3), dtype=np.uint8)
for i in range(canvas_height):
for j in range(canvas_width):
x_start = int(i * scale_h)
y_start = int(j * scale_w)
x_end = int((i + 1) * scale_h)
y_end = int((j + 1) * scale_w)
region = img[x_start:x_end, y_start:y_end]
# Reshape region and find most common color
reshaped = region.reshape(-1, 3)
unique, counts = np.unique(reshaped, axis=0, return_counts=True)
downscaled[i, j] = unique[np.argmax(counts)]
return downscaled
def downscale_random_sample(img, canvas_size, seed=42):
"""
Randomly samples a pixel from each region.
Creates a more randomized pixel art effect.
"""
np.random.seed(seed)
height, width, _ = img.shape
canvas_width, canvas_height = canvas_size
scale_h = height / canvas_height
scale_w = width / canvas_width
downscaled = np.zeros((canvas_height, canvas_width, 3), dtype=np.uint8)
for i in range(canvas_height):
for j in range(canvas_width):
x_start = int(i * scale_h)
y_start = int(j * scale_w)
x_end = int((i + 1) * scale_h)
y_end = int((j + 1) * scale_w)
region = img[x_start:x_end, y_start:y_end]
# Randomly select a pixel from the region
if region.size > 0:
flat_region = region.reshape(-1, 3)
random_index = np.random.randint(len(flat_region))
downscaled[i, j] = flat_region[random_index]
return downscaled
def downscale_weighted_average(img, canvas_size):
"""
Applies a weighted average where central pixels have more influence.
Creates a soft pixel art look with center-weighted smoothing.
"""
height, width, _ = img.shape
canvas_width, canvas_height = canvas_size
scale_h = height / canvas_height
scale_w = width / canvas_width
downscaled = np.zeros((canvas_height, canvas_width, 3), dtype=np.uint8)
for i in range(canvas_height):
for j in range(canvas_width):
x_start = int(i * scale_h)
y_start = int(j * scale_w)
x_end = int((i + 1) * scale_h)
y_end = int((j + 1) * scale_w)
region = img[x_start:x_end, y_start:y_end]
# Create a weighted kernel
h, w, _ = region.shape
y, x = np.ogrid[:h, :w]
center_y, center_x = h // 2, w // 2
weights = 1 - (((y - center_y)**2 + (x - center_x)**2) / (h*w))
weights = weights.reshape(h, w, 1)
# Apply weighted average
weighted_region = region * weights
downscaled[i, j] = np.sum(weighted_region, axis=(0,1)) / np.sum(weights)
return downscaled.astype(np.uint8)
python Copy code
# Assuming img_array is your original image
canvas_size = (64, 64)
# Create subplots to compare methods
fig, axes = plt.subplots(2, 3, figsize=(15, 10))
axes = axes.flatten()
# Original image
axes[0].imshow(img_array)
axes[0].set_title("Original Image")
# Previous methods
axes[1].imshow(downscale_nearest_neighbor(img_array, canvas_size))
axes[1].set_title("Nearest Neighbor")
# New methods
axes[2].imshow(downscale_median(img_array, canvas_size))
axes[2].set_title("Median")
axes[3].imshow(downscale_max_color(img_array, canvas_size))
axes[3].set_title("Max Color")
axes[4].imshow(downscale_random_sample(img_array, canvas_size))
axes[4].set_title("Random Sample")
axes[5].imshow(downscale_weighted_average(img_array, canvas_size))
axes[5].set_title("Weighted Average")
for ax in axes:
ax.axis('off')
plt.tight_layout()
plt.show()
Each method offers a unique approach to pixel art downscaling. Experiment with them to find the style that best suits your artistic vision!