剪出文字形状的图像

时间:2011-06-09 15:10:44

标签: java image image-processing

我需要在另一张图片中剪切出文字形状的图像。我认为它最好显示在图像中。

这是一张猫的照片:

Photo of a nice cat

这是我希望删除的文字:

Text to cut out of the cat photo

结果图像是:

Resulting cut-out of the cat photo

文本图像将始终为黑色且背景为透明,并且生成的剪切图案也应具有透明背景。两个输入图像的大小也相同。

6 个答案:

答案 0 :(得分:33)

Cat text

import java.awt.*;
import java.awt.font.*;
import java.awt.image.BufferedImage;
import java.awt.geom.Rectangle2D;
import javax.imageio.ImageIO;
import java.net.URL;
import java.io.File;

class PictureText {

    public static void main(String[] args) throws Exception {
        URL url = new URL("http://i.stack.imgur.com/Nqf3H.jpg");
        BufferedImage originalImage = ImageIO.read(url);
        final BufferedImage textImage = new BufferedImage(
            originalImage.getWidth(),
            originalImage.getHeight(),
            BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = textImage.createGraphics();
        FontRenderContext frc = g.getFontRenderContext();
        Font font = new Font(Font.SANS_SERIF, Font.BOLD, 250);
        GlyphVector gv = font.createGlyphVector(frc, "Cat");
        Rectangle2D box = gv.getVisualBounds();
        int xOff = 25+(int)-box.getX();
        int yOff = 80+(int)-box.getY();
        Shape shape = gv.getOutline(xOff,yOff);
        g.setClip(shape);
        g.drawImage(originalImage,0,0,null);
        g.setClip(null);
        g.setStroke(new BasicStroke(2f));
        g.setColor(Color.BLACK);
        g.setRenderingHint(
            RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        g.draw(shape);

        g.dispose();

        File file = new File("cat-text.png");
        ImageIO.write(textImage,"png",file);
        Desktop.getDesktop().open(file);
    }
}

答案 1 :(得分:21)

创建一个新的BufferedImage并迭代单词cat的所有像素,如果它们是黑色,则将cat-image像素复制到新图像。

以下是一些代码:(最终工作代码,支持反别名

public static BufferedImage textEffect(BufferedImage image, BufferedImage text) {
    if (image.getWidth() != text.getWidth() ||
        image.getHeight() != text.getHeight())
    {
        throw new IllegalArgumentException("Dimensions are not the same!");
    }
    BufferedImage img = new BufferedImage(image.getWidth(),
                                          image.getHeight(),
                                          BufferedImage.TYPE_INT_ARGB_PRE);

    for (int y = 0; y < image.getHeight(); ++y) {
        for (int x = 0; x < image.getWidth(); ++x) {
           int textPixel = text.getRGB(x, y);
           int textAlpha = (textPixel & 0xFF000000);
           int sourceRGB = image.getRGB(x, y);
           int newAlpha = (int) (((textAlpha >> 24) * (sourceRGB >> 24)) / 255d);
           int imgPixel = (newAlpha << 24) |  (sourceRGB & 0x00FFFFFF);
           int rgb = imgPixel | textAlpha;
           img.setRGB(x, y, rgb);

        }
    }
    return img;
}

答案 2 :(得分:15)

使用GlyphVector。使用Font

public GlyphVector layoutGlyphVector(FontRenderContext frc,
                                         char[] text,
                                         int start,
                                         int limit,
                                         int flags) {

您可以通过公共抽象形状Shape

从字形向量中获取大纲getOutline()

将大纲Shape指定为Graphics实例的剪辑。

在图形上绘制图像。

只会填充剪裁的形状。

答案 3 :(得分:8)

这里没有java,但所需的图像操作很容易理解。在Mathematica:

enter image description here

答案 4 :(得分:2)

使用Marvin Framework

,只需几行源代码即可在Java中完成

enter image description here

源代码:

public class CutAndFill {
    public static void main(String[] args) {
        // 1. Load images
        MarvinImage catImage = MarvinImageIO.loadImage("./res/catImage.jpg");
        MarvinImage catText = MarvinImageIO.loadImage("./res/catText.png");

        // 2. Load plug-in, set parameters and process de image
        MarvinImagePlugin combine = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.combine.combineByMask");
        combine.setAttribute("combinationImage", catImage);
        combine.setAttribute("colorMask", Color.black);
        combine.process(catText.clone(), catText);

        // 3. Save the output image.
        MarvinImageIO.saveImage(catText, "./res/catOut.jpg");
    }
}

答案 5 :(得分:-1)

首先,使“Cat”图像的黑色部分透明。有关此问题,请参阅here。然后,composite在你最喜欢的猫(我的是Sheeba)的照片上面的图像。

关于这个的好处是你可以制作一次透明文本图像,保存它,然后将它应用到Sheeba的所有家人和朋友!