Aggiungi il supporto per la creazione di ombre in base ai canali alfa delle immagini
This commit is contained in:
@@ -70,30 +70,6 @@ class SDL2Wrapper(IsometricGeometry, SDL2Gui):
|
||||
dstrect=(x, y, srcrect[2], srcrect[3]), srcrect=srcrect)
|
||||
return (frame + 1) % total_frames
|
||||
|
||||
def make_non_transparent_pixels_black(self, surface):
|
||||
width = surface.w
|
||||
height = surface.h
|
||||
|
||||
# Blocca la surface per l'accesso diretto ai pixel
|
||||
sdl2.SDL_LockSurface(surface)
|
||||
|
||||
# Itera attraverso i pixel e modifica quelli non trasparenti
|
||||
u32_pixels = ctypes.cast(surface.pixels, ctypes.POINTER(ctypes.c_uint32))
|
||||
for y in range(height):
|
||||
for x in range(width):
|
||||
pixel = u32_pixels[y * width + x]
|
||||
if pixel & 0xFF000000 != 0:
|
||||
u32_pixels[y * width + x] = 0xFF000000
|
||||
# Sblocca la surface
|
||||
sdl2.SDL_UnlockSurface(surface)
|
||||
|
||||
# Crea una nuova texture dalla surface modificata
|
||||
new_texture = self.factory.from_surface(surface)
|
||||
|
||||
# Libera la surface temporanea
|
||||
sdl2.SDL_FreeSurface(surface)
|
||||
|
||||
return new_texture
|
||||
|
||||
def create_background(self, map, spritesheet_name):
|
||||
self.surface_width = round(self.cell_size * len(map[0]) * math.sqrt(2))
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 36 KiB |
@@ -0,0 +1,62 @@
|
||||
from PIL import ImageOps, Image, ImageFilter
|
||||
|
||||
FG_IMG_PATH = "fg.png"
|
||||
|
||||
BG_IMG_PATH = "bg.jpeg"
|
||||
|
||||
def load_image(path):
|
||||
|
||||
"""Load an image using PIL."""
|
||||
|
||||
return Image.open(path)
|
||||
|
||||
def extract_alpha(image):
|
||||
|
||||
"""Extract the alpha channel from an image."""
|
||||
|
||||
return image.split()[-1]
|
||||
|
||||
def create_shadow_from_alpha(alpha, blur_radius):
|
||||
|
||||
"""Create a shadow based on a blurred version of the alpha channel."""
|
||||
|
||||
alpha_blur = alpha.filter(ImageFilter.BoxBlur(blur_radius))
|
||||
|
||||
shadow = Image.new(mode="RGB", size=alpha_blur.size)
|
||||
|
||||
shadow.putalpha(alpha_blur)
|
||||
|
||||
return shadow
|
||||
|
||||
def composite_images(fg, shadow):
|
||||
|
||||
"""Composite the shadow and foreground onto the background."""
|
||||
|
||||
shadow.paste(fg, (-5, 4), fg)
|
||||
|
||||
return shadow
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
|
||||
# Load the images
|
||||
|
||||
fg = load_image(FG_IMG_PATH)
|
||||
|
||||
# Create the shadow based on the alpha channel of the foreground
|
||||
|
||||
alpha = extract_alpha(fg)
|
||||
|
||||
shadow = create_shadow_from_alpha(alpha, blur_radius=1)
|
||||
|
||||
# Composite the shadow and foreground onto the background
|
||||
|
||||
final_image = composite_images(fg, shadow)
|
||||
|
||||
# Display the final image (optional)
|
||||
|
||||
final_image.show()
|
||||
|
||||
# Save the final image
|
||||
|
||||
final_image.save(f"final_image.png")
|
||||
Reference in New Issue
Block a user