Skip to content

Commit c347354

Browse files
authored
* Fix K0lb3#151 * Update Texture2DConverter.py
1 parent a657c15 commit c347354

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

‎UnityPy/export/Texture2DConverter.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
7991
def get_image_from_texture2d(texture_2d, flip=True) -> Image.Image:
8092
"""converts the given texture into PIL.Image
8193

0 commit comments

Comments
 (0)