Half opacity of image

I’m making a color by number game and have a grayscale image I want to use as a guide in my game, when a pixel is colored, that pixel will also be colored on the grayscale image in it’s own color.

Now it’s ugly to have the mage at full opacity, so i would like to save it at half

func _to_grayscale(input):
----for x in input.get_size().x:
--------for y in input.get_size().y:
------------var current_pixel = input.get_pixel(x, y)
------------if(current_pixel.a == 1):
----------------var new_color = Color.from_hsv(0, 0, current_pixel.v * 2)
----------------input.set_pixel(x, y, new_color)

This is the code that grayscales my input, how do I make it so that when a pixel is grayed, it’s opacity is set to 0.5?
When it’s saved it does not matter if it isn’t opague anymore, I just need half the value of the grayness

Color.from_hsv() has a fourth argument for an alpha value. It’s 1.0 by default but you should be able to alter opacity by changing it?

var new_color = Color.from_hsv(0, 0, current_pixel.v * 2, 0.5)

1 Like

I tried this, but after saving it (as a png ) it resulted in the same image, the second one should have the gray values be half as dark, which is why i tried to do current_pixel.v * 2, but that resulted in a way to bright image and also didn’t work

May need to convert to a format that has an alpha channel first, or else the alpha values will be ignored. Try calling input.convert() before your loop:

	input.convert(Image.Format.FORMAT_RGBA8)
	
	print(input.data['format'])
	for x in input.get_size().x:
		for y in input.get_size().y:
			var cp := input.get_pixel(x, y)
			if(cp.a == 1):
				var new_color := Color.from_hsv(0, 0, cp.v * 2, 0.5)
				input.set_pixel(x, y, new_color)

this gives a better result thanks

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.