
正文
java中抠图代码 java绘制图片代码
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
请问用Java 怎样实现抠图功能。比如图片是一块石头放在一张纸上,怎样
1、用ps打开两张图片。
2、在工具里选择“移动工具”,按住鼠标左键把第二个图片拖动到第一个图片里。由于第二张的像素有点大,所以会把原来的图片覆盖住的,通过鼠标稍微移动一下。
3、按ctrl+t(自由变换快捷键),图片的四周出现了可以调节的横线,按住shift拖动图片的一个角可以进行等比例缩放,这张图太大了,所以等比例缩小一点。调整为合适的大小,放到合适的地方。调整完毕,按enter键确认。
4、在右下角的图层面板里点击第三个按钮(添加矢量蒙板),为第二个图层添加一个蒙板。
5、可以看到在“工具”里,前景色和背景色默认修改为了白色和黑色。
6、然后选择工具里的“渐变”工具。可以看到,上方工具栏出现了渐变的一些设置。因为前景色为白色,背景色为黑色,所以默认是白色到黑色的渐变条。后面分别设置为径向渐变,正常模式,百分之百不透明度,反向不打勾。
7、点击白色到黑色的渐变条,进入渐变编辑器。
8、把左侧下方的白色滑块拖到中间,可以在下方的位置处直接填写百分之50。
9、把鼠标放在左侧的滑动条下方,会出现“点按可添加色标”。
10、点击一下,出现一个新的色块,为白色。把它拖动到最左边,可以直接填写百分之0。
11、选中最左边的白色色块,点击一下下面的颜色后面的白色,弹出“选择色标颜色”的窗口。在里面选择纯黑色。点击“确定”。这样就把白色改成了黑色。渐变色变成了黑-白-黑。点击“确定”。
12、可以看到上面确实变成了黑色-白色-黑色渐变。
13、一只手按住shift键,一只手按住鼠标左键在图片上拉出一条直线(按住shift键是保证水平)。
14、松开手,蒙板就起作用了,这是利用了蒙板状态下,黑色隐藏,白色显示的特点。
15、然后稍加修饰。选择工具里的“矩形选框工具”,选中要裁剪的部分。
16、点击“图像”,选择“裁剪”。
17、图片被裁剪,裁剪完成后按ctrl+d取消选中状态,或者可以点击右键,选择“取消”。
18、、这样就实现了两张图片的合成。利用蒙板和渐变色合成的方式的好处是第二个图片可以保留一部分的背景,有一种融入的感觉。如果采用抠图合并的方法,一般是给人物换背景图,技术要求比较高。
相关问答
Q1: 6行Python代码教你3秒批量抠图,抠头发丝也非常简单
抠图是日常办公中经常会遇到的问题,某乎上就有这样一个问题:
里边的tag 全是设计相关,但是我学了些python,怎么用Python的方法来解决呢?
首先我找了本回答下的高赞回答,利用的ps,魔棒、磁性套索、钢笔等等。
我把他用到的几张示例图保存了下来。然后放在一个文件夹里。此为示例图。
然后用python代码执行,3秒钟左右图就扣好了。
比较难的一张,扣头发丝,效果也非常不错,我们来看一下原图和对比图,基本的小细节都处理得不错。
当然,处理更多的图片也是几秒钟的事。
下面讲一下原理。
本例是利用的Python来实现的。调用的是RemoveBg 这个库。
RemoveBg 是国外的一个抠图网站。本身是完全免费的,但是只能一张一张上传-抠图-下载,如果你有大批量的图片需要处理,就比较低效,也不符合本题的主旨,高效。
好在,本着极客精神,本站为我们提供了API,可以直接调用网站的抠图方法,直接在本地抠图。
首先,我们需要注册一下网站,用自己常用的邮箱就可以了。
然后点击工具和API。
往下拉,找到API Docs。
然后点击API key 就可以找到属于你的API了。
到这里,要先记住你的API key。
然后你,可以在你喜欢的位置新建一个文件夹。
如果你有python基础的话,就可以轻易读懂这个:
没有的话,可以直接复制,然后路径地址。
然后遍历这个文件夹中的文件,然后再用这个工具处理,就可以了
总的代码就这几行,总之来说是非常简单。
前提是你需要安装python环境,然后再安装RemoveBg这个包就可以了。
Q2: 用python抠图(背景透明化)
matplotlib读取RGB图片的时候,往往把图片转化为四通道图片,其中第四个通道是α通道,可以决定图像的透明度。
如果把图片的前景的透明度设定为1,背景的透明度设定为0,那么就算去掉了背景了。
原图。
代码。
出图。
有部分白色背景由于不够白,被保留了下来。
换图。
观察可知,背景是深浅不一的蓝色。
深色参数是a[0,0],浅色区域比较多,也不容易确定位置。
用如下代码抠图,太狠!
再修改代码。
if a[i,j,2]1.5*a[i,j,0] and a[i,j,2]1.2*a[i,j,1]:
不准抠去黑色部分。经过逐步调整颜色阈值,得到下面的效果。
上面代码如下。
Q3: 抠图删除弹出对话框怎么设置
如果您是在使用抠图工具进行图片编辑时,希望对话框弹出的行为有一些自定义设置,可以尝试以下操作:
1. 打开抠图工具,在界面上方找到“选项”或“设置”按钮,点击打开设置界面。
2. 在设置界面中,查找和“弹出对话框”、“删除操作”或类似的选项。
3. 根据您的需求更改相应选项。例如,您可以选择让弹出对话框自动关闭,或者手动关闭;您也可以设置删除操作时是否需要确认。
4. 完成设置后,点击保存或应用按钮,使更改生效。
需要注意的是,不同的抠图工具可能提供不同的设置选项,因此具体设置方法可能会略有不同。此外,如果您使用的是某种特定品牌或版本的软件,可以尝试查看该软件的官方文档或帮助文件,寻找更详细的设置说明。
Q4: 怎么用java实现抠图功能?
package com.thinkgem.jeesite.modules.file.utils;
import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
public class ImageUtils {
/**
* 图片去白色的背景,并裁切
*
* @param image 图片
* @param range 范围 1-255 越大 容错越高 去掉的背景越多
* @return 图片
* @throws Exception 异常
*/
public static byte[] transferAlpha(Image image, InputStream in, int range) throws Exception {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
ImageIcon imageIcon = new ImageIcon(image);
BufferedImage bufferedImage = new BufferedImage(imageIcon
.getIconWidth(), imageIcon.getIconHeight(),
BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics();
g2D.drawImage(imageIcon.getImage(), 0, 0, imageIcon
.getImageObserver());
int alpha = 0;
int minX = bufferedImage.getWidth();
int minY = bufferedImage.getHeight();
int maxX = 0;
int maxY = 0;
for (int j1 = bufferedImage.getMinY(); j1 bufferedImage
.getHeight(); j1++) {
for (int j2 = bufferedImage.getMinX(); j2 bufferedImage
.getWidth(); j2++) {
int rgb = bufferedImage.getRGB(j2, j1);
int R = (rgb 0xff0000) 16;
int G = (rgb 0xff00) 8;
int B = (rgb 0xff);
if (((255 - R) range) ((255 - G) range) ((255 - B) range)) { //去除白色背景;
rgb = ((alpha + 1) 24) | (rgb 0x00ffffff);
} else {
minX = minX = j2 ? minX : j2;
minY = minY = j1 ? minY : j1;
maxX = maxX = j2 ? maxX : j2;
maxY = maxY = j1 ? maxY : j1;
}
bufferedImage.setRGB(j2, j1, rgb);
}
}
int width = maxX - minX;
int height = maxY - minY;
BufferedImage sub = bufferedImage.getSubimage(minX, minY, width, height);
int degree = getDegree(in);
sub = rotateImage(sub,degree);
ImageIO.write(sub, "png", byteArrayOutputStream);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
return byteArrayOutputStream.toByteArray();
}
/**
* 图片旋转
* @param bufferedimage bufferedimage
* @param degree 旋转的角度
* @return BufferedImage
*/
public static BufferedImage rotateImage(final BufferedImage bufferedimage,
final int degree) {
int w = bufferedimage.getWidth();
int h = bufferedimage.getHeight();
Rectangle rect_des = CalcRotatedSize(new Rectangle(new Dimension(
w, h)), degree);
int type = bufferedimage.getColorModel().getTransparency();
BufferedImage img;
Graphics2D graphics2d;
(graphics2d = (img = new BufferedImage(rect_des.width, rect_des.height, type))
.createGraphics()).setRenderingHint(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.translate((rect_des.width - w) / 2,
(rect_des.height - h) / 2);
graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2);
graphics2d.drawImage(bufferedimage, 0, 0, null);
graphics2d.dispose();
return img;
}
/**
* 计算旋转后图像的大小
* @param src Rectangle
* @param degree 旋转的角度
* @return Rectangle
*/
public static Rectangle CalcRotatedSize(Rectangle src, int degree) {
if (degree = 90) {
if(degree / 90 % 2 == 1){
int temp = src.height;
src.height = src.width;
src.width = temp;
}
degree = degree % 90;
}
double r = Math.sqrt(src.height * src.height + src.width * src.width) / 2;
double len = 2 * Math.sin(Math.toRadians(degree) / 2) * r;
double angel_alpha = (Math.PI - Math.toRadians(degree)) / 2;
double angel_dalta_width = Math.atan((double) src.height / src.width);
double angel_dalta_height = Math.atan((double) src.width / src.height);
int len_dalta_width = (int) (len * Math.cos(Math.PI - angel_alpha
- angel_dalta_width));
int len_dalta_height = (int) (len * Math.cos(Math.PI - angel_alpha
- angel_dalta_height));
int des_width = src.width + len_dalta_width * 2;
int des_height = src.height + len_dalta_height * 2;
return new java.awt.Rectangle(new Dimension(des_width, des_height));
}
/**
* byte[] ------BufferedImage
*
* @param byteImage byteImage
* @return return
* @throws IOException IOException
*/
public static BufferedImage ByteToBufferedImage(byte[] byteImage) throws IOException {
ByteArrayInputStream in = new ByteArrayInputStream(byteImage);
return ImageIO.read(in);
}
/**
* 获取照片信息的旋转角度
* @param inputStream 照片的路径
* @return 角度
*/
public static int getDegree(InputStream inputStream) {
try {
Metadata metadata = ImageMetadataReader.readMetadata(new BufferedInputStream(inputStream),true);
for (Directory directory : metadata.getDirectories()) {
for (Tag tag : directory.getTags()) {
if ("Orientation".equals(tag.getTagName())) {
return turn(getOrientation(tag.getDescription()));
}
}
}
} catch (ImageProcessingException e) {
e.printStackTrace();
return 0;
} catch (IOException e) {
e.printStackTrace();
return 0;
}
return 0;
}
/**
* 获取旋转的角度
* @param orientation orientation
* @return 旋转的角度
*/
public static int turn(int orientation) {
Integer turn = 360;
if (orientation == 0 || orientation == 1) {
turn = 360;
} else if (orientation == 3) {
turn = 180;
} else if (orientation == 6) {
turn = 90;
} else if (orientation == 8) {
turn = 270;
}
return turn;
}
/**
* 根据图片自带的旋转的信息 获取 orientation
* @param orientation orientation
* @return orientation
*/
public static int getOrientation(String orientation) {
int tag = 0;
if ("Top, left side (Horizontal / normal)".equalsIgnoreCase(orientation)) {
tag = 1;
} else if ("Top, right side (Mirror horizontal)".equalsIgnoreCase(orientation)) {
tag = 2;
} else if ("Bottom, right side (Rotate 180)".equalsIgnoreCase(orientation)) {
tag = 3;
} else if ("Bottom, left side (Mirror vertical)".equalsIgnoreCase(orientation)) {
tag = 4;
} else if ("Left side, top (Mirror horizontal and rotate 270 CW)".equalsIgnoreCase(orientation)) {
tag = 5;
} else if ("Right side, top (Rotate 90 CW)".equalsIgnoreCase(orientation)) {
tag = 6;
} else if ("Right side, bottom (Mirror horizontal and rotate 90 CW)".equalsIgnoreCase(orientation)) {
tag = 7;
} else if ("Left side, bottom (Rotate 270 CW)".equalsIgnoreCase(orientation)) {
tag = 8;
}
return tag;
}
}
Q5: 如何用Java 实现抠图?
选中你想抠图的图片,右键,选中编辑选项,进入"画图"程序
2、按住"Ctrl+A",选中图片,"Ctrl+C"复制图片
java中抠图代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java绘制图片代码、java中抠图代码的信息别忘了在本站进行查找喔。







