Skip to content

Commit ba7c147

Browse files
committed
convert images to greyscale
1 parent 7f94598 commit ba7c147

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

batch-processing/IMPLEMENTATION.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,7 @@ develop:
6565
docker run -it --mount type=bind,source="$$(pwd)",target=/app --mount type=bind,source="/tmp",target=/tmp --rm develop
6666
rm -rf ./mount
6767
```
68+
69+
## Greyscale
70+
71+
`convert`, accessed via `ConvertImageCommand`, with `-set colorspace Gray -separate -average` seems to work well.

batch-processing/main.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ func main() {
2323
os.Exit(1)
2424
}
2525

26+
// Set up imagemagick
27+
imagick.Initialize()
28+
defer imagick.Terminate()
29+
2630
// Open the file supplied
2731
in, err := os.Open(*input)
2832
if err != nil {
@@ -41,16 +45,17 @@ func main() {
4145
for i, row := range records[1:] {
4246
url := row[0]
4347

44-
filepath := fmt.Sprintf("/tmp/%d-%d.%s", time.Now().UnixMilli(), rand.Int(), "jpg")
48+
inputFilepath := fmt.Sprintf("/tmp/%d-%d.%s", time.Now().UnixMilli(), rand.Int(), "jpg")
49+
outputFilepath := fmt.Sprintf("/tmp/%d-%d.%s", time.Now().UnixMilli(), rand.Int(), "jpg")
4550

46-
log.Printf("downloading: row %d (%q) to %q", i, url, filepath)
51+
log.Printf("downloading: row %d (%q) to %q", i, url, inputFilepath)
4752

4853
// Create a new file that we will write to
49-
out, err := os.Create(filepath)
54+
input, err := os.Create(inputFilepath)
5055
if err != nil {
5156
log.Fatal(err)
5257
}
53-
defer out.Close()
58+
defer input.Close()
5459

5560
// Get it from the internet!
5661
res, err := http.Get(url)
@@ -62,13 +67,19 @@ func main() {
6267
// TODO: check status code
6368

6469
// Copy the body of the response to the created file
65-
_, err = io.Copy(out, res.Body)
70+
_, err = io.Copy(input, res.Body)
71+
if err != nil {
72+
log.Fatal(err)
73+
}
74+
75+
ret, err := imagick.ConvertImageCommand([]string{
76+
"convert", inputFilepath, "-set", "colorspace", "Gray", "-separate", "-average", outputFilepath,
77+
})
6678
if err != nil {
6779
log.Fatal(err)
6880
}
81+
82+
fmt.Printf("monochrome: %s\n%s\n", outputFilepath, ret.Meta)
6983
}
7084

71-
// Set up imagemagick
72-
imagick.Initialize()
73-
defer imagick.Terminate()
7485
}

0 commit comments

Comments
 (0)