处理。图像有圆角

问题描述 投票:0回答:2

我正在绘制一个图像的一部分,但是我想把它的圆角应用到图像上。我找不到任何方法可以做到这一点。

在draw()方法中。

img_section = img.get(gaze_x, gaze_y, gaze_size_x, gaze_size_y);
image(img_section, gaze_x, gaze_y);
processing
2个回答
1
投票

你可以 拷贝 图像,然后手动设置角像素,使用 设置() 功能。

你可以直接 画圆 如果图像将被放置在一个单一颜色的背景上,只需画一个与图像相同颜色的圆形矩形。

或者你可以想出一个图像蒙版,然后把它画在你的图像上面。


0
投票

package utils

import (
    "ddkt365-poster/library/log"
    "image"
    "image/color"
    "math"
)

// Settable Settable
type Settable interface {
    Set(x, y int, c color.Color)
}

var empty = color.RGBA{255, 255, 255, 0}

// Convert Convert
func Convert(m *image.Image, rate float64) {
    b := (*m).Bounds()
    w, h := b.Dx(), b.Dy()
    r := (float64(min(w, h)) / 2) * rate
    log.Error("bounds:%v", r)
    sm, ok := (*m).(Settable)
    if !ok {
        // Check if image is YCbCr format.
        ym, ok := (*m).(*image.YCbCr)
        if !ok {
            log.Error("errInvalidFormat")
            return
        }
        *m = yCbCrToRGBA(ym)
        sm = (*m).(Settable)
    }
    // Parallelize?
    for y := 0.0; y <= r; y++ {
        l := math.Round(r - math.Sqrt(2*y*r-y*y))
        for x := 0; x <= int(l); x++ {
            sm.Set(x-1, int(y)-1, empty)
        }
        for x := 0; x <= int(l); x++ {
            sm.Set(w-x, int(y)-1, empty)
        }
        for x := 0; x <= int(l); x++ {
            sm.Set(x-1, h-int(y), empty)
        }
        for x := 0; x <= int(l); x++ {
            sm.Set(w-x, h-int(y), empty)
        }
    }
}

func min(a, b int) int {
    if a < b {
        return a
    }
    return b
}

func yCbCrToRGBA(m image.Image) image.Image {
    b := m.Bounds()
    nm := image.NewRGBA(b)
    for y := 0; y < b.Dy(); y++ {
        for x := 0; x < b.Dx(); x++ {
            nm.Set(x, y, m.At(x, y))
        }
    }
    return nm
}

            // Image with rounded corners (Go image/draw package)
            if i.BorderRadius > 0 {
                utils.Convert(&img, (float64(i.BorderRadius) / 100))
            }
            draw.Draw(canvs, img.Bounds().Add(image.Pt(i.X, i.Y)), img, image.ZP, draw.Over)
© www.soinside.com 2019 - 2024. All rights reserved.