Skip to content

Instantly share code, notes, and snippets.

@hirokazumiyaji
Created September 30, 2016 09:43
Show Gist options
  • Save hirokazumiyaji/2dfdb78712739cb57e62210837b78e54 to your computer and use it in GitHub Desktop.
Save hirokazumiyaji/2dfdb78712739cb57e62210837b78e54 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"image"
"image/draw"
"io/ioutil"
"math/rand"
"net/http"
"path"
"time"
"github.com/disintegration/imaging"
"github.com/golang/freetype"
)
func main() {
urls := []string{
"http://pic.prepics-cdn.com/xxx888xxx/40714761.jpeg",
"https://images-na.ssl-images-amazon.com/images/I/81h5pQdGJHL.jpg",
"http://digzoom.com/wp-content/uploads/2016/01/nishinonanase1.jpg",
}
rand.Seed(time.Now().UnixNano())
u := urls[rand.Intn(len(urls)-1)]
res, err := http.Get(u)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
img, _, err := image.Decode(res.Body)
if err != nil {
fmt.Println(err)
return
}
_, name := path.Split(u)
fb, err := ioutil.ReadFile("font.ttf")
if err != nil {
fmt.Println(err)
return
}
f, err := freetype.ParseFont(fb)
if err != nil {
fmt.Println(err)
return
}
rgba := image.NewRGBA(img.Bounds())
draw.Draw(rgba, rgba.Bounds(), img, image.ZP, draw.Src)
c := freetype.NewContext()
c.SetDPI(72)
c.SetFont(f)
c.SetFontSize(200)
c.SetClip(rgba.Bounds())
c.SetDst(rgba)
c.SetSrc(image.Black)
location, err := time.LoadLocation("Asia/Tokyo")
if err != nil {
fmt.Println(err)
return
}
now := time.Now().In(location)
pt := freetype.Pt(rgba.Bounds().Size().X/2-100, rgba.Bounds().Size().Y-50)
if _, err := c.DrawString(fmt.Sprintf("%02d:%02d", now.Hour(), now.Minute()), pt); err != nil {
fmt.Println(err)
}
imaging.Save(rgba, name)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment