Skip to main content

python wrapper for etcpak

Project description

etcpak

PyPI supported Python versions Win/Mac/Linux MIT Build Status

A python wrapper for wolfpld/etcpak

Some changes were made to the original code to make it cross-platform compatible.

  1. Installation
  2. Example
  3. Functions

Installation

pip install etcpak

or download/clone the git and use

python setup.py install

Example

from PIL import Image
import etcpak

# load image
img = Image.open(file_path)

# get image data
img_data = img.convert("RGBA").tobytes()

# compress data
compressed = etcpak.compress_to_dxt5(img_data, img.width, img.height)

composite image for format comparission

import os
import etcpak
import texture2ddecoder
from PIL import Image

FORMATS = [
    ("DXT1", etcpak.compress_to_dxt1, texture2ddecoder.decode_bc1),
    ("DXT1 Dither", etcpak.compress_to_dxt1_dither, texture2ddecoder.decode_bc1),
    ("DXT5", etcpak.compress_to_dxt5, texture2ddecoder.decode_bc3),
    ("ETC1", etcpak.compress_to_etc1, texture2ddecoder.decode_etc1),
    ("ETC1 Dither", etcpak.compress_to_etc1_dither, texture2ddecoder.decode_etc1),
    ("ETC2 RGB", etcpak.compress_to_etc2_rgb, texture2ddecoder.decode_etc2),
    ("ETC2 RGBA", etcpak.compress_to_etc2_rgba, texture2ddecoder.decode_etc2a8)
]

p = "S:\\Pictures"
for fp in os.listdir(p):
    if not fp[-4:] in [".png", ".jpg", ".bmp", "jpeg"]:
        continue
    # load image and adjust format and size
    print(fp)
    img = Image.open(os.path.join(p, fp)).convert("RGBA")
    img = img.crop((0,0,img.width-img.width%4, img.height-img.height%4))
    
    # create composite image
    comp = Image.new("RGBA", (img.width*8, img.height))
    comp.paste(img, (0, 0))
    print(img.width * img.height * 4)

    # iterate over all formats
    for i, (name, enc, dec) in enumerate(FORMATS):
        print(name)
        # make sure that the channel order is correct for the compression
        if name[:3] == "DXT":
            raw = img.tobytes()
        elif name[:3] == "ETC":
            r,g,b,a = img.split()
            raw = Image.merge('RGBA', (b,g,r,a)).tobytes()
        
        # compress
        data = enc(raw, img.width, img.height)

        # decompress
        dimg = Image.frombytes("RGBA", img.size, dec(data, img.width, img.height), "raw", "BGRA")

        # add to composite image
        comp.paste(dimg, (img.width*(i+1), 0))

    # save composite image
    comp.save(os.path.splitext(fp)[0]+".png")

Functions

  • all functions accept only arguments, no keywords
  • the data has to be RGBA/BGRA for the RGB functions as well
  • all DXT compressions require data in the RGBA format
  • all ETC compressions require data in the BGRA format

compress_to_dxt1

Compresses RGBA to DXT1

:param data: RGBA data of the image
:type data: bytes
:param width: width of the image
:type width: int
:param height: height of the image
:type height: int
:returns: compressed data
:rtype: bytes"

compress_to_dxt1_dither

Compresses RGBA to DXT1 Dither

:param data: RGBA data of the image
:type data: bytes
:param width: width of the image
:type width: int
:param height: height of the image
:type height: int
:returns: compressed data
:rtype: bytes"

compress_to_dxt5

Compresses RGBA to DXT5

:param data: RGBA data of the image
:type data: bytes
:param width: width of the image
:type width: int
:param height: height of the image
:type height: int
:returns: compressed data
:rtype: bytes"

compress_to_etc1

Compresses RGBA to ETC1 RGB

:param data: RGBA data of the image
:type data: bytes
:param width: width of the image
:type width: int
:param height: height of the image
:type height: int
:returns: compressed data
:rtype: bytes"

compress_to_etc1_dither

Compresses RGBA to ETC1 Dither

:param data: RGBA data of the image
:type data: bytes
:param width: width of the image
:type width: int
:param height: height of the image
:type height: int
:returns: compressed data
:rtype: bytes"

compress_to_etc1_alpha

Compresses A to ETC1 Alpha

:param data: RGBA data of the image
:type data: bytes
:param width: width of the image
:type width: int
:param height: height of the image
:type height: int
:returns: compressed data
:rtype: bytes"

compress_to_etc2_rgb

Compresses RGBA to ETC2 RGB

:param data: RGBA data of the image
:type data: bytes
:param width: width of the image
:type width: int
:param height: height of the image
:type height: int
:returns: compressed data
:rtype: bytes"

compress_to_etc2_rgba

Compresses RGBA to ETC2 RGBA

