- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两张图片。第二个是对第一个蒙版应用某种掩码的结果。我需要的是获得该蒙版并能够将其应用于其他图像。这是两张图片:normal , tattered .
如您所见,第二个有破烂的边缘,它们是透明的,不是白色的。(还有某种模糊发生,如果有办法我可以找出它到底是什么模糊,那就太好了,但这里真的没有必要)
我需要的是能够从第一个图像创建第二个图像。
从理论上讲,我应该创建一个 mask - 一个具有任何颜色的相同大小的图像,每个像素的透明度为 0 或 255,具体取决于上面第二张图像中相同像素的透明度值。然后我可以将任何输入图像的像素的 alpha 值设置为该掩码的 alpha 值。
但是,我不知道如何实际操作。我在 java 中使用 BufferedImage 尝试过它,但是它不起作用。当我尝试从所选像素的颜色中获取 Alpha 时,它始终为 255,即使对于应该透明的像素也是如此。我确实设法在 Processing 中获得了 alpha 值(它们实际上不仅仅是 0 或 255,中间有很多值),但是,当我尝试将此值应用于新图像并保存时,它保存为完全不透明的图像,当我加载它,alpha值都是255。
PImage mask = loadImage("some/path/1.png");
PImage img = loadImage("some/path/2.png");
img.loadPixels();
for (int x = 0; x < img.width; x++) {
for (int y = 0; y < img.height; y++) {
color maskColor = mask.get(x, y);
if (alpha(maskColor) < 255) {
color imgColor = img.get(x, y);
img.pixels[y*img.width + x] = color(red(imgColor), green(imgColor), blue(imgColor), alpha(maskColor));
}
}
}
img.updatePixels();
img.save("some/path/3.png");
最佳答案
您也可以将破烂的图像用作其他图像的 mask 。您只需要掩码中的 alpha 信息。
使用 BufferedImage 创建破烂边框的实现:
public class Test {
public static void main(String[] args) throws IOException {
BufferedImage mask = loadImage("e:\\mask.png");
BufferedImage img = loadImage("e:\\1.png");
int width = mask.getWidth();
int height = mask.getHeight();
BufferedImage processed = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int rgb = mask.getRGB(x,y);
int maskAlpha = alpha(rgb);
int imgColor = img.getRGB(x, y);
if (maskAlpha < 255) {
processed.setRGB(x,y,maskAlpha(imgColor, maskAlpha));
} else {
processed.setRGB(x,y,imgColor);
}
}
}
writeImage(processed, "e:\\2.png");
}
static BufferedImage loadImage(String imagePath) {
File file = new File(imagePath);
BufferedImage image = null;
try {
image = ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
}
return image;
}
static void writeImage(BufferedImage img,String filePath){
String format = filePath.substring(filePath.indexOf('.')+1);
//Get Picture Format
System.out.println(format);
try {
ImageIO.write(img,format,new File(filePath));
} catch (IOException e) {
e.printStackTrace();
}
}
static int maskAlpha(int rgb, int alpha) {
//strip alpha from original color
int color = (0x00ffffff&rgb);
return color + ((alpha)<<24);
}
static int alpha(int rgb) {
return (0xff&(rgb>>24));
}
static int red(int rgb) {
return (0xff&rgb);
}
static int green(int rgb) {
return (0xff&(rgb>>8));
}
static int blue(int rgb) {
return (0xff&(rgb>>16));
}
}
在这里BufferedImage.TYPE_4BYTE_ABGR意味着
Represents an image with 8-bit RGBA color components with the colorsBlue, Green, and Red stored in 3 bytes and 1 byte of alpha. The imagehas a ComponentColorModel with alpha. The color data in this image isconsidered not to be premultiplied with alpha. The byte data isinterleaved in a single byte array in the order A, B, G, R from lowerto higher byte addresses within each pixel.
也就是说颜色整数是32位的,在java中是按照abgr的顺序存储的,即alpha是前8位,r是后8位,所以可以得到argb值如下:
int r = (0xff&rgb);
int g = (0xff&(rgb>>8));
int b = (0xff&(rgb>>16));
int a = (0xff&(rgb>>24));
关于java - 在图像上查找透明像素并使相同像素在另一个图像上透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66943187/
我想禁用我的 UIButton,所以我调用: button.enabled = FALSE; 然而,这会使按钮变得透明,我可以看到它下面的元素。我不介意它改变颜色,我只是不希望它是透明的。 我尝试在
一个常规的 NSButton 如果被禁用,它似乎是透明的。 图像显示了一个样式为“push”的按钮 但是,我想禁用没有透明度的按钮。 我试图以编程方式将 alphaValue 设置为 1.0 但似乎
我正在尝试将Memoji从iPhone/Mac转换为具有透明背景的一系列png,以便我可以创建一个 Sprite 表。当按下空格键时,我可以清楚地看到它具有透明背景,但是在运行 ffmpeg -i s
我想创建一个具有部分透明背景的 View (舞台、窗口)。我有一张包含 Alpha channel 的图像 我在JavaFx中使用了这种场景,我必须将场景填充设置为空,并将根节点背景颜色设置为透明。我
我想创建一个具有部分透明背景的 View (舞台、窗口)。我有一张包含 Alpha channel 的图像 我在JavaFx中使用了这种场景,我必须将场景填充设置为空,并将根节点背景颜色设置为透明。我
我想将 JDesktopPane 设置为透明,并允许我单击下面的内容(例如桌面图标等)。内部框架应保持不透明,并且能够像当前一样在屏幕周围重新定位。有什么想法吗? package test1;
我知道我不是第一个提出此类问题的人,但我认为我的问题有点不同。 我在 MS Paint 上绘制了一个 png 图像,它是一个播放器,当我使用图形对象绘制图像时,我希望图像的背景是透明的。我尝试了一些带
我在 Mac 的 swift 项目中想使用单选按钮,但与我的背景颜色相同。如何将背景设置为不透明或更改背景颜色? 最佳答案 我找到了解决此问题的方法。将单选按钮的文本设置为“”,将 rb 添加到水平堆
我无法将我的相对布局设置为透明。我尝试了很多不同的方法,从类中自定义 View ,添加 android:background="@android:color/transparent"等。它们要么像下图
在 Storyboard中,我创建了一个 ![UINavigationController] 及其 Root View Controller UIViewcontroller。在工具栏中,我有一个分段
我必须让我的导航栏透明,我试过这段代码: self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIB
我注意到,如果我将 View 背景颜色设置为透明,则在 segue 过渡结束后背景颜色会变成黑色(我猜这是默认的“视口(viewport)”颜色)。 我可以改变吗?当然,我可以将颜色从透明更改为紫色,
嘿,有没有可能让滚动条“隐藏”,我不想使用 overflow-y: hidden就像背景:透明或类似的东西 最佳答案 Here您会找到有关如何仅使用 CSS 隐藏滚动条的说明。 在第二个example
默认情况下,背景显示为白色。是否可以通过任何方式将其背景颜色更改为透明..? (function() { var cx = '005519348941191855053:d0uziw7c
我正在尝试添加一个在 div 的左右边缘具有透明度/淡出的嵌入框阴影。我设法添加了一个普通的嵌入框阴影,但我不知道如何为边缘添加透明度。我该怎么做? 这是我正在努力实现的一个例子。 .contai
如何删除 周围的边框并使其背景透明? 最佳答案 试试这个: border: none; border-color: transparent; 关于html - 透明、无边框的文本输入,我们在Stac
是否可以使 JButton 透明(包括边框)而不是文本?我扩展了 swing 的 JButton 并覆盖了它: @Override public void paint(Graphics g) {
众所周知,要使QWidget/QOpenGLWidget半透明/透明,只需: widget->setAttribute(Qt::WA_TranslucentBackground) 但是,由于 QWin
我想知道如何在 Ubuntu 中为所有应用程序制作透明 Gnome 终端,我知道我们可以为桌面制作透明终端,而不是为所有应用程序制作透明终端。我用谷歌搜索了很多,但找不到任何解决方案。谁能告诉我该怎么
我想让 SWT Canvas 的背景透明,以便可以看到它后面的其他小部件。 我尝试将 alpha 设置为 0 并用矩形填充 Canvas ,并且还使用选项 SWT.NO_BACKGROUND 无济于事
我是一名优秀的程序员,十分优秀!