mirror of
https://github.com/jehiah/TrafficSpeed.git
synced 2025-12-24 12:47:54 +08:00
step2 - implement rotation
This commit is contained in:
@@ -27,4 +27,12 @@ If the image file is not detected peroperly:
|
||||
ffmpeg -i IMG_8491.MOV -an -c copy IMG_8491.m4a
|
||||
```
|
||||
|
||||
CGO_LDFLAGS="-L/usr/local/Cellar/ffmpeg/3.3/lib" CGO_CFLAGS="-I/usr/local/Cellar/ffmpeg/3.3/include" gb build
|
||||
CGO_LDFLAGS="-L/usr/local/Cellar/ffmpeg/3.3/lib" CGO_CFLAGS="-I/usr/local/Cellar/ffmpeg/3.3/include" gb build
|
||||
|
||||
|
||||
# Image libraries
|
||||
|
||||
> https://github.com/bamiaux/rez Image resizing in pure Go and SIMD
|
||||
http://www.imagemagick.org/Usage/distorts/
|
||||
https://godoc.org/github.com/rainycape/magick
|
||||
https://godoc.org/gopkg.in/gographics/imagick.v3/imagick#DistortImageMethod
|
||||
@@ -12,6 +12,8 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gopkg.in/gographics/imagick.v3/imagick"
|
||||
)
|
||||
|
||||
type FrameAnalysis struct {
|
||||
@@ -50,6 +52,8 @@ func OpenInBrowser(l net.Listener) error {
|
||||
}
|
||||
|
||||
func main() {
|
||||
imagick.Initialize()
|
||||
defer imagick.Terminate()
|
||||
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.Llongfile)
|
||||
fileName := flag.String("file", "", "")
|
||||
httpAddress := flag.String("http-address", ":53001", "http address")
|
||||
|
||||
@@ -62,6 +62,13 @@ func ParsePoint(s string) (p Point) {
|
||||
|
||||
const SkipRotate = 0.00001
|
||||
|
||||
func RadiansToDegrees(rad float64) (deg float64) {
|
||||
deg = rad * 180.0 / math.Pi
|
||||
return
|
||||
}
|
||||
|
||||
const rightAngelRadians = 1.570796 // 90 degrees
|
||||
|
||||
func Radians(a, b Point) float64 {
|
||||
if a.Y == b.Y {
|
||||
return SkipRotate
|
||||
@@ -71,12 +78,12 @@ func Radians(a, b Point) float64 {
|
||||
opposite := math.Max(a.Y, b.Y) - math.Min(a.Y, b.Y)
|
||||
radians := math.Atan(adjacent / opposite)
|
||||
log.Printf("adjacent: %v opposite %v radians %v", adjacent, opposite, radians)
|
||||
if a.Y < b.Y {
|
||||
adjusted := (-1 * radians) + 1.570796
|
||||
if a.Y > b.Y {
|
||||
adjusted := (-1 * radians) + rightAngelRadians
|
||||
log.Printf("adjusting to %v because %v < %v", adjusted, a, b)
|
||||
return adjusted
|
||||
}
|
||||
adjusted := radians - 1.570796
|
||||
adjusted := radians - rightAngelRadians
|
||||
log.Printf("adjusting to %v because %v > %v", adjusted, a, b)
|
||||
return adjusted
|
||||
}
|
||||
|
||||
@@ -4,8 +4,10 @@ package main
|
||||
// #cgo CGO_CFLAGS="-I/usr/local/Cellar/ffmpeg/3.3/include"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"image/png"
|
||||
"io"
|
||||
"log"
|
||||
"time"
|
||||
@@ -16,6 +18,7 @@ import (
|
||||
"github.com/nareix/joy4/format"
|
||||
"github.com/nfnt/resize"
|
||||
// "github.com/nareix/joy4/format/mp4"
|
||||
"gopkg.in/gographics/imagick.v3/imagick"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -27,7 +30,7 @@ type Project struct {
|
||||
Filename string `json:"filename"`
|
||||
|
||||
// User Inputs
|
||||
Rotate float64 `json:"rotate,omitempty"`
|
||||
Rotate float64 `json:"rotate,omitempty"` // radians
|
||||
BBox *BBox `json:"bbox,omitempty"`
|
||||
Masks []Mask `json:"masks,omitempty"`
|
||||
Tolerance float64 `json:"tolerance"`
|
||||
@@ -137,6 +140,20 @@ func (p *Project) Run() error {
|
||||
if p.Step == 2 {
|
||||
p.Response.Step2Img, err = dataImg(&vf.Image)
|
||||
}
|
||||
if p.Step == 3 {
|
||||
// apply rotation
|
||||
mw := imagick.NewMagickWand()
|
||||
background := imagick.NewPixelWand()
|
||||
background.SetColor("#000000")
|
||||
out := new(bytes.Buffer)
|
||||
png.Encode(out, &vf.Image)
|
||||
mw.ReadImageBlob(out.Bytes())
|
||||
mw.RotateImage(background, RadiansToDegrees(p.Rotate))
|
||||
mw.SetImageFormat("PNG")
|
||||
imgBytes := mw.GetImageBlob()
|
||||
p.Response.Step3Img = dataImgFromBytes(imgBytes)
|
||||
}
|
||||
|
||||
}
|
||||
p.Duration = pkt.Time
|
||||
p.Frames = int64(frame)
|
||||
|
||||
@@ -18,15 +18,18 @@ func dataImg(img image.Image) (template.URL, error) {
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return dataImgFromBytes(out.Bytes()), nil
|
||||
}
|
||||
|
||||
func dataImgFromBytes(b []byte) template.URL {
|
||||
// This now takes a []byte of the buffer and base64 encodes it to a string
|
||||
// Never needing to create the image file all done in memory
|
||||
base64Img := base64.StdEncoding.EncodeToString(out.Bytes())
|
||||
base64Img := base64.StdEncoding.EncodeToString(b)
|
||||
|
||||
//And now you can see the magic happen.
|
||||
// Go ahead and run it then take the output and copy/paste it in your
|
||||
// browser's URL bar, and you'll see your image.
|
||||
return template.URL("data:image/png;base64," + base64Img), nil
|
||||
return template.URL("data:image/png;base64," + base64Img)
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
Reference in New Issue
Block a user