Browse Source

Add grayscale image step

feature/add-image-processing
Roman Zipp 3 years ago
parent
commit
e9740e5c8d
  1. 6
      config/example.json
  2. 11
      pipelines/executable_step.go
  3. 44
      pipelines/pipeline_test.go
  4. 4
      pipelines/step.go

6
config/example.json

@ -15,10 +15,14 @@
},
{
"name": "rotate image",
"type": 0,
"type": 1,
"options": {
"angle": 90.0
}
},
{
"name": "grayscale",
"type": 2
}
],
"output": {

11
pipelines/executable_step.go

@ -38,3 +38,14 @@ func (s RotateImageStep) Execute(src image.Image) (image.Image, error) {
src = imaging.Rotate(src, s.Options.Angle, image.Black)
return src, nil
}
// Grayscale image
type GrayscaleImageStep struct {
Step
}
func (s GrayscaleImageStep) Execute(src image.Image) (image.Image, error) {
src = imaging.Grayscale(src)
return src, nil
}

44
pipelines/pipeline_test.go

@ -189,6 +189,50 @@ func TestExecuteSteps(t *testing.T) {
os.Remove(storageProvider.GetPath(Bucket, "source.jpg"))
os.Remove(storageProvider.GetPath(Bucket, dest))
})
t.Run("Image grayscale step is successful", func(t *testing.T) {
const Bucket string = "pipeline_test_05"
const Payload string = `{
"name": "example pipeline",
"type": 1,
"removeMetadata": false,
"steps": [
{
"name": "grayscale",
"type": 2
}
]
}`
wd, _ := os.Getwd()
pipe := DeserializePipelines([][]byte{[]byte(Payload)})[0]
storageProvider := storage.GetFileSystemStorageProvider("test", "..")
// copy test file to storage bucket
_, err := storageProvider.StoreExisting(Bucket, "source.jpg", filepath.Join(wd, "../tests/files/900x900.jpg"))
assert.Nil(t, err, "Test file should be readable")
assert.FileExists(t, storageProvider.GetPath(Bucket, "source.jpg"))
// run pipeline steps
dest, err := pipe.Run("source.jpg", Bucket, storageProvider)
assert.Nil(t, err)
assert.FileExists(t, storageProvider.GetPath(Bucket, dest))
// read image config
file, err := storageProvider.OpenFile(Bucket, dest)
assert.Nil(t, err)
imgConf, _, err := image.DecodeConfig(file)
assert.Nil(t, err)
assert.Equal(t, 900, imgConf.Width)
assert.Equal(t, 900, imgConf.Height)
// clean up
os.Remove(storageProvider.GetPath(Bucket, "source.jpg"))
os.Remove(storageProvider.GetPath(Bucket, dest))
})
}
// output options

4
pipelines/step.go

@ -10,6 +10,7 @@ type StepType int
const (
TypeResizeImageStep StepType = iota
TypeRotateImageStep
TypeGrayscaleImageStep
)
type Step struct {
@ -34,6 +35,9 @@ func (s Step) GetExecutable() (IExecutableStep, error) {
return nil, err
}
return step, nil
case TypeGrayscaleImageStep:
return GrayscaleImageStep{}, nil
}
return nil, errors.New("invalid type")

Loading…
Cancel
Save