Created
November 14, 2013 19:54
-
-
Save clone1018/7473264 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// generator | |
package main | |
import ( | |
"errors" | |
"fmt" | |
"github.com/nfnt/resize" | |
"image" | |
_ "image/draw" | |
"image/jpeg" | |
"os" | |
"path/filepath" | |
"strconv" | |
) | |
const ( | |
X_360 = 400 | |
Y_360 = 1060 | |
WIDTH_360 = 3522 | |
HEIGHT_360 = 1026 | |
) | |
func (f Frame) Generate() { | |
f.generateFolders() | |
f.generate360() | |
f.generateVTO() | |
/* | |
errs = append(errs, f.generateFolder()...) | |
errs = append(errs, f.generateDescription()...) | |
errs = append(errs, f.generateFront()...) | |
errs = append(errs, f.generateVTO()...) | |
*/ | |
} | |
func (f Frame) generateFolders() { | |
createPath := filepath.Join("c:", "FrameBuffer", "Upload", f.sku, "360") | |
os.MkdirAll(createPath, 0755) | |
} | |
func (f Frame) generate360() { | |
for rot := 1; rot < 33; rot++ { | |
cNum := strPad(strconv.Itoa(rot)) | |
cImage := f.sku + "_" + cNum + ".jpg" | |
iPath := filepath.Join(f.path, "edits", cImage) | |
nPath := filepath.Join("c:", "FrameBuffer", "Upload", f.sku, "360", cImage) | |
file, _ := os.Open(iPath) | |
defer file.Close() | |
origin, _, _ := image.Decode(file) | |
// Crop image | |
cropped, _ := cropImage(origin, image.Rect(X_360, Y_360, X_360+WIDTH_360, Y_360+HEIGHT_360)) | |
resized := resizeImage(755, 220, cropped) | |
newFile, _ := os.Create(nPath) | |
defer newFile.Close() | |
jpeg.Encode(newFile, resized, nil) | |
fmt.Println("Saved: " + nPath) | |
} | |
} | |
func (f Frame) generateVTO() { | |
// Coming soon | |
} | |
func cropImage(i image.Image, d image.Rectangle) (image.Image, error) { | |
bounds := i.Bounds() | |
if bounds.Min.X > d.Min.X || bounds.Min.Y > d.Min.Y || bounds.Max.X < d.Max.X || bounds.Max.Y < d.Max.Y { | |
return nil, errors.New("Bounds invalid for crop") | |
} | |
dims := d.Size() | |
outIm := image.NewRGBA(image.Rect(0, 0, dims.X, dims.Y)) | |
for x := 0; x < dims.X; x++ { | |
for y := 0; y < dims.Y; y++ { | |
outIm.Set(x, y, i.At(d.Min.X+x, d.Min.Y+y)) | |
} | |
} | |
return outIm, nil | |
} | |
func resizeImage(width, height uint, img image.Image) image.Image { | |
return resize.Resize(width, height, img, resize.Bicubic) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment