Setting a specific color on a image transparent

I want to take a image that im drawing in the paint(graphics g) method, and set a certain color on that image transparent. so it blends with the background image properly.
any ideas would be appreciated.

Bird.gif
import java.awt.*;
import java.awt.image.*;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class TransparencyTest extends JPanel
    BufferedImage image;
    public TransparencyTest(BufferedImage orig)
        Color toErase = new Color(248, 248, 248, 255);
        image = eraseColor(convertImage(orig), toErase);
        setBackground(Color.pink);
    protected void paintComponent(Graphics g)
        super.paintComponent(g);
        int w = getWidth();
        int h = getHeight();
        int x = (w - image.getWidth())/2;
        int y = (h - image.getHeight())/2;
        g.drawImage(image, x, y, this);
    private BufferedImage convertImage(BufferedImage in)
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gd.getDefaultConfiguration();
        BufferedImage out = gc.createCompatibleImage(in.getWidth(), in.getHeight(),
                                                     Transparency.TRANSLUCENT);
        Graphics2D g2 = out.createGraphics();
        g2.drawImage(in, 0, 0, this);
        g2.dispose();
        return out;
    private BufferedImage eraseColor(BufferedImage source, Color color)
        int w = source.getWidth();
        int h = source.getHeight();
        int type = BufferedImage.TYPE_INT_ARGB;
        BufferedImage out = new BufferedImage(w, h, type);
        Graphics2D g2 = out.createGraphics();
        g2.setPaint(new Color(0,0,0,0));
        g2.fillRect(0,0,w,h);
        int target = color.getRGB();
        for(int j = 0; j < w*h; j++)
            int x = j % w;
            int y = j / w;
            if(source.getRGB(x, y) == target)
                source.setRGB(x, y, 0);
        g2.drawImage(source, 0, 0, this);
        g2.dispose();
        return out;
    public static void main(String[] args) throws IOException
        String path = "images/Bird.gif";
        BufferedImage bi = ImageIO.read(TransparencyTest.class.getResource(path));
        TransparencyTest test = new TransparencyTest(bi);
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(test);
        f.setSize(400,400);
        f.setLocation(200,200);
        f.setVisible(true);
}

