本文整理汇总了Golang中github.com/kitwalker12/fotomat/vips.Image类的典型用法代码示例。如果您正苦于以下问题:Golang Image类的具体用法?Golang Image怎么用?Golang Image使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Image类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Save
// Save returns an Image compressed using the given SaveOptions as a byte slice.
func Save(image *vips.Image, options SaveOptions) ([]byte, error) {
if options.Quality < 1 || options.Quality > 100 {
options.Quality = DefaultQuality
}
if options.Compression < 1 || options.Compression > 9 {
options.Compression = DefaultCompression
}
// Make a decision on image format and whether we're using lossless.
if options.Format == Unknown {
if options.AllowWebp {
options.Format = Webp
} else if image.HasAlpha() || useLossless(image, options) {
options.Format = Png
} else {
options.Format = Jpeg
}
} else if options.Format == Webp && !useLossless(image, options) {
options.Lossless = false
}
switch options.Format {
case Jpeg:
return jpegSave(image, options)
case Png:
return pngSave(image, options)
case Webp:
return webpSave(image, options)
default:
return nil, ErrInvalidSaveFormat
}
}
示例2: DetectOrientation
// DetectOrientation detects the current Image Orientation from the EXIF header.
func DetectOrientation(image *vips.Image) Orientation {
o, ok := image.ImageGetAsString(vips.ExifOrientation)
if !ok || o == "" {
return Undefined
}
orientation, err := strconv.Atoi(o[:1])
if err != nil || orientation <= 0 || orientation >= len(orientationInfo) {
return Undefined
}
return Orientation(orientation)
}
示例3: crop
func crop(image *vips.Image, ow, oh int) error {
m := format.MetadataImage(image)
// If we have nothing to do, return.
if ow == m.Width && oh == m.Height {
return nil
}
// Center horizontally
x := (m.Width - ow + 1) / 2
// Assume faces are higher up vertically
y := (m.Height - oh + 1) / 4
if x < 0 || y < 0 {
panic("Bad crop offsets!")
}
return image.ExtractArea(m.Orientation.Crop(ow, oh, x, y, m.Width, m.Height))
}
示例4: Apply
// Apply executes a set of operations to change the pixel ordering from
// orientation to TopLeft.
func (orientation Orientation) Apply(image *vips.Image) error {
oi := &orientationInfo[orientation]
if oi.apply == nil {
return nil
}
// We want to stay sequential, so we copy memory here and execute
// all work in the pipeline so far.
if err := image.Write(); err != nil {
return err
}
if err := oi.apply(image); err != nil {
return err
}
_ = image.ImageRemove(vips.ExifOrientation)
return nil
}
示例5: MetadataImage
// MetadataImage returns Metadata from an Image. Format is always unset.
func MetadataImage(image *vips.Image) Metadata {
o := DetectOrientation(image)
w, h := o.Dimensions(image.Xsize(), image.Ysize())
if w <= 0 || h <= 0 {
panic("Invalid image dimensions.")
}
return Metadata{Width: w, Height: h, Orientation: o, HasAlpha: image.HasAlpha()}
}
示例6: minTransparency
func minTransparency(image *vips.Image) (float64, error) {
if !image.HasAlpha() {
return 1.0, nil
}
band, err := image.Copy()
if err != nil {
return 0, err
}
defer band.Close()
if err := band.ExtractBand(band.ImageGetBands()-1, 1); err != nil {
return 0, err
}
// If all pixels are at least 90% opaque, we can flatten.
min, err := band.Min()
if err != nil {
return 0, err
}
return min / band.MaxAlpha(), nil
}
示例7: jpegSave
func jpegSave(image *vips.Image, options SaveOptions) ([]byte, error) {
// JPEG interlace saves 2-3%, but incurs a few hundred bytes of
// overhead, requires buffering the image completely in RAM for
// encoding and decoding, and takes over 3x the CPU. This isn't
// usually beneficial on small images and is too expensive for large
// images.
pixels := image.Xsize() * image.Ysize()
interlace := pixels >= 200*200 && pixels <= 1024*1024
// Strip and optimize both save space, enable them.
return image.JpegsaveBuffer(true, options.Quality, true, interlace)
}
示例8: useLossless
func useLossless(image *vips.Image, options SaveOptions) bool {
if !options.Lossless {
return false
}
if !options.LossyIfPhoto {
return true
}
// Mobile devices start being unwilling to load >= 3 megapixel PNGs.
// Also we don't want to bother to edge detect on large images.
if image.Xsize()*image.Ysize() >= 3*1024*1024 {
return false
}
// Take a histogram of a Sobel edge detect of our image. What's the
// highest number of histogram values in a row that are more than 1%
// of the maximum value? Above 16 indicates a photo.
metric, err := image.PhotoMetric(0.01)
return err != nil || metric < 16
}
示例9: pngSave
func pngSave(image *vips.Image, options SaveOptions) ([]byte, error) {
// PNG interlace is larger; don't use it.
return image.PngsaveBuffer(true, options.Compression, false)
}
示例10: transverse
func transverse(image *vips.Image) error {
if err := image.Flip(vips.DirectionVertical); err != nil {
return err
}
return image.Rot(vips.Angle270)
}
示例11: rot270
func rot270(image *vips.Image) error {
return image.Rot(vips.Angle270)
}
示例12: rot180
func rot180(image *vips.Image) error {
return image.Rot(vips.Angle180)
}
示例13: rot90
func rot90(image *vips.Image) error {
return image.Rot(vips.Angle90)
}
示例14: flop
func flop(image *vips.Image) error {
return image.Flip(vips.DirectionHorizontal)
}
示例15: flip
func flip(image *vips.Image) error {
return image.Flip(vips.DirectionVertical)
}