Discussion:
Correcting error of write Image to file
(too old to reply)
bH
2010-04-11 18:32:29 UTC
Permalink
Correcting error of write Image to file

Hi All,
I want to correct an error of ImageIO.write(...

1 error found:
File: C:\Documents and
Settings\bH\Desktop\OpaqueToTransparent.java [line: 39]
Error: C:\Documents and Settings\bH\Desktop\OpaqueToTransparent.
java:39: cannot find symbol
symbol : method write(java.awt.Image,java.lang.String,java.
io.File)
location: class javax.imageio.ImageIO

TIA
bH

//from http://www.rgagnon.com/javadetails/java-0265.html
//from http://www.exampledepot.com/egs/javax.imageio/

import java.awt.Image;
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.io.IOException;
import java.io.File;
import javax.imageio.ImageIO;

public class OpaqueToTransparent extends JFrame {
Image GifOrigWithBlueBackgrnd;
Image GifModifWithTransparentBackgrnd;

OpaqueToTransparent() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
init();
setVisible(true);
}

public void init() {
try {
// Read from a file
File file = new File("images/cosmob.gif");
GifOrigWithBlueBackgrnd = ImageIO.read(file);
GifModifWithTransparentBackgrnd =
Transparency.makeColorTransparent
(GifOrigWithBlueBackgrnd, new Color(0).blue);

} catch (IOException e) {}
//try {
File file = new File("images/cosmobX.gif");
//error on the next line
ImageIO.write(GifModifWithTransparentBackgrnd,
"gif", file);
}
catch (IOException e) {}

// Use a label to display the image
JFrame frame = new JFrame();
JLabel label1 = new JLabel(new
ImageIcon(GifOrigWithBlueBackgrnd));
JLabel label2 = new JLabel(new
ImageIcon(GifModifWithTransparentBackgrnd));
frame.getContentPane().add(label1, BorderLayout.CENTER);
frame.getContentPane().add(label2, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new OpaqueToTransparent();
}
});
}
}



import java.awt.*;
import java.awt.image.*;

