@@ -24,11 +24,13 @@ def image_to_texture2d(img: Image.Image, target_texture_format: TF, flip: bool =
2424 tex_format = TF .DXT5
2525 # ETC
2626 elif target_texture_format in [TF .ETC_RGB4 , TF .ETC_RGB4Crunched , TF .ETC_RGB4_3DS ]:
27+ img = assert_rgba (img , target_texture_format )
2728 r , g , b , a = img .split ()
2829 raw_img = Image .merge ("RGBA" , (b , g , r , a )).tobytes ()
2930 enc_img = etcpak .compress_to_etc1 (raw_img , img .width , img .height )
3031 tex_format = TF .ETC_RGB4
3132 elif target_texture_format == TF .ETC2_RGB :
33+ img = assert_rgba (img , target_texture_format )
3234 r , g , b , a = img .split ()
3335 raw_img = Image .merge ("RGBA" , (b , g , r , a )).tobytes ()
3436 enc_img = etcpak .compress_to_etc2_rgb (raw_img , img .width , img .height )
@@ -37,6 +39,7 @@ def image_to_texture2d(img: Image.Image, target_texture_format: TF, flip: bool =
3739 target_texture_format in [TF .ETC2_RGBA8 , TF .ETC2_RGBA8Crunched , TF .ETC2_RGBA1 ]
3840 or "_RGB_" in target_texture_format .name
3941 ):
42+ img = assert_rgba (img , target_texture_format )
4043 r , g , b , a = img .split ()
4144 raw_img = Image .merge ("RGBA" , (b , g , r , a )).tobytes ()
4245 enc_img = etcpak .compress_to_etc2_rgba (raw_img , img .width , img .height )
@@ -76,6 +79,15 @@ def image_to_texture2d(img: Image.Image, target_texture_format: TF, flip: bool =
7679 return enc_img , tex_format
7780
7881
82+ def assert_rgba (img : Image .Image , target_texture_format : TextureFormat ):
83+ if img .mode == "RGB" :
84+ img = img .convert ("RGBA" )
85+ assert (
86+ img .mode == "RGBA"
87+ ), f"{ target_texture_format } compression only supports RGB & RGBA images" # noqa: E501
88+ return img
89+
90+
7991def get_image_from_texture2d (texture_2d , flip = True ) -> Image .Image :
8092 """converts the given texture into PIL.Image
8193
0 commit comments