Similar Messages

  • How to set canvas extension color for arbitrary image rotation?

    I'm using Photoshop CS4 v 11.0 with Windows XP.  When adjusting canvas size there's a menu for chosing the canvas extension color.  Where is the menu for chosing canvas extension color for arbitrary image rotation?
    Thanks.

    This will be the background colour selected in your toolbox.

  • Saving "Waveform graph" with cursors, set of different colors, in an image file

    Hello:
    I'm trying to save to a file, a waveform of a "waveform graph" that has cursors, which have different colors. When opening the file I can see that the waves are plotted defined color, but the grids and cursors are colored black.
    Someone can tell me what to do, so that the file is saved with the original colors of the "waveform graph"?
    Best Regards

    Probably this is happening because you are using the Invoke node Image Export, which captures an image with less information. However, if your application allows, using the Invoke Node Get Image, which captures your control, displays all the colors of your waveform, include cursors and grids colors.
    In this link bellow, you can see how use this Invoke Node:
    http://digital.ni.com/public.nsf/allkb/5AE4ADCBB65A6D8F86256FBF007A1AC1?OpenDocument
    I hope this helped you. Success in your application.
    Regards.
    Wesley Rocha
    Applications Engineer
    National Instruments Brazil
    www.ni.com/suporte
    Wesley Rocha
    Application Engineer
    National Instruments Brazil
    Visite a nossa comunidade em PORTUGUÊS!!!

  • How to set the (specific) color for a Chart Series in a Flash Diagramm

    Hello all,
    I wonder how I can set the color (of a bar) for a Chart Series in a Flash Diagramm.
    So far I found now set-screw for this.
    Can someone give me a hint please.
    Thanks in advance.
    Andre

    Hi,
    On the Chart Attributes page for the region, change the "Color Scheme" setting to Custom. Then, in the "Custom Colors" setting, enter the colours you want for all series, separated by a comma. For example:
    red,yellow,greenAndy

  • How do you keep the colors of an image true when you set it as a background?

    I have an image with lines in it and a black blackground. When I set it as a background :
    html {
        background: url(../Images/backgroundimage.jpg);
        background-repeat:auto;
        background-position:center top;
    the black is a couple shades lighter so when I place a <div> with an <img>that is also mainly black on top of it, the top image is much darker than the background. I have no other css tags on the div other than a margin. The only difference in the images is I saved the background from photoshop and the image in the div from illustrator but I dont think that should have effected anything. Does anybody know anything about this because I am seriously stumped.
    Thanks

    We have been here before…
    Firstly, Adobe' Photoshop has the ability to save a color profile with an image. That color profile will be consistent across all versions of Adobe software. Problem is, Adobe does not make web browsers (and that is probably a good thing).
    When you save images for websites, you need to go through the proper Photoshop or Illustrator dialog. You want to Save for Web & Other Devices. Why? because the applications will take the image out of CMYK color space (if it is in that color space) and make it RGB. They will also try to translate the colors in a sensible way. When you use this dialog, you will see various options for JPG, including ICC Profile. Turning this on or off will change how the colors get mapped, and some experimentation to get best results is a good idea.
    I have it turned off, presently.
    The image will be set up for 72 dots per inch automatically, and you can, then, resize the image to optimize it for the website you are working on. If you are trying to fill a hole for an image that is 350 pixels wide by 250 tall, don't save the image as 1125 wide by 1024 high. That will take a long time to download from your server to whatever device is displaying it. Your original image will not be changed and you can navigate your path to your defined website's images folder.
    This should help you immeasurably. I should mention that If I am trying to match a background exactly, I will tend to knock out that color from the image, save the image as a .PNG file with transparency (where that particular color is) and place the image on the background in question with the background color I am trying to match on the website.
    -Mark

  • Make a color in image transparent on the fly

    Hi I am trying to load an image and at runtime pick the white
    background of the image as transparent. Can any one point me to the
    flex/flash libraries that may help w/ this? basically I have 2
    images both of them have white background. I want to be able to
    drag and drop them so that the main objects of the images are side
    by side. But the problem is each image has thick white borders. so
    i figure if i can dynamically choose white color in the image to be
    transparent then I can overlap them and the white borders will be
    transparent and won't get in the way. Any expert can point me to
    the right direction?

    doug777, thanks for the reply. My issue is that the image I
    am trying to place next to each other is some images off the web
    i.e. I don't have control over the image transparent color. that's
    why I want to find a way to on the fly set which color in the
    images will be transparent. I was hoping flex has some library for
    that

  • I need to replace a color on my image with a specific color from another part of the image.  How?!

    I have spent over 2 hours trying to figure out how to replace a color on one part of my image with a specific color shown on another part of the image.  So far I have been able to go to "Replace Color" on my Adobe Photoshop version, but it seems that I can only change the hue and brightness of the color that it currently is.  I need to use a specific color so this does not help me.  Does anyone know how to complete the task I am referring to?
    Also, please keep in mind that I am using Adobe Photoshop 6.0, not a fancy cs version or anything.  So it's pretty old school...
    Any help/feedback is greatly appreciated!  Thank you.

    Hi brookehelene,
    You might consider making a selection the object and using a color fill layer set to the color blend mode instead of replace color.
    I used the same picture as kendallplant did and changed the color on the same flower by sampling the pink color from the flower to the right)
    (click on the screenshots below for larger views)
    1. Select>Color Range
       (i used color range because the dialog is somewhat similar to the replace color dialog, except one is making a selection instead of replacing a color)
       (there are many other ways to make the selection such as the magic wand, so if your more familar with the other selection tools you can use those instead to make your selection)
    2. Use the eyedroppers and fuzziness slider in the color range dialog to make the selection of the object.
       (in the screenshot below the white areas are selected and black areas are not)
       (your selection probably won't be perfect, but you can paint on the layer mask to refine the selection)
    3. After you press the okay in the color range dialog you'll see the selection (marching ants)
       At the bottom of the layers palette press the Create New Fill or Adjustment Layer icon
       to reveal a list and choose Solid Color.
       Then use the eyedropper to sample a color from your image and press ok.
       Change the Blend mode for the color fill layer to Color (top of the layers palette)
    4. As you can see a lot has been selected besides the flower.
        To refine the areas of color, paint on the layer mask with the paintbrush tool.
        Use white to add the color or black to subtract the color
        You can press the D key on the keyboard to get the default colors (black and white) in the toolbox
        and press the X key to switch between the two when painting on the layer mask.
    Even though the above takes longer you can easily change the color or modify the areas that you want to change the color on.
    To change the color you can double click on the color fill icon in the layers panel.
    You can also use other layer blend modes besides color such as hue, so you might try some of the others and see what they do.

  • How do you set the font color for a specific entire row inside a JTable?

    How do you set the font color for a specific entire row inside a JTable?
    I want to change the font color for only a couple of rows inside a JTable.
    I've seen some ways to possibly do this with an individual cell.
    Clarification on changing the font color in an individual cell would be helpful too if
    there is no easy way to do this for a row.

    hai,
    Try out with this piece of code.Create your table and assign the renderer to each column in the table.
    CellColorRenderer m_CellColorRenderer = new CellColorRenderer();
    for(int i=0;i<your_JTable.getColumnCount();i++)
    your_JTable.getColumnModel().getColumn(i).setCellRenderer(m_CellColorRenderer);
    class CellColorRenderer extends JLabel implements TableCellRenderer
    CellColorRenderer()     
    setOpaque(true);     
    setHorizontalAlignment(LEFT);
    setVerticalAlignment(CENTER);
    setBackground(Color.white);
    setForeground(Color.black);
    protected void setValue(Object value)
         setText((value == null) ? "" : value.toString());
    public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected, boolean hasFocus, int row,int column)
         if(isSelected == true)
              setForeground(Color.red);
         else
              setForeground(Color.black);
         setValue(value);
         return this;
    regards,
    bala

  • Colors in a specific region in an image

    hi all;
    i have an image and a list of 4 RGB colors.
    i need to check a specific region of the image (a small sub-image) to see if it contains any of the 4 colors i have.
    what my best option here?
    any help would be great.
    thanks in advance.

    This can be easily done with a StringBuffer:StringBuffer sb = new StringBuffer("leone24");
    sb.replace(5,7,"31");
    String newString = sb.toString();

  • How to set PS for the most reliable image previews and how to make sure the color spaces are the same across plateforms

    I've been having an ongoing problem with some of my images changing color/luminance values after merging or flattening an image. This seems to most often happen if I use one of the filters (like crosshatch or notepaper, not sharpen or blur). I can make a copy using command/option/shift + E which most often will look exactly like what's on screen.
    Some people have suggested that it's to do with resolution, PS showing me a low resolution image but I don't think so because the histogram changes -- sometimes in a big way, but most often those spikes in the luminance levels are shaved off. Also, when I have the image open at 100% and effect a merge/flatten, the color changes.
    I'd like to know if there's a way to have PS show me one for one what I'm working on while I'm working on it, or if there is a way to control the color profiles when I'm saving. My best guess at this point is that there is a color profile mismatch going on somewhere. I just don't know where to look for it.
    Typically I work in 16 bit, so my color space is set to ProPhoto RGB; my camera is set to the Adobe color space;
    color management policies are set to preserve color profiles;
    conversion options --> intent is set to relative colorimetric.

    But it is calibrated. I use Xrite 1 and update it every month (on the first).
    The colors can be really off. Here's an example.
    https://flic.kr/p/r6A1Zd (what I want) https://flic.kr/p/qc5QTD (not what I want)
    Also, I can't see how the bit depth will have any effect since the change in color happens in PS when I merge or flatten. It's 8 bit through the editing and 8 bit when I flatten it.
    Have you any suggestions on what I can do on the computer side?

  • Setting Specific Color for Outer Glows?

    Hi,
    I'm trying to set up an outer glow around some text (which is black text) and the glow keeps showing up as yellow. I want it to be white (like a cloud). Is there some way to get it to do this? Or am I stuck with yellow (which I don't understand where the yellow is coming from - I have the foreground set to black and the background set to white). I'm using Photoshop Elements 3.0 for Mac...
    Thanks so much for any help with this!
    - Jacki

    Jacki,
    Why not do something as simple as this:
    Add a blank layer above the text layer.
    Set the foreground color to white.
    Use the Brush tool with a soft brush to paint the glow, the Eraser tool with a soft brush to remove excess glow.
    If the glow is too bright lower the layer's Opacity. You can also try applying a Gaussian blur to the glow to soften and expand it a bit.
    http://www.pixentral.com/show.php?picture=1tv8M645F2Y8kZx7KVrqihk6mWGl5m

  • Can I change the color of an image to a specific color code?

    Since I was unable to do this in Photoshop, I was wondering if there was a way in Flash to simply change the color of an image to a speciifc color code.  The image is more or less a simple line with rounded corners.  It is only composed of 1 color.  Does anyone know if it can be done with Flash?

    Shouldn't be a problem to do it in Photoshop or Flash. Since you couldn't do it in PS, I'm guessing that there is more to it than you are making obvious here.
    But in Flash you would import it, make it into a movieclip or graphic. Then apply a tint from the color effect portion of the properties panel.

  • How can I change a color of an image without affecting the texture, but based on a pantone pick?

    Here is an image of a paintbrush stroke that shows the texture of the paint.  I want to change the color to a specific pantone green in order to match a specific color pallet.  Rather than just using hue saturation to "guess" at the color (no pantone number), is there a way I can actually set a base color without losing the texture?

    You can only use the picker values as a guide on an image with many tones.
    I presume you are going to print as cmyk, so find the mix of the target colour and make a 'solid colour' adjustment layer and choose that mix. Then set the adj layer properties to 'hue' which should do it.
    Otherwise, add a 'hue/saturation' layer (and click on colorize in the adj layer palette) and move the slider to suit.
    If you convert to greyscale, you could colorize the pic with the spot colour too.

  • ZOMG PLEASE HELP! - ColorMatrixFilter - How to set a SPECIFIC HLS to a greyscale png

    I am able to use the color matrix filter to change the pngs
    that I am loading dynamicly. Thing is, I can't seem to figure out
    how to apply a SPECIFIC hue, saturation and luminance.
    Basically I can successfully modify my grey scale png to all
    sorts of colors already. I need to be able to colorize them with,
    for example I would in photoshop put in H:212 S:36 L:-48 and it
    would turn out a great blue... how do I turn those numbers in to
    the proper color matrix to be applied?
    I have done a good bit of research on hls/rgb and also tried
    using this adobe sample app to put in numbers similar to above,
    expecting to get a matrix that would apply those transformations.
    Using the resultant matrix, the image stays at a very low
    saturation even when using positive luminance values. I could guess
    at it, but I have a bunch of PMS colors I am going to have to
    match... SO at least being able to convert HLS or RGB into an
    applicable color matrix with predictable results is needed.
    Here is a sample of what I am trying to do, but just using
    CSS and JavaScript.
    http://www.customtiedye.com/colors.htm
    - Since it is using pngs you can't use internet explorer to view it
    properly. But even if you have only ie, you can see that changing
    the selection in the lower drop down box shows you a different
    color of the shirt. All of these images were colorized in photoshop
    from the same source image I am using in the flash application. I
    need to translate the HLS numbers I used in photoshop into
    colormatrices to apply to the same image.
    Here is what I have so far for the flash application.
    http://www.customtiedye.com/dyeshop.html
    - If you don't see the flash app, try in IE, I have not bothered
    tweakin anything as far as html because I am stuck at this issue.
    The buttons are not marked right, start at the top and go
    down, you'll see what I mean. I can apply the color matrixes and
    the effect is working, but HOW DO I GET THE SPECIFIC MATRICIES FOR
    SPECIFIC COLORS?
    Please help :o) Thank you in advance.

    Thanks for the reply. I've seen every example out there I
    believe. From satori color to quasimondo. The problem is not making
    it work. The problem is how to make it show an exact color.
    How do I use colormatrix to colorize a greyscale image to a
    SPECIFIC color. It's a great tool and I can change my greyscales to
    all sorts of colors, but how do I make them a SPECIFIC color, like
    with setrgb I could just set the rgb, if I have hls values how do I
    convert that into a useable colormatrix to modify a greyscale.
    For instance: (please read)
    In photoshop I would adjust hls and put in say: 212/60/60 for
    a nice blue, apply it and my grey scale is now a nice blue.
    In flash, how do I use that info to change the grey scale
    image to the same color?

  • How to change the color of an image....

    I have a logo that I use that has a
    white background with black lettering. I would like to use that logo sometimes with white lettering. How can I change that using PSE 7?
    I am creating postcards with a picture on the left and a color block on the right that compliments that picture on the left. I would also like to implement a gradient of some sort from the picture to the color block. Any tips on how to do that as well? I would like it to go from left to right....

    We'll use the image you posted.
    1. Use the black and white icon in your layers palette to make a pink solid color fill adjustment layer.
    Since the mask is white the entire image will be covered by the pink color.
    2. Now, we are going to hide the entire effect (pink solid color fill adjustment layer) by filling it with black. With the solid color fill layer highlighted in the layers palette, you can use the paint bucket or just invert the white mask by pressing Ctrl + I.
    Here we see that the entire pink solid color fill layer is hidden. We are now looking at your background layer.
    We are going to use a different gradient this time and make it easier to understand. I think I used linear last time. I think it will be easier to understand going this route for this particular situation.
    3. Set your foreground color chip to white then select the gradient tool. With the gradient tool selected, open the gradient editor and select the white to transparent preset. (To open gradient editor, double click the gradient window in your options bar.
    Above is what you need to look for in the options bar to double click to gain access to the gradient editor.
    The second preset should be foreground to transparent. If white to transparent isn't available, you need to change your tool box's foreground color to white as the gradient's foreground color will be whatever is selected as the foreground color in the tool box.
    4. Here is a screen shot of my gradient tool settings that I used.
    I pulled from right to left because I do not have reverse checked. If I had reverse checked, I would have pulled left to right. Notice the gradient style I have selected and that the tool is in normal blend mode and that transparency has been enabled.
    I should also mention that I pulled from pretty close to where the two images join and only pulled maybe a centimeter worth of drag.
    Yellow line shows start and stop point of tool drag. *You can drag more than once if the gradient is too thin at the join.
    The reason you want to hold in the shift key as you drag the gradient is because this gives you an even pull...makes a straight line. Without using the shift key you can get a wonky uneven join.

Maybe you are looking for

  • Firefox will not download on my neoware m1000 windows xp embedded laptop

    Firefox will not correctly install on my neoware m100 notebook, os is windows xp embedded and citrix. i have tried downloading and received an error corrupted file message or it just ends at extracting files == This happened == Every time Firefox ope

  • Software for greeting cards

    I have no idea where to post my question, so I'll begin here. Is there software available for Macs that will enable me to print my own greeting cards, e.g., birthday cards, thank you notes, get well cards, etc.? Thanks! eMac   Mac OS X (10.3.9)  

  • Will apple still replace my phone even with a cracked screen?

    The power button on my Iphone 4S was stuck. I was taking it to the Apple store to see what they could do and on the way I cracked my screen. Will apple still replace my phone because the power button was broke first?

  • Quicktime Pro and .ts MPEG2 Files

    I posted a similar question in the AppleTV forum, but thought it may be better suited here. I have some HD .ts files that I'd like to convert for use with my AppleTV when it arrives. If I buy Quicktime Pro, would it be able to convert the file into a

  • Error message "The URL could not be opened" comes up when trying to update iTunes

    iTunes needs an update, but Internet Explorer does not open the URL. A browser window opens, the computer is online yet the URL will not open.