public class Transparency {
public static Image makeColorTransparent
(Image im, final Color color) {
ImageFilter filter = new RGBImageFilter() {
// the color we are looking for...
// Alpha bits are set to opaque
public int markerRGB = color.getRGB() | 0xFF000000;

public final int filterRGB(int x, int y, int rgb) {
if ( ( rgb | 0xFF000000 ) == markerRGB ) {
// Mark the alpha bits as zero - transparent
return 0x00FFFFFF & rgb;
}
else {
// nothing to do
return rgb;
}
}
};

ImageProducer ip = new FilteredImageSource(im.getSource(),
filter);
return Toolkit.getDefaultToolkit().createImage(ip);
}
}
Joshua Cranmer
2010-04-11 19:09:22 UTC
Permalink
Post by bH
Correcting error of write Image to file
Hi All,
I want to correct an error of ImageIO.write(...
File: C:\Documents and
Settings\bH\Desktop\OpaqueToTransparent.java [line: 39]
Error: C:\Documents and Settings\bH\Desktop\OpaqueToTransparent.
java:39: cannot find symbol
symbol : method write(java.awt.Image,java.lang.String,java.
io.File)
location: class javax.imageio.ImageIO
Perhaps you want to write a BufferedImage, not an Image?
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
bH
2010-04-11 20:53:27 UTC
Permalink
Post by Joshua Cranmer
Post by bH
Correcting error of write Image to file
Hi All,
I want to correct an error of ImageIO.write(...
File: C:\Documents and
Settings\bH\Desktop\OpaqueToTransparent.java  [line: 39]
Error: C:\Documents and Settings\bH\Desktop\OpaqueToTransparent.
java:39: cannot find symbol
symbol  : method write(java.awt.Image,java.lang.String,java.
io.File)
location: class javax.imageio.ImageIO
Perhaps you want to write a BufferedImage, not an Image?
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
Hi Joshua,
Thanks for your hint, but a problem error
is shown below.

I made an attempt to replace what I have originally with
your suggestion but it produces the error found below.
I was following the tutorial from:
http://java.sun.com/docs/books/tutorial/2d/images/saveimage.html
bH

import java.awt.image.BufferedImage;
.......
BufferedImage bi = null;
.......
try {
BufferedImage bi = GifModifWithTransparentBackgrnd;
// retrieve image
File outputfile = new File("images/cosmobX.gif");
ImageIO.write(bi, "gif", outputfile);
} catch (IOException e) {}


1 error found:
File: C:\Documents and Settings\bH\Desktop\
OpaqueToTransparent.java [line: 40]
Error: C:\Documents and Settings\bH\OpaqueToTransparent.java:40:
incompatible types
found : java.awt.Image
required: java.awt.image.BufferedImage
Lew
2010-04-11 21:27:10 UTC
Permalink
Post by bH
Thanks for your hint, but a problem error
is shown below.
I made an attempt to replace what I have originally with
your suggestion but it produces the error found below.
http://java.sun.com/docs/books/tutorial/2d/images/saveimage.html
bH
import java.awt.image.BufferedImage;
.......
BufferedImage bi = null;
.......
try {
BufferedImage bi = GifModifWithTransparentBackgrnd;
// retrieve image
File outputfile = new File("images/cosmobX.gif");
ImageIO.write(bi, "gif", outputfile);
} catch (IOException e) {}
File: C:\Documents and Settings\bH\Desktop\
OpaqueToTransparent.java [line: 40]
incompatible types
found : java.awt.Image
required: java.awt.image.BufferedImage
Well, yeah.

There are two problems with that. First, and the one that caused the compiler
to choke, is that you completely disregarded the type incompatibility. Java is
not a language where you just toss things into the program and magically they
figure out what you mean. You have to follow the rules.

One of the most fundamental, basic rules in Java is that you cannot cast
references between incompatible types. One type must be a supertype of the
other. If you are casting to a supertype, you are automatically fine, because
every instance of a subtype /is-an/ instance of its supertype already. But
NOT every instance of a supertype belongs to a particular subtype.

To cast "down" from supertype to subtype, you have to explicitly cast the
change with a "(<type>)" cast operator. This "downcast" always risks a
'ClassCastException', so you must either prevent it or catch it.

You left out the cast operator, and completely neglected to handle a possible
'ClassCastException'.

The other problem is that your (misnamed) 'GifModifWithTransparentBackgrnd'
might never be set to the correct image. It's hard to say with the
fragmentary snippet you share.
--
Lew
Lew
2010-04-11 21:16:40 UTC
Permalink
Post by Joshua Cranmer
Post by bH
I want to correct an error of ImageIO.write(...
File: C:\Documents and
Settings\bH\Desktop\OpaqueToTransparent.java [line: 39]
Error: C:\Documents and Settings\bH\Desktop\OpaqueToTransparent.
java:39: cannot find symbol
symbol : method write(java.awt.Image,java.lang.String,java.
io.File)
location: class javax.imageio.ImageIO
Perhaps you want to write a BufferedImage, not an Image?
Indeed, quick perusal of the ImageIO Javadocs, an undertaking whose need is
clearly indicated by the cited error message,
<http://java.sun.com/javase/6/docs/api/javax/imageio/ImageIO.html>
reveals that there is no such method in that class, rather, the 'write()'
methods take a 'RenderedImage' parameter,
<http://java.sun.com/javase/6/docs/api/javax/imageio/ImageIO.html#write(java.awt.image.RenderedImage,
java.lang.String, java.io.File)>

Any time you see a compiler error that it cannot find a symbol, and that
symbol is a method call, suspect that you've got the argument types wrong.

Turns out, in fact, you are calling 'write()' with an argument
Post by Joshua Cranmer
Post by bH
public class OpaqueToTransparent extends JFrame {
Image GifOrigWithBlueBackgrnd;
Image GifModifWithTransparentBackgrnd;
While you're fixing that, change the variable names to conform with the Java
naming conventions, to whit, to start with a lower-case letter.

Also, don't eat exceptions. I expect you just didn't worry about them for
this simple example, but didn't plan to be so casual in the real world.
--
Lew
John B. Matthews
2010-04-12 04:49:09 UTC
Permalink
In article
Post by bH
Correcting error of write Image to file
Hi All,
I want to correct an error of ImageIO.write(...
File: C:\Documents and
Settings\bH\Desktop\OpaqueToTransparent.java [line: 39]
Error: C:\Documents and Settings\bH\Desktop\OpaqueToTransparent.
java:39: cannot find symbol
symbol : method write(java.awt.Image,java.lang.String,java.
io.File)
location: class javax.imageio.ImageIO
//from http://www.rgagnon.com/javadetails/java-0265.html
//from http://www.exampledepot.com/egs/javax.imageio/
[...]
Post by bH
public class OpaqueToTransparent extends JFrame {
...
// Use a label to display the image
JFrame frame = new JFrame();
If you extend JFrame, you don't need to create another one.
Post by bH
try {
File file = new File("images/cosmobX.gif");
//error on the next line
ImageIO.write(GifModifWithTransparentBackgrnd,
"gif", file);
}
You can render your transformed image in order to write it:

private void saveImage(Image image, String name) throws IOException {
BufferedImage bi = new BufferedImage(
image.getWidth(null), image.getHeight(null),
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();
g2d.drawImage(image, 0, 0, null);
ImageIO.write(bi, "gif", new File(name));
g2d.dispose();
}

I don't know enough about GIF format to say if that produces the result
you want. See this thread "Regarding transparent gif creation":

http://forums.sun.com/thread.jspa?threadID=5337139
Post by bH
catch (IOException e) {}
Don't swallow exceptions; at least write

ex.printStackTrace();

[...]
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
bH
2010-04-13 19:27:58 UTC
Permalink
Post by John B. Matthews
In article
Post by bH
Correcting error of write Image to file
Hi All,
I want to correct an error of ImageIO.write(...
File: C:\Documents and
Settings\bH\Desktop\OpaqueToTransparent.java  [line: 39]
Error: C:\Documents and Settings\bH\Desktop\OpaqueToTransparent.
java:39: cannot find symbol
symbol  : method write(java.awt.Image,java.lang.String,java.
io.File)
location: class javax.imageio.ImageIO
//fromhttp://www.rgagnon.com/javadetails/java-0265.html
//fromhttp://www.exampledepot.com/egs/javax.imageio/
[...]
Post by bH
public class OpaqueToTransparent extends JFrame {
...
   // Use a label to display the image
   JFrame frame = new JFrame();
If you extend JFrame, you don't need to create another one.
Post by bH
 try {
    File file = new File("images/cosmobX.gif");
      //error on the next line
      ImageIO.write(GifModifWithTransparentBackgrnd,
      "gif", file);
   }
private void saveImage(Image image, String name) throws IOException {
    BufferedImage bi = new BufferedImage(
        image.getWidth(null), image.getHeight(null),
        BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = bi.createGraphics();
    g2d.drawImage(image, 0, 0, null);
    ImageIO.write(bi, "gif", new File(name));
    g2d.dispose();
}
I don't know enough about GIF format to say if that produces the result
http://forums.sun.com/thread.jspa?threadID=5337139
Post by bH
    catch (IOException e) {}
Don't swallow exceptions; at least write
  ex.printStackTrace();
[...]
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>- Hide quoted text -
- Show quoted text -
Hi John,
Thanks so much for your "saveImage" help.
I have also corrected the "catch" errors you mentioned.
Within the program the gif transparency is
correct. However, the gif images do not display
with the correct oolors when saved.
The items listed in your link were observed.

I have written a similar program to use a
gif from a URL and filter out a green color in
a downloaded gif.

Thanks again.
bH
John B. Matthews
2010-04-13 21:04:05 UTC
Permalink
In article
Post by bH
Post by John B. Matthews
In article
I don't know enough about GIF format to say if that produces the
result you want. See this thread "Regarding transparent gif
http://forums.sun.com/thread.jspa?threadID=5337139
[...]
Hi John,
Thanks so much for your "saveImage" help.
I have also corrected the "catch" errors you mentioned.
Within the program the gif transparency is
correct. However, the gif images do not display
with the correct oolors when saved.
The items listed in your link were observed.
I have written a similar program to use a
gif from a URL and filter out a green color in
a downloaded gif.
It's a problem. For reference, the ImageJ menu command Image > Color >
Show LUT is a convenient way to examine the lookup tables before and
after:

<http://rsbweb.nih.gov/ij/>
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
Loading...