require 'imlib2'

text = Imlib2::Image.load('text.png')

text.save("text_unchanged.png")

width = text.width
height = text.height
new_width = width / 2
new_height = height / 2

text_scaled = text.crop_scaled(0, 0, width, height, new_width, new_height)
text_scaled.save("text_scaled.png")

background = Imlib2::Image.new(width, height)
color = Imlib2::Color::RgbaColor.new(0xa4, 0xb4, 0xd8, 0xff)
rect = [0, 0, width, height]
background.fill_rect rect, color

src = [0, 0, width, height]
dst = src
text_with_background = background.blend text, src, dst, merge_alpha = true
text_with_background.save("text_with_background.png")

text_with_background_scaled = text_with_background.crop_scaled(0, 0, width, height, new_width, new_height)
text_with_background_scaled.save("text_with_background_scaled.png")

background_scaled = background.crop_scaled(0, 0, width, height, new_width, new_height)
text_scaled_with_background = background_scaled.blend text_scaled, src, dst, merge_alpha = true
text_scaled_with_background.save("text_scaled_with_background.png")
