summaryrefslogtreecommitdiff
path: root/texture.py
diff options
context:
space:
mode:
authorTim Keller <tjk@tjkeller.xyz>2025-06-24 19:26:15 -0500
committerTim Keller <tjk@tjkeller.xyz>2025-06-24 19:26:15 -0500
commit4be4fd53b3f71d1da55fc7f4c31640bf6f1c4992 (patch)
tree2a930728b4d5d81820211bd7790d9684f0a9f252 /texture.py
parentffa5ff333eabffe07394fb21bc413d7d238ee651 (diff)
parent4c3d572eb850c32a45ec9cbaf82688d45c1eebf4 (diff)
downloadimmich-frame-4be4fd53b3f71d1da55fc7f4c31640bf6f1c4992.tar.xz
immich-frame-4be4fd53b3f71d1da55fc7f4c31640bf6f1c4992.zip
merge pixpy repo into this repo
Diffstat (limited to 'texture.py')
-rw-r--r--texture.py67
1 files changed, 67 insertions, 0 deletions
diff --git a/texture.py b/texture.py
new file mode 100644
index 0000000..3a56b5f
--- /dev/null
+++ b/texture.py
@@ -0,0 +1,67 @@
+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 self.get_exif(img)
+ img = self.handle_orientation(img, self.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
+ self.initialized = True
+
+ 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}")
+
+ def free(self):
+ if self.id is None:
+ del self._img_data
+ else:
+ glBindTexture(GL_TEXTURE_2D, 0) # Unbind
+ glDeleteTextures([self.id])
+
+ @staticmethod
+ def get_exif(img):
+ return { ExifTags.TAGS[k]: v for k, v in img.getexif().items() if k in ExifTags.TAGS }
+
+ @staticmethod
+ def handle_orientation(img, exif):
+ orientation = exif.get("Orientation", 1) if exif is not None else 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
+
+
+class ImageTextureImmichAsset(ImageTexture):
+ def __init__(self, asset, asset_index):
+ self.initialized = False
+ self.asset_index = asset_index
+ self.asset_key = asset["id"]
+ self.exif = asset.get("exif", None)
+
+ # So that free can work
+ self.id = None
+ self._img_data = None
+
+ def initialize(self, image_source):
+ super().__init__(image_source, self.exif)