diff options
| author | Tim Keller <tjkeller.xyz> | 2025-05-09 21:58:54 -0500 |
|---|---|---|
| committer | Tim Keller <tjkeller.xyz> | 2025-05-09 21:58:54 -0500 |
| commit | 145de3311cb9f32ac0bc9842b0600fcad84fed16 (patch) | |
| tree | 2822a41929eac3758d0ffe7a7d1b8cf49158a09e /texture.py | |
| parent | b487f62ba7cd7dbbcadb2b5704ec1c99f6578390 (diff) | |
| download | immich-frame-145de3311cb9f32ac0bc9842b0600fcad84fed16.tar.xz immich-frame-145de3311cb9f32ac0bc9842b0600fcad84fed16.zip | |
add support for downloading new images from immich api on a separate thread
Diffstat (limited to 'texture.py')
| -rw-r--r-- | texture.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/texture.py b/texture.py new file mode 100644 index 0000000..cf37d6a --- /dev/null +++ b/texture.py @@ -0,0 +1,43 @@ +from OpenGL.GL import * +from OpenGL.GLUT import * +from OpenGL.GLU import * +from PIL import Image, ExifTags + +class ImageTexture: + def __init__(self, image_source, exif=None): + self.id = None + img = Image.open(image_source) + self.exif = exif or ImageTexture.get_exif(img) + img = ImageTexture.handle_orientation(img, exif) + img = img.convert("RGBA") # Ensure the image is in RGBA mode + self.width = img.width + self.height = img.height + self._img_data = img.tobytes("raw", "RGBA", 0, -1) # Convert image data to bytes + + def gl_init(self): + if self.id: + return + #glPixelStorei(GL_UNPACK_ALIGNMENT, 1) + self.id = glGenTextures(1) + glBindTexture(GL_TEXTURE_2D, self.id) + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, self.width, self.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, self._img_data) + #glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img.width, img.height, 0, GL_RGB, GL_UNSIGNED_BYTE, img_data) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) + del self._img_data # No longer needed, clear mem + + print(f"Loaded texture {self.id}") + + @staticmethod + def get_exif(img): + return { ExifTags.TAGS[k]: v for k, v in img.getexif().items() if k in ExifTags } + + @staticmethod + def handle_orientation(img, exif): + orientation = exif.get("Orientation", 1) + + if orientation == 3: return img.rotate(180, expand=True) + if orientation == 6: return img.rotate(270, expand=True) + if orientation == 8: return img.rotate( 90, expand=True) + + return img |
