【问题标题】:Processing: Image with rounded corners处理:带圆角的图像
【发布时间】:2020-05-20 22:44:17
【问题描述】:

我正在绘制图像的一部分,但我想对其应用圆角。我找不到任何方法。

在draw()方法中:

img_section = img.get(gaze_x, gaze_y, gaze_size_x, gaze_size_y);
image(img_section, gaze_x, gaze_y);

【问题讨论】:

    标签: processing


    【解决方案1】:

    您可以copy 图像,然后使用set() 函数手动设置角像素。

    您可以只在图像周围draw a rounded rectangle - 如果图像将放置在单一颜色的背景上,只需绘制一个与图像颜色相同的圆角矩形。

    或者您可以想出一个图像蒙版并将其绘制在您的图像之上。

    【讨论】:

    • 我最终使用了图像蒙版 - 感谢您的帮助!
    【解决方案2】:
    
    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)
    

    【讨论】:

    • 您介意编辑您的答案以提供一些关于您正在做什么以及关键方面的解释吗?
    猜你喜欢
    • 2014-11-10
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多