|
1 | 1 | # Copyright (C) 2024 Travis Abendshien (CyanVoxel). |
2 | 2 | # Licensed under the GPL-3.0 License. |
3 | 3 | # Created for TagStudio: https://github.com/CyanVoxel/TagStudio |
| 4 | + |
| 5 | +import io |
4 | 6 | import os |
5 | 7 | import platform |
6 | 8 | import sys |
@@ -601,19 +603,32 @@ def update_widgets(self) -> bool: |
601 | 603 | # TODO: Do this all somewhere else, this is just here temporarily. |
602 | 604 | ext: str = filepath.suffix.lower() |
603 | 605 | try: |
604 | | - if filepath.suffix.lower() in [".gif"]: |
605 | | - with open(filepath, mode="rb") as file: |
606 | | - if self.preview_gif.movie(): |
607 | | - self.preview_gif.movie().stop() |
608 | | - self.gif_buffer.close() |
609 | | - |
610 | | - ba = file.read() |
611 | | - self.gif_buffer.setData(ba) |
612 | | - movie = QMovie(self.gif_buffer, QByteArray()) |
613 | | - self.preview_gif.setMovie(movie) |
614 | | - movie.start() |
| 606 | + if MediaCategories.is_ext_in_category( |
| 607 | + ext, MediaCategories.IMAGE_ANIMATED_TYPES, mime_fallback=True |
| 608 | + ): |
| 609 | + if self.preview_gif.movie(): |
| 610 | + self.preview_gif.movie().stop() |
| 611 | + self.gif_buffer.close() |
| 612 | + |
| 613 | + image: Image.Image = Image.open(filepath) |
| 614 | + anim_image: Image.Image = image |
| 615 | + image_bytes_io: io.BytesIO = io.BytesIO() |
| 616 | + anim_image.save( |
| 617 | + image_bytes_io, |
| 618 | + "GIF", |
| 619 | + lossless=True, |
| 620 | + save_all=True, |
| 621 | + loop=0, |
| 622 | + disposal=2, |
| 623 | + ) |
| 624 | + image_bytes_io.seek(0) |
| 625 | + ba: bytes = image_bytes_io.read() |
| 626 | + |
| 627 | + self.gif_buffer.setData(ba) |
| 628 | + movie = QMovie(self.gif_buffer, QByteArray()) |
| 629 | + self.preview_gif.setMovie(movie) |
| 630 | + movie.start() |
615 | 631 |
|
616 | | - image = Image.open(str(filepath)) |
617 | 632 | self.resizeEvent( |
618 | 633 | QResizeEvent( |
619 | 634 | QSize(image.width, image.height), |
|
0 commit comments