:param data: RGBA data of the image
:type data: bytes
:param width: width of the image
:type width: int
:param height: height of the image
:type height: int
:returns: compressed data
:rtype: bytes"

compress_to_etc2_alpha

Compresses RGBA to ETC2 Alpha

:param data: RGBA data of the image
:type data: bytes
:param width: width of the image
:type width: int
:param height: height of the image
:type height: int
:returns: compressed data
:rtype: bytes"

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

etcpak-0.9.8.tar.gz (56.6 kB view hashes)

Uploaded Source

Built Distributions

etcpak-0.9.8-cp312-cp312-win_amd64.whl (35.3 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

etcpak-0.9.8-cp312-cp312-win32.whl (31.6 kB view hashes)

Uploaded CPython 3.12 Windows x86

etcpak-0.9.8-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (313.8 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

etcpak-0.9.8-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (280.6 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

etcpak-0.9.8-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (270.0 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

etcpak-0.9.8-cp312-cp312-macosx_11_0_arm64.whl (45.6 kB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

etcpak-0.9.8-cp312-cp312-macosx_10_9_x86_64.whl (62.9 kB view hashes)

Uploaded CPython 3.12 macOS 10.9+ x86-64

etcpak-0.9.8-cp312-cp312-macosx_10_9_universal2.whl (105.3 kB view hashes)

Uploaded CPython 3.12 macOS 10.9+ universal2 (ARM64, x86-64)

etcpak-0.9.8-cp311-cp311-win_amd64.whl (35.2 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

etcpak-0.9.8-cp311-cp311-win32.whl (31.6 kB view hashes)

Uploaded CPython 3.11 Windows x86

etcpak-0.9.8-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (313.8 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

etcpak-0.9.8-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (280.6 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

etcpak-0.9.8-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (270.0 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

etcpak-0.9.8-cp311-cp311-macosx_11_0_arm64.whl (45.6 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

etcpak-0.9.8-cp311-cp311-macosx_10_9_x86_64.whl (63.0 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

etcpak-0.9.8-cp311-cp311-macosx_10_9_universal2.whl (105.4 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64)

etcpak-0.9.8-cp310-cp310-win_amd64.whl (35.2 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

etcpak-0.9.8-cp310-cp310-win32.whl (31.6 kB view hashes)

Uploaded CPython 3.10 Windows x86

etcpak-0.9.8-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (313.8 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

etcpak-0.9.8-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (280.6 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

etcpak-0.9.8-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (270.1 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

etcpak-0.9.8-cp310-cp310-macosx_11_0_arm64.whl (45.6 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

etcpak-0.9.8-cp310-cp310-macosx_10_9_x86_64.whl (63.0 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

etcpak-0.9.8-cp310-cp310-macosx_10_9_universal2.whl (105.4 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64)

etcpak-0.9.8-cp39-cp39-win_amd64.whl (35.2 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

etcpak-0.9.8-cp39-cp39-win32.whl (31.6 kB view hashes)

Uploaded CPython 3.9 Windows x86

etcpak-0.9.8-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (313.6 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

etcpak-0.9.8-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (280.4 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

etcpak-0.9.8-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (269.8 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

etcpak-0.9.8-cp39-cp39-macosx_11_0_arm64.whl (45.6 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

etcpak-0.9.8-cp39-cp39-macosx_10_9_x86_64.whl (63.0 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

etcpak-0.9.8-cp39-cp39-macosx_10_9_universal2.whl (105.4 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64)

etcpak-0.9.8-cp38-cp38-win_amd64.whl (35.2 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

etcpak-0.9.8-cp38-cp38-win32.whl (31.6 kB view hashes)

Uploaded CPython 3.8 Windows x86

etcpak-0.9.8-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (313.6 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

etcpak-0.9.8-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (280.4 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

etcpak-0.9.8-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (269.8 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

etcpak-0.9.8-cp38-cp38-macosx_11_0_arm64.whl (45.6 kB view hashes)

Uploaded CPython 3.8 macOS 11.0+ ARM64

etcpak-0.9.8-cp38-cp38-macosx_10_9_x86_64.whl (63.0 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

etcpak-0.9.8-cp38-cp38-macosx_10_9_universal2.whl (105.4 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ universal2 (ARM64, x86-64)

etcpak-0.9.8-cp37-cp37m-win_amd64.whl (35.2 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

etcpak-0.9.8-cp37-cp37m-win32.whl (31.6 kB view hashes)

Uploaded CPython 3.7m Windows x86

etcpak-0.9.8-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (313.5 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ ARM64

etcpak-0.9.8-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (280.3 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

etcpak-0.9.8-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (269.7 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

etcpak-0.9.8-cp37-cp37m-macosx_10_9_x86_64.whl (62.9 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page