Coverage for /opt/conda/envs/apienv/lib/python3.10/site-packages/daiquiri/implementors/imageviewer/reference.py: 0%
34 statements
« prev ^ index » next coverage.py v7.6.5, created at 2024-11-15 02:12 +0000
« prev ^ index » next coverage.py v7.6.5, created at 2024-11-15 02:12 +0000
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3import os
4import shutil
6from marshmallow import fields, validates_schema, ValidationError
8from PIL import Image
10from daiquiri.core.components import ComponentActor, ComponentActorSchema
11from daiquiri.core.components.utils.tileimage import generate_pyramid
14class ReferenceSchema(ComponentActorSchema):
15 sampleid = fields.Int(required=True, metadata={"title": "Sampleid"})
16 file_path = fields.Str(required=True, metadata={"title": "File Path"})
17 description = fields.Str(required=True, metadata={"title": "Description"})
19 @validates_schema
20 def schema_validate(self, data, **kwargs):
21 if not os.path.exists(data["file_path"]):
22 raise ValidationError("Selected file does not exist")
24 _, ext = os.path.splitext(data["file_path"])
25 if ext.lower()[1:] not in ["jpg", "jpeg", "png"]:
26 raise ValidationError("File must be a jpg or png")
28 class Meta:
29 uischema = {
30 "sampleid": {"classNames": "hidden-row", "ui:widget": "hidden"},
31 }
34class ReferenceActor(ComponentActor):
35 schema = ReferenceSchema
36 name = "reference"
37 saving_args = {"data_filename": "{sampleid.name}_reference{sampleactionid}"}
39 def method(
40 self,
41 file_path,
42 description,
43 **kwargs,
44 ):
45 file_base = f"reference_{self['sampleactionid']}"
46 output_filename = os.path.join(self["base_path"], f"{file_base}.h5")
47 snapshot_filename = os.path.join(self["base_path"], f"{file_base}_snapshot.jpg")
48 original_filename = os.path.join(self["base_path"], os.path.basename(file_path))
50 shutil.copy(file_path, original_filename)
52 generate_pyramid(output_filename, file_path, 100, 1.0)
54 with Image.open(file_path) as thumb:
55 thumb.thumbnail((1024, 1024), Image.LANCZOS)
56 thumb.save(snapshot_filename)
58 self.metadata.update_sampleaction(
59 sampleactionid=self["sampleactionid"],
60 no_context=True,
61 resultfilepath=output_filename,
62 xtalsnapshotafter=snapshot_filename,
63 xtalsnapshotbefore=original_filename,
64 message=description,
65 )