|
28 | 28 | from PIL.Image import DecompressionBombError |
29 | 29 | from pillow_heif import register_avif_opener, register_heif_opener |
30 | 30 | from pydub import exceptions |
31 | | -from PySide6.QtCore import QObject, QSize, Qt, Signal |
32 | | -from PySide6.QtGui import QGuiApplication, QPixmap |
| 31 | +from PySide6.QtCore import QBuffer, QObject, QSize, Qt, Signal |
| 32 | +from PySide6.QtGui import QGuiApplication, QImage, QPainter, QPixmap |
| 33 | +from PySide6.QtSvg import QSvgRenderer |
33 | 34 | from src.core.constants import FONT_SAMPLE_SIZES, FONT_SAMPLE_TEXT |
34 | 35 | from src.core.media_types import MediaCategories, MediaType |
35 | 36 | from src.core.palette import ColorType, UiColor, get_ui_color |
@@ -750,8 +751,33 @@ def _image_vector_thumb(self, filepath: Path, size: int) -> Image.Image: |
750 | 751 | filepath (Path): The path of the file. |
751 | 752 | size (tuple[int,int]): The size of the thumbnail. |
752 | 753 | """ |
753 | | - # TODO: Implement. |
754 | 754 | im: Image.Image = None |
| 755 | + # Create an image to draw the svg to and a painter to do the drawing |
| 756 | + image: QImage = QImage(size, size, QImage.Format.Format_ARGB32) |
| 757 | + image.fill("#1e1e1e") |
| 758 | + |
| 759 | + # Create an svg renderer, then render to the painter |
| 760 | + svg: QSvgRenderer = QSvgRenderer(str(filepath)) |
| 761 | + |
| 762 | + if not svg.isValid(): |
| 763 | + raise UnidentifiedImageError |
| 764 | + |
| 765 | + painter: QPainter = QPainter(image) |
| 766 | + svg.setAspectRatioMode(Qt.AspectRatioMode.KeepAspectRatio) |
| 767 | + svg.render(painter) |
| 768 | + painter.end() |
| 769 | + |
| 770 | + # Write the image to a buffer as png |
| 771 | + buffer: QBuffer = QBuffer() |
| 772 | + buffer.open(QBuffer.OpenModeFlag.ReadWrite) |
| 773 | + image.save(buffer, "PNG") |
| 774 | + |
| 775 | + # Load the image from the buffer |
| 776 | + im = Image.new("RGB", (size, size), color="#1e1e1e") |
| 777 | + im.paste(Image.open(BytesIO(buffer.data().data()))) |
| 778 | + im = im.convert(mode="RGB") |
| 779 | + |
| 780 | + buffer.close() |
755 | 781 | return im |
756 | 782 |
|
757 | 783 | def _model_stl_thumb(self, filepath: Path, size: int) -> Image.Image: |
@@ -924,6 +950,7 @@ def render( |
924 | 950 | ext, MediaCategories.IMAGE_RAW_TYPES, mime_fallback=True |
925 | 951 | ): |
926 | 952 | image = self._image_raw_thumb(_filepath) |
| 953 | + # Vector Images -------------------------------------------- |
927 | 954 | elif MediaCategories.is_ext_in_category( |
928 | 955 | ext, MediaCategories.IMAGE_VECTOR_TYPES, mime_fallback=True |
929 | 956 | ): |
|
0 commit comments