-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #324 from ryanohoro/transcode
ScanTranscode - Convert New/Uncommon Image Formats
- Loading branch information
Showing
11 changed files
with
210 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import io | ||
import logging | ||
|
||
import pillow_avif | ||
from PIL import Image, UnidentifiedImageError | ||
from pillow_heif import register_heif_opener | ||
|
||
from strelka import strelka | ||
|
||
logging.getLogger("PIL").setLevel(logging.WARNING) | ||
|
||
# Must be imported as a plugin, doesn't need to be used | ||
_ = pillow_avif.AvifImagePlugin | ||
|
||
register_heif_opener() | ||
|
||
|
||
class ScanTranscode(strelka.Scanner): | ||
""" | ||
Converts supported images for easier scanning | ||
Typical supported output options: | ||
gif webp jpeg bmp png tiff | ||
""" | ||
|
||
def scan(self, data, file, options, expire_at): | ||
output_format = options.get("output_format", "jpeg") | ||
|
||
def convert(im): | ||
with io.BytesIO() as f: | ||
im.save(f, format=f"{output_format}", quality=90) | ||
return f.getvalue() | ||
|
||
try: | ||
converted_image = convert(Image.open(io.BytesIO(data))) | ||
|
||
# Send extracted file back to Strelka | ||
self.emit_file(converted_image, name=file.name) | ||
except UnidentifiedImageError: | ||
self.flags.append("unidentified_image") | ||
return | ||
|
||
self.flags.append("transcoded") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
from pathlib import Path | ||
from unittest import TestCase, mock | ||
|
||
import pytest | ||
from strelka.scanners.scan_transcode import ScanTranscode as ScanUnderTest | ||
from strelka.tests import run_test_scan | ||
|
||
output_formats = ["gif", "webp", "jpeg", "bmp", "png", "tiff"] | ||
|
||
|
||
@pytest.mark.parametrize("output_format", output_formats) | ||
def test_scan_transcode_avif(mocker, output_format) -> None: | ||
""" | ||
Pass: Sample event matches output of scanner. | ||
Failure: Unable to load file or sample event fails to match. | ||
""" | ||
|
||
test_scan_event = {"elapsed": mock.ANY, "flags": ["transcoded"]} | ||
|
||
scanner_event = run_test_scan( | ||
mocker=mocker, | ||
scan_class=ScanUnderTest, | ||
fixture_path=Path(__file__).parent / "fixtures/test_qr.avif", | ||
options={"output_format": output_format}, | ||
) | ||
|
||
TestCase.maxDiff = None | ||
TestCase().assertDictEqual(test_scan_event, scanner_event) | ||
|
||
|
||
@pytest.mark.parametrize("output_format", output_formats) | ||
def test_scan_transcode_heic(mocker, output_format) -> None: | ||
""" | ||
Pass: Sample event matches output of scanner. | ||
Failure: Unable to load file or sample event fails to match. | ||
""" | ||
|
||
test_scan_event = {"elapsed": mock.ANY, "flags": ["transcoded"]} | ||
|
||
scanner_event = run_test_scan( | ||
mocker=mocker, | ||
scan_class=ScanUnderTest, | ||
fixture_path=Path(__file__).parent / "fixtures/test_qr.heic", | ||
options={"output_format": output_format}, | ||
) | ||
|
||
TestCase.maxDiff = None | ||
TestCase().assertDictEqual(test_scan_event, scanner_event) | ||
|
||
|
||
@pytest.mark.parametrize("output_format", output_formats) | ||
def test_scan_transcode_heif(mocker, output_format) -> None: | ||
""" | ||
Pass: Sample event matches output of scanner. | ||
Failure: Unable to load file or sample event fails to match. | ||
""" | ||
|
||
test_scan_event = {"elapsed": mock.ANY, "flags": ["transcoded"]} | ||
|
||
scanner_event = run_test_scan( | ||
mocker=mocker, | ||
scan_class=ScanUnderTest, | ||
fixture_path=Path(__file__).parent / "fixtures/test_qr.heif", | ||
options={"output_format": output_format}, | ||
) | ||
|
||
TestCase.maxDiff = None | ||
TestCase().assertDictEqual(test_scan_event, scanner_event) | ||
|
||
|
||
def test_scan_transcode_broken_heic(mocker) -> None: | ||
""" | ||
Pass: Sample event matches output of scanner. | ||
Failure: Unable to load file or sample event fails to match. | ||
""" | ||
|
||
test_scan_event = {"elapsed": mock.ANY, "flags": ["unidentified_image"]} | ||
|
||
scanner_event = run_test_scan( | ||
mocker=mocker, | ||
scan_class=ScanUnderTest, | ||
fixture_path=Path(__file__).parent / "fixtures/test_broken.heic", | ||
) | ||
|
||
TestCase.maxDiff = None | ||
TestCase().assertDictEqual(test_scan_event, scanner_event) |