Skip to content

Commit c9b6fab

Browse files
committed
refactor(ops)!: clean up remaining image operations
- Renamed `tamper:GaussianBlur` to `tamper:AddGaussianBlur` and added `tamper:AddBlur` parent class - moved remaining image ops to their own files Signed-off-by: Joshua Locash <locashjosh@gmail.com>
1 parent aef1ffb commit c9b6fab

12 files changed

Lines changed: 145 additions & 123 deletions

File tree

docs/operations.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Applies a median blur over a square neighbourhood.
9595
tamper:kernelSize 3 .
9696
```
9797

98-
### GaussianBlur`tamper:GaussianBlur`
98+
### AddGaussianBlur`tamper:AddGaussianBlur`
9999

100100
Applies a Gaussian blur over a square kernel.
101101

@@ -107,7 +107,7 @@ Applies a Gaussian blur over a square kernel.
107107
```turtle
108108
@prefix tamper: <https://example.org/tamper/core#> .
109109
110-
[] a tamper:GaussianBlur ;
110+
[] a tamper:AddGaussianBlur ;
111111
tamper:kernelSize 5 ;
112112
tamper:blurSigma 0.0 .
113113
```

tamper/ops/__init__.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
from .add_noise import AddGaussianNoise, AddSaltPepperNoise
22
from .compress import Compress
33
from .crop import Crop
4-
from .image import (
5-
Resize,
6-
MedianFilter,
7-
GaussianBlur,
8-
)
4+
from .resize import Resize
5+
from .filtering import MedianFilter
6+
from .add_blur import AddGaussianBlur
97
from .resample import Resample
108
from .transcode import Transcode
119
from .validation import validate_operations
@@ -18,7 +16,7 @@
1816
"Crop",
1917
"Resize",
2018
"MedianFilter",
21-
"GaussianBlur",
19+
"AddGaussianBlur",
2220
"AddGaussianNoise",
2321
"AddSaltPepperNoise",
2422
"Resample",

tamper/ops/add_blur.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from os import PathLike
2+
from pathlib import Path
3+
4+
import cv2
5+
from rdflib import XSD
6+
7+
from tamper.vocabularies import TAMPER
8+
9+
from tamper.core import ImageAsset, Operation, MappedProperty
10+
11+
12+
class AddGaussianBlur(Operation):
13+
__rdf_type__ = TAMPER.AddGaussianBlur
14+
15+
kernel_size: MappedProperty[int] = MappedProperty(TAMPER.kernelSize, XSD.integer)
16+
sigma: MappedProperty[float] = MappedProperty(TAMPER.blurSigma, XSD.double)
17+
18+
def mutate(self, out_dir: PathLike[str] | None = None):
19+
used = self.get_used()
20+
if len(used) != 1:
21+
raise ValueError("Operation requires exactly one image asset")
22+
23+
img_asset = ImageAsset(self.graph, used[0])
24+
25+
img = cv2.imread(img_asset.file_path)
26+
blurred = cv2.GaussianBlur(
27+
img, (self.kernel_size, self.kernel_size), sigmaX=self.sigma
28+
)
29+
ext = Path(img_asset.file_path).suffix or ".png"
30+
ok, buf = cv2.imencode(ext, blurred)
31+
if not ok:
32+
raise RuntimeError(f"Encoding to {ext} failed")
33+
34+
with self._generates_file(dir=out_dir, suffix=ext) as f:
35+
Path(f).write_bytes(buf.tobytes())

tamper/ops/filtering.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from os import PathLike
2+
from pathlib import Path
3+
4+
import cv2
5+
from rdflib import XSD
6+
7+
from tamper.vocabularies import TAMPER
8+
9+
from tamper.core import ImageAsset, Operation, MappedProperty
10+
11+
12+
class MedianFilter(Operation):
13+
__rdf_type__ = TAMPER.MedianFilter
14+
15+
kernel_size: MappedProperty[int] = MappedProperty(TAMPER.kernelSize, XSD.integer)
16+
17+
def mutate(self, out_dir: PathLike[str] | None = None):
18+
used = self.get_used()
19+
if len(used) != 1:
20+
raise ValueError("Operation requires exactly one image asset")
21+
22+
img_asset = ImageAsset(self.graph, used[0])
23+
24+
img = cv2.imread(img_asset.file_path)
25+
filtered = cv2.medianBlur(img, self.kernel_size)
26+
ext = Path(img_asset.file_path).suffix or ".png"
27+
ok, buf = cv2.imencode(ext, filtered)
28+
if not ok:
29+
raise RuntimeError(f"Encoding to {ext} failed")
30+
31+
with self._generates_file(dir=out_dir, suffix=ext) as f:
32+
Path(f).write_bytes(buf.tobytes())

tamper/ops/image.py

Lines changed: 0 additions & 98 deletions
This file was deleted.

tamper/ops/operation-shapes.ttl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ tamper:MedianFilterShape a sh:NodeShape ;
197197
sh:targetClass tamper:MedianFilter ;
198198
sh:property tamper:KernelSizeShape .
199199

200-
tamper:GaussianBlurShape a sh:NodeShape ;
201-
sh:targetClass tamper:GaussianBlur ;
200+
tamper:AddGaussianBlurShape a sh:NodeShape ;
201+
sh:targetClass tamper:AddGaussianBlur ;
202202
sh:property tamper:KernelSizeShape ;
203203
sh:property tamper:BlurSigmaShape .
204204

tamper/ops/resize.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from os import PathLike
2+
from pathlib import Path
3+
4+
import cv2
5+
from rdflib import XSD
6+
7+
from tamper.vocabularies import TAMPER
8+
9+
from tamper.core import ImageAsset, Operation, MappedProperty
10+
11+
12+
_INTERPOLATIONS = {
13+
"nearest": cv2.INTER_NEAREST,
14+
"linear": cv2.INTER_LINEAR,
15+
"cubic": cv2.INTER_CUBIC,
16+
"area": cv2.INTER_AREA,
17+
"lanczos4": cv2.INTER_LANCZOS4,
18+
}
19+
20+
21+
class Resize(Operation):
22+
__rdf_type__ = TAMPER.Resize
23+
24+
width: MappedProperty[int] = MappedProperty(TAMPER.targetWidth, XSD.integer)
25+
height: MappedProperty[int] = MappedProperty(TAMPER.targetHeight, XSD.integer)
26+
interpolation: MappedProperty[str] = MappedProperty(
27+
TAMPER.interpolation, XSD.string
28+
)
29+
30+
def mutate(self, out_dir: PathLike[str] | None = None):
31+
used = self.get_used()
32+
if len(used) != 1:
33+
raise ValueError("Operation requires exactly one image asset")
34+
35+
img_asset = ImageAsset(self.graph, used[0])
36+
37+
img = cv2.imread(img_asset.file_path)
38+
resized = cv2.resize(
39+
img,
40+
(self.width, self.height),
41+
interpolation=_INTERPOLATIONS[self.interpolation],
42+
)
43+
ext = Path(img_asset.file_path).suffix or ".png"
44+
ok, buf = cv2.imencode(ext, resized)
45+
if not ok:
46+
raise RuntimeError(f"Encoding to {ext} failed")
47+
48+
with self._generates_file(dir=out_dir, suffix=ext) as f:
49+
Path(f).write_bytes(buf.tobytes())

tamper/plans/thread_executor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
AddGaussianNoise,
1616
Resize,
1717
MedianFilter,
18-
GaussianBlur,
18+
AddGaussianBlur,
1919
validate_operations,
2020
)
2121

@@ -31,7 +31,7 @@
3131
TAMPER.AddSaltPepperNoise: AddSaltPepperNoise,
3232
TAMPER.Resize: Resize,
3333
TAMPER.MedianFilter: MedianFilter,
34-
TAMPER.GaussianBlur: GaussianBlur,
34+
TAMPER.AddGaussianBlur: AddGaussianBlur,
3535
TAMPER.Resample: Resample,
3636
TAMPER.Crop: Crop,
3737
}

tamper/vocabularies/_TAMPER.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@ class TAMPER(DefinedNamespace):
77
DESCRIPTION_EDIT_ME_!
88
99
Generated from: SOURCE_RDF_FILE_EDIT_ME_!
10-
Date: 2026-06-14 03:27:32.161201
10+
Date: 2026-06-14 15:55:55.624992
1111
"""
1212

1313
_NS = Namespace("https://example.org/tamper/core#")
1414

15+
AddBlur: URIRef # Adds blur to an image
16+
AddGaussianBlur: URIRef # Convolves an image with a Gaussian kernel of a given size and standard deviation.
1517
AddGaussianNoise: URIRef # Adds gaussian noise to an image.
1618
AddNoise: URIRef # Adds noise to an image. An abstract grouping of the noise-type-specific operations.
1719
AddSaltPepperNoise: URIRef # Adds salt and pepper noise to an image
1820
AudioAsset: URIRef # A digital audio file containing one or more audio streams.
1921
AudioStream: URIRef # A stream containing audio sample data.
2022
Compress: URIRef # Applies compression to an image using a format and quality level
2123
Crop: URIRef # Extracts a rectangular region from a media asset, given a top-left origin and a width and height.
22-
GaussianBlur: URIRef # Convolves an image with a Gaussian kernel of a given size and standard deviation.
2324
ImageAsset: URIRef # A digital image file.
2425
MediaAsset: URIRef # A digital media file such as an image, audio, or video.
2526
MedianFilter: URIRef # Applies a median filter over a square neighbourhood, commonly used to suppress noise residuals.

tamper/vocabularies/tamper-core.ttl

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,16 @@ tamper:MedianFilter a owl:Class ;
9595
rdfs:comment "Applies a median filter over a square neighbourhood, commonly used to suppress noise residuals."@en ;
9696
rdfs:subClassOf tamper:Operation .
9797

98-
tamper:GaussianBlur a owl:Class ;
99-
rdfs:label "Gaussian Blur"@en ;
100-
rdfs:comment "Convolves an image with a Gaussian kernel of a given size and standard deviation."@en ;
98+
tamper:AddBlur a owl:Class ;
99+
rdfs:label "Add Blur"@en ;
100+
rdfs:comment "Adds blur to an image" ;
101101
rdfs:subClassOf tamper:Operation .
102102

103+
tamper:AddGaussianBlur a owl:Class ;
104+
rdfs:label "Add Gaussian Blur"@en ;
105+
rdfs:comment "Convolves an image with a Gaussian kernel of a given size and standard deviation."@en ;
106+
rdfs:subClassOf tamper:AddBlur .
107+
103108
tamper:Crop a owl:Class ;
104109
rdfs:label "Crop"@en ;
105110
rdfs:comment "Extracts a rectangular region from a media asset, given a top-left origin and a width and height."@en ;
@@ -369,15 +374,15 @@ tamper:kernelSize a owl:DatatypeProperty, owl:FunctionalProperty ;
369374
a owl:Class ;
370375
owl:unionOf (
371376
tamper:MedianFilter
372-
tamper:GaussianBlur
377+
tamper:AddGaussianBlur
373378
)
374379
] ;
375380
rdfs:range xsd:integer .
376381

377382
tamper:blurSigma a owl:DatatypeProperty, owl:FunctionalProperty ;
378383
rdfs:label "blur sigma"@en ;
379384
rdfs:comment "The Gaussian kernel standard deviation. A value of 0 lets the implementation derive it from the kernel size."@en ;
380-
rdfs:domain tamper:GaussianBlur ;
385+
rdfs:domain tamper:AddGaussianBlur ;
381386
rdfs:range xsd:double .
382387

383388
tamper:cropX a owl:DatatypeProperty, owl:FunctionalProperty ;

0 commit comments

Comments
 (0)