位图转换 - 创建透明的 +来自黑白源的黑色图像

发布于 2024-10-01 15:28:56 字数 1491 浏览 1 评论 0原文

我有一大堆 jpg 文件需要在项目中使用,但由于某种原因无法更改。每个文件都是相似的(手写),白BG上的黑笔。但是,我需要在 Flash 项目中针对非白色背景使用这些资源,因此我尝试进行一些客户端处理,以使用 getPixel 和 setPixel32 消除背景。

我当前使用的代码当前使用线性比较,虽然它有效,但结果低于预期,因为灰色阴影在混合中丢失了。除了调整参数以使事情看起来正确之外,我还感觉我计算 RGBa 值的方法很弱。

谁能推荐比我下面使用的更好的解决方案?非常感谢!

private function transparify(data:BitmapData) : Bitmap {

    // Create a new BitmapData with transparency to return
    var newData:BitmapData = new BitmapData(data.width, data.height, true);
    var orig_color:uint;
    var alpha:Number;
    var percent:Number;

    // Iterate through each pixel using nested for loop
    for(var x:int = 0; x < data.width; x++){
        for (var y:int = 0; y < data.height; y++){

            orig_color = data.getPixel(x,y);

            // percent is the opacity percentage, white should be 0, 
            // black would be 1, greys somewhere in the middle
            percent = (0xFFFFFF - orig_color)/0xFFFFFF;

            // To get the alpha value, I multiply 256 possible values by 
            // my percentage, which gets multiplied by 0xFFFFFF to fit in the right
            // value for the alpha channel
            alpha = Math.round(( percent )*256)*0xFFFFFF;

// Adding the alpha value to the original color should give me the same 
// color with an alpha channel added
            var newCol = orig_color+alpha;
            newData.setPixel32(x,y,newCol);
        }
    }

    var newImg:Bitmap = new Bitmap(newData);
    return newImg;
} 

I have a whole bunch of jpg files that I need to use in a project, that for one reason or another cannot be altered. Each file is similar (handwriting), black pen on white BG. However I need to use these assets against a non-white background in my flash project, so I'm trying to do some client-side processing to get rid of the backgrounds using getPixel and setPixel32.

The code I am currently using currently uses a linear comparison, and while it works, the results are less than expected, as the shades of grey are getting lost in the mix. Moreso than just tweaking my parameters to get things looking proper, I get the feeling that my method for computing the RGBa value is weak.

Can anyone recommend a better solution than what I'm using below? Much appreciated!

private function transparify(data:BitmapData) : Bitmap {

    // Create a new BitmapData with transparency to return
    var newData:BitmapData = new BitmapData(data.width, data.height, true);
    var orig_color:uint;
    var alpha:Number;
    var percent:Number;

    // Iterate through each pixel using nested for loop
    for(var x:int = 0; x < data.width; x++){
        for (var y:int = 0; y < data.height; y++){

            orig_color = data.getPixel(x,y);

            // percent is the opacity percentage, white should be 0, 
            // black would be 1, greys somewhere in the middle
            percent = (0xFFFFFF - orig_color)/0xFFFFFF;

            // To get the alpha value, I multiply 256 possible values by 
            // my percentage, which gets multiplied by 0xFFFFFF to fit in the right
            // value for the alpha channel
            alpha = Math.round(( percent )*256)*0xFFFFFF;

// Adding the alpha value to the original color should give me the same 
// color with an alpha channel added
            var newCol = orig_color+alpha;
            newData.setPixel32(x,y,newCol);
        }
    }

    var newImg:Bitmap = new Bitmap(newData);
    return newImg;
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百合的盛世恋 2024-10-08 15:28:56

由于它是白色背景,混合模式可能会给你更好的结果。

Since it's a white background, blendMode may give you a better result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文