-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#51: Checking if image is an illustration in separate process to prev…
…ent imagick memory footprint grow
- Loading branch information
Showing
7 changed files
with
196 additions
and
144 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/Pixboost/illustration | ||
|
||
go 1.18 | ||
|
||
require gopkg.in/gographics/imagick.v3 v3.7.0 // indirect |
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,2 @@ | ||
gopkg.in/gographics/imagick.v3 v3.7.0 h1:w8iQa58ikuqjX4l2OVML3pgqFcDMD8ywXJ9/cXa33fk= | ||
gopkg.in/gographics/imagick.v3 v3.7.0/go.mod h1:+Q9nyA2xRZXrDyTtJ/eko+8V/5E7bWYs08ndkZp8UmA= |
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,114 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"gopkg.in/gographics/imagick.v3/imagick" | ||
"io" | ||
"log" | ||
"math" | ||
"os" | ||
"sort" | ||
) | ||
|
||
type colorSlice []*imagick.PixelWand | ||
|
||
func (c colorSlice) Len() int { return len(c) } | ||
func (c colorSlice) Less(i, j int) bool { return c[i].GetColorCount() < c[j].GetColorCount() } | ||
func (c colorSlice) Swap(i, j int) { c[i], c[j] = c[j], c[i] } | ||
|
||
func main() { | ||
imgData, err := io.ReadAll(os.Stdin) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
var ( | ||
colors colorSlice | ||
colorsCnt uint | ||
) | ||
|
||
mw := imagick.NewMagickWand() | ||
|
||
err = mw.ReadImageBlob(imgData) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
if (mw.GetImageWidth() * mw.GetImageHeight()) > 500*500 { | ||
aspectRatio := float32(mw.GetImageWidth()) / float32(mw.GetImageHeight()) | ||
err = mw.ScaleImage(500, uint(500/aspectRatio)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
colorsCnt, colors = mw.GetImageHistogram() | ||
if colorsCnt > 30000 { | ||
fmt.Print(false) | ||
return | ||
} | ||
|
||
sort.Sort(sort.Reverse(colors)) | ||
|
||
var ( | ||
colorIdx int | ||
count uint | ||
currColor *imagick.PixelWand | ||
pixelsCount = uint(0) | ||
totalPixelsCount = float32(mw.GetImageHeight() * mw.GetImageWidth()) | ||
tenPercent = uint(totalPixelsCount * 0.1) | ||
fiftyPercent = uint(totalPixelsCount * 0.5) | ||
isBackground = false | ||
lastBackgroundColor *imagick.PixelWand | ||
colorsInBackground = uint(0) | ||
pixelsInBackground = uint(0) | ||
) | ||
|
||
for colorIdx, currColor = range colors { | ||
if pixelsCount > fiftyPercent { | ||
break | ||
} | ||
|
||
count = currColor.GetColorCount() | ||
|
||
switch { | ||
case colorIdx == 0: | ||
isBackground = true | ||
lastBackgroundColor = currColor | ||
pixelsInBackground += count | ||
colorsInBackground++ | ||
case isBackground: | ||
// Comparing colors to find out if it's still background or not. | ||
// This logic addresses backgrounds with more than one similar color. | ||
alphaDiff := currColor.GetAlpha() - lastBackgroundColor.GetAlpha() | ||
redDiff := currColor.GetRed() - lastBackgroundColor.GetRed() | ||
greenDiff := currColor.GetGreen() - lastBackgroundColor.GetGreen() | ||
blueDiff := currColor.GetBlue() - lastBackgroundColor.GetBlue() | ||
distance := | ||
math.Max(math.Pow(redDiff, 2), math.Pow(redDiff-alphaDiff, 2)) + | ||
math.Max(math.Pow(greenDiff, 2), math.Pow(greenDiff-alphaDiff, 2)) + | ||
math.Max(math.Pow(blueDiff, 2), math.Pow(blueDiff-alphaDiff, 2)) | ||
if distance < 0.1 { | ||
lastBackgroundColor = currColor | ||
pixelsInBackground += count | ||
colorsInBackground++ | ||
} else { | ||
isBackground = false | ||
if pixelsInBackground < tenPercent { | ||
pixelsCount = pixelsInBackground | ||
colorsInBackground = 0 | ||
pixelsInBackground = 0 | ||
} else { | ||
pixelsCount += count | ||
fiftyPercent = uint((totalPixelsCount - float32(pixelsInBackground)) * 0.5) | ||
} | ||
} | ||
default: | ||
pixelsCount += count | ||
} | ||
} | ||
|
||
colorsCntIn50Pct := uint(colorIdx) - colorsInBackground | ||
|
||
fmt.Print(colorsCntIn50Pct < 10 || (float32(colorsCntIn50Pct)/float32(colorsCnt)) <= 0.02) | ||
} |
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
Oops, something went wrong.