Charles is right; blit_into is just copying pixel values. If you
want blending you either have to figure it out yourself or let
OpenGL do it for you.
--
You received this message because you are subscribed to the Google Groups "pyglet-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyglet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyglet-users/bac6d6cb-c2fa-4f93-860d-a25534465655%40googlegroups.com.
import pyglet
import ctypes
image_one = pyglet.image.load("one.png")
image_two = pyglet.image.load("two.png")
window = pyglet.window.Window()
def blend_color(color_src, color_dst, src_factor, dst_factor):
# Blending Formula = source * src_factor + dst * dst_factor. Make sure they are int on return
return int((color_src * src_factor + color_dst * dst_factor) * 255)
def blend_alpha(alpha_src, alpha_dst, src_factor, dst_factor):
# Blend alpha, no source factor, can change if you want.
return int((alpha_src + alpha_dst * dst_factor) * 255)
def combine_pixels(source, dst):
""" source and dst are pixels of RGBA
This demonstarates formula for glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
"""
converted_src = [px / 255 for px in source] # From bytes to values in relation to 1.
converted_dst = [px / 255 for px in dst]
# blend mode
src_factor = converted_src[3] # Source Factor: Source alpha. AKA GL_SRC_ALPHA
dst_factor = (1 - converted_src[3]) # Dst Factor: 1 minus source alpha AKA GL_ONE_MINUS_SRC_ALPHA
r = blend_color(converted_src[0], converted_dst[0], src_factor, dst_factor)
g = blend_color(converted_src[1], converted_dst[1], src_factor, dst_factor)
b = blend_color(converted_src[2], converted_dst[2], src_factor, dst_factor)
a = blend_alpha(converted_src[3], converted_dst[3], src_factor, dst_factor)
return r, g, b, a
# Turn both images in a list of RGBA int values.
pixel_data_one = list(image_one.get_image_data().get_data('RGBA', image_one.width * 4))
pixel_data_two = list(image_two.get_image_data().get_data('RGBA', image_two.width * 4))
combined_image = []
for i in range(0, len(pixel_data_one), 4):
pixel_one = pixel_data_one[i:i + 4]
pixel_two = pixel_data_two[i:i + 4]
# Pixel_two (source) on top of pixel_one (destination)
combined_image.extend(combine_pixels(pixel_one, pixel_two))
data = (ctypes.c_ubyte * len(pixel_data_one))(*combined_image)
new_image = pyglet.image.ImageData(image_one.width, image_two.height, "RGBA", data)
new_image.save("combined_image.png")
# draw new image.
sprite = pyglet.sprite.Sprite(new_image, x=0, y=0)
@window.event
def on_draw():
window.clear()
sprite.draw()
pyglet.app.run()