Skip to content

Instantly share code, notes, and snippets.

@oldlastman
Created June 21, 2024 12:10
Show Gist options
  • Save oldlastman/3e87a6c961cfe5e8ef84478234ee87f3 to your computer and use it in GitHub Desktop.
Save oldlastman/3e87a6c961cfe5e8ef84478234ee87f3 to your computer and use it in GitHub Desktop.
Convertir webp en svg estilo pixelart
from PIL import Image
import svgwrite
# Load the new image
print("Loading the new image (img.webp)...")
input_path_sol = "./img.webp"
# Open the image with PIL
image_sol = Image.open(input_path_sol).convert("RGBA")
# Resize the image to 64x64 pixels
resized_image_sol = image_sol.resize((64, 64), Image.NEAREST)
# Save the resized image
print("Saving the resized image...")
resized_image_sol_path = "./img-resized.png"
resized_image_sol.save(resized_image_sol_path)
# Display the resized image to the user
#display_image_to_user(resized_image_sol_path)
# Convert to SVG and save
output_path_sol_64 = "./convertida.svg"
dwg_sol_64 = svgwrite.Drawing(output_path_sol_64, size=(64, 64), profile='tiny')
# Create a group for the pixel art
g_sol_64 = dwg_sol_64.g(id='pixelart', transform='scale(1)')
print("Creating the SVG file...")
# Add rectangles for each pixel
for y in range(64):
for x in range(64):
color = resized_image_sol.getpixel((x, y))
if color[3] > 0: # Ignore transparent pixels
fill = svgwrite.rgb(color[0], color[1], color[2])
g_sol_64.add(dwg_sol_64.rect(insert=(x, y), size=(1, 1), fill=fill))
# Add the group to the SVG drawing
dwg_sol_64.add(g_sol_64)
# Save the SVG file
dwg_sol_64.save()
print("SVG file saved at:", output_path_sol_64)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment