Skip to content

Commit

Permalink
Merge branch 'main' into pdf-thumbs
Browse files Browse the repository at this point in the history
  • Loading branch information
CyanVoxel authored Oct 14, 2024
2 parents 45a12f2 + 9255a86 commit 5ca7a23
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
31 changes: 29 additions & 2 deletions tagstudio/src/qt/widgets/thumb_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@
Qt,
Signal,
)
from PySide6.QtGui import QGuiApplication, QImage, QPixmap
from PySide6.QtGui import QGuiApplication, QImage, QPainter, QPixmap
from PySide6.QtPdf import QPdfDocument, QPdfDocumentRenderOptions
from PySide6.QtSvg import QSvgRenderer
from src.core.constants import FONT_SAMPLE_SIZES, FONT_SAMPLE_TEXT
from src.core.media_types import MediaCategories, MediaType
from src.core.palette import ColorType, UiColor, get_ui_color
Expand Down Expand Up @@ -762,8 +763,33 @@ def _image_vector_thumb(self, filepath: Path, size: int) -> Image.Image:
filepath (Path): The path of the file.
size (tuple[int,int]): The size of the thumbnail.
"""
# TODO: Implement.
im: Image.Image = None
# Create an image to draw the svg to and a painter to do the drawing
image: QImage = QImage(size, size, QImage.Format.Format_ARGB32)
image.fill("#1e1e1e")

# Create an svg renderer, then render to the painter
svg: QSvgRenderer = QSvgRenderer(str(filepath))

if not svg.isValid():
raise UnidentifiedImageError

painter: QPainter = QPainter(image)
svg.setAspectRatioMode(Qt.AspectRatioMode.KeepAspectRatio)
svg.render(painter)
painter.end()

# Write the image to a buffer as png
buffer: QBuffer = QBuffer()
buffer.open(QBuffer.OpenModeFlag.ReadWrite)
image.save(buffer, "PNG")

# Load the image from the buffer
im = Image.new("RGB", (size, size), color="#1e1e1e")
im.paste(Image.open(BytesIO(buffer.data().data())))
im = im.convert(mode="RGB")

buffer.close()
return im

def _model_stl_thumb(self, filepath: Path, size: int) -> Image.Image:
Expand Down Expand Up @@ -982,6 +1008,7 @@ def render(
ext, MediaCategories.IMAGE_RAW_TYPES, mime_fallback=True
):
image = self._image_raw_thumb(_filepath)
# Vector Images --------------------------------------------
elif MediaCategories.is_ext_in_category(
ext, MediaCategories.IMAGE_VECTOR_TYPES, mime_fallback=True
):
Expand Down
8 changes: 8 additions & 0 deletions tagstudio/tests/fixtures/sample.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions tagstudio/tests/qt/test_thumb_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,14 @@ def test_pdf_preview(cwd, snapshot):
img.save(img_bytes, format="PNG")
img_bytes.seek(0)
assert img_bytes.read() == snapshot(extension_class=PNGImageSnapshotExtension)


def test_svg_preview(cwd, snapshot):
file_path: Path = cwd / "fixtures" / "sample.svg"
renderer = ThumbRenderer()
img: Image.Image = renderer._image_vector_thumb(file_path, 200)

img_bytes = io.BytesIO()
img.save(img_bytes, format="PNG")
img_bytes.seek(0)
assert img_bytes.read() == snapshot(extension_class=PNGImageSnapshotExtension)

0 comments on commit 5ca7a23

Please sign in to comment.