Skip to content

Commit

Permalink
Merge pull request #52 from Pixboost/feature/is-illustration-separate…
Browse files Browse the repository at this point in the history
…-process

#51: Checking if image is an illustration in separate process to prev…
  • Loading branch information
dooman87 authored Aug 2, 2024
2 parents 562d867 + 77fad81 commit f6bead7
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 155 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ jobs:
push: false
load: true
tags: transformimgs
build-args: |
"BRANCH=${{ github.ref_name }}"
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache
-
Expand Down
10 changes: 9 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM dpokidov/imagemagick:7.1.1-31-2-bookworm AS build

ARG BRANCH=main

RUN apt-get update && apt-get upgrade -y && apt-get install -y --no-install-recommends \
g++ \
gcc \
Expand Down Expand Up @@ -107,7 +109,12 @@ WORKDIR $GOPATH

RUN mkdir -p /go/src/github.com/Pixboost/
WORKDIR /go/src/github.com/Pixboost/
RUN git clone https://github.com/Pixboost/transformimgs.git

RUN git clone --branch $BRANCH --single-branch https://github.com/Pixboost/transformimgs.git

WORKDIR /go/src/github.com/Pixboost/transformimgs/illustration

RUN go build -o /illustration

WORKDIR /go/src/github.com/Pixboost/transformimgs/cmd

Expand All @@ -118,6 +125,7 @@ FROM dpokidov/imagemagick:7.1.1-31-2-bookworm
ENV IM_HOME /usr/local/bin

USER 65534
COPY --from=build --chown=nobody:nogroup /illustration /usr/local/bin/illustration
COPY --from=build --chown=nobody:nogroup /transformimgs /transformimgs

ENTRYPOINT ["/transformimgs", "-imConvert=/usr/local/bin/convert", "-imIdentify=/usr/local/bin/identify"]
7 changes: 7 additions & 0 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,15 @@ ENV PATH $GOPATH/bin:$PATH
RUN mkdir -p "$GOPATH/src" "$GOPATH/bin" && chmod -R 1777 "$GOPATH"
WORKDIR $GOPATH



ENV IM_HOME /usr/local/bin

COPY ./illustration /illustration
WORKDIR /illustration
RUN go install

VOLUME /go/src/github.com/Pixboost/transformimgs/
WORKDIR /go/src/github.com/Pixboost/transformimgs/

ENTRYPOINT ["sh", "/go/src/github.com/Pixboost/transformimgs/run.sh"]
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,17 @@ Prerequisites:
* Go 1.18+ with [modules support](https://golang.org/ref/mod)
* Installed [imagemagick v7.0.25+](http://imagemagick.org) with AVIF support in `/usr/local/bin`

Install illustration command:

```bash
cd illustration/
go install
```
$ git clone [email protected]:Pixboost/transformimgs.git
$ cd transformimgs
$ ./run.sh

Run the application:

```bash
./run.sh
```

### Using from Go Web Application
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ require (
github.com/dooman87/glogi v0.0.0-20180107233622-68f3443d07f1
github.com/dooman87/kolibri v0.0.0-20170117194222-c194ff118b67
github.com/gorilla/mux v1.8.1
gopkg.in/gographics/imagick.v3 v3.7.0
)
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,3 @@ github.com/dooman87/kolibri v0.0.0-20170117194222-c194ff118b67 h1:5zx4LUSP0iPn0K
github.com/dooman87/kolibri v0.0.0-20170117194222-c194ff118b67/go.mod h1:IGXOwI2+tWhVzcLeKONI0eXxxFVC4+A5ZFCup6fuQqE=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
gopkg.in/gographics/imagick.v3 v3.5.1 h1:58JqK0UCx5RfvbRggF5FKuK6jHwAtTQopUxK8mzFa40=
gopkg.in/gographics/imagick.v3 v3.5.1/go.mod h1:+Q9nyA2xRZXrDyTtJ/eko+8V/5E7bWYs08ndkZp8UmA=
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=
5 changes: 5 additions & 0 deletions illustration/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/Pixboost/transformimgs/illustration

go 1.18

require gopkg.in/gographics/imagick.v3 v3.7.0
2 changes: 2 additions & 0 deletions illustration/go.sum
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=
122 changes: 122 additions & 0 deletions illustration/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
illustration command takes an image in stdin and prints true if image is cartoon like, including
icons, logos, illustrations.
It prints "false" for banners, product images, photos.
The initial idea is from here: https://legacy.imagemagick.org/Usage/compare/#type_reallife
*/
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)
}
Loading

0 comments on commit f6bead7

Please sign in to comment.