I have an Image of 1000px X 1000px how do I go about and shrink it down to say 250x250 pixels and get an useable array of rgb colors values out of that
I tried using Java's Graphics2D but it turns out that doesn't actually reduce the amount of pixels
BufferedImage result = new BufferedImage(128, 128, BufferedImage.TYPE_INT_ARGB);
Graphics2D gra = result.createGraphics();
gra.drawImage(ImageIO.read(new File("")), 0, 0, 128, 128, null);
gra.dispose();
System.out.println(ImageIO.write(result, "png", new File("")));
The code that you posted works, you just need to properly specify the filename. I pointed it to a an image that was 3024x3024 and it saved as a 128x128 image.
BufferedImage result = new BufferedImage(128, 128, BufferedImage.TYPE_INT_ARGB);
Graphics2D gra = result.createGraphics();
gra.drawImage(ImageIO.read(new File("/tmp/3024x3024.jpeg")), 0, 0, 128, 128, null);
gra.dispose();
System.out.println(ImageIO.write(result, "png", new File("/tmp/128x128.png")));
Related
I want to simply convert an image to greyscale. But all I get is a completely black image. Why?
BufferedImage original = ImageIO.read(url);
BufferedImage image = new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
Graphics g = image.getGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
It has the correct width and height like the source image, but is completely black...
You are writing empty image to itself.
Change
g.drawImage(image, 0, 0, null);
to
g.drawImage(original, 0, 0, null);
I am working on converting colored image to black and white image. I am using BufferedImage for this with the type of TYPE_BYTE_BINARY. But output image is not converted correctly. For example, if image contains blue letters on black background, result image for this part is totaly black. Can anybody help me? My code is below.
//Invert the colormodel
byte[] map = new byte[] { (byte) (255), (byte) (0) };
IndexColorModel colorModel = new IndexColorModel(1, 2, map,
map, map);
BufferedImage bufferedImage = new BufferedImage(
img.getWidth(null), img.getHeight(null),
BufferedImage.TYPE_BYTE_BINARY, colorModel);
Graphics2D g2 = bufferedImage.createGraphics();
g2.drawImage(img, 0, 0, null);
g2.dispose();
Blue has a very low intensity, so blue (like RGB(0, 0, 255)) turning out black when converted to b/w with 50% threshold is to be expected. Try brightening the original image before converting to b/w, to increase the parts of the image coming out white.
You can use RescaleOp to brighten the image prior to conversion, or pass an instance along with your image to the drawImage method that takes a BufferedImageOp as parameter. Note that you can scale the R, G and B values independently.
BufferedImage bufferedImage= new BufferedImage(img.getWidth(null), img.getHeight(null);
ColorConvertOp op =
new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
op.filter(bufferedImage, bufferedImage);
check this link
I am having a problem with the Grapics2D API, I am trying to re-size an image but instead of getting the smaller image its showing a half white and half grey area. what am I doing wrong? I tried with and without render hints and it didn't work, or is there another approach that will give me a new Buffered Image object at the specified size?
//height = 400, width = 600, capture_rect = screen size
BufferedImage img = robot.createScreenCapture(CAPTURE_RECT);
BufferedImage resized = new BufferedImage(WIDTH, HEIGHT, img.getType());
Graphics2D g = resized.createGraphics();
g.drawImage(img, 0, 0, WIDTH, HEIGHT, 0, 0, img.getWidth(), img.getHeight(), null);
g.dispose();
setImage(resized);
See the article on Perils of Image.getScaledImage. It has some recommendations.
Here is some code it recommends:
private float xScaleFactor, yScaleFactor = ...;
private BufferedImage originalImage = ...;
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
int newW = (int)(originalImage.getWidth() * xScaleFactor);
int newH = (int)(originalImage.getHeight() * yScaleFactor);
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(originalImage, 0, 0, newW, newH, null);
}
It looks like you just need to specify the new width/height and it will do the scaling for you automatically.
Or maybe the problem is with the img.getType() method. You should be able to just using:
BufferedImage.TYPE_INT_RGB // (or ARGB)
See which works the best.
I had tired the following code to generate the thumb nail but when I place it on another image, the thumbnail has a white rectangle. How can I remove it using java graphics 2D?
Image image = javax.imageio.ImageIO.read(new File(originalFile));
BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setBackground(Color.WHITE);
graphics2D.setPaint(Color.WHITE);
graphics2D.fillRect(0, 0, thumbWidth, thumbHeight);
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
File file = new File(thumbnailFile);
if(javax.imageio.ImageIO.write(thumbImage, "png", file))
return file;
}
Even if i remove these three lines I am getting black rectangle.
graphics2D.setBackground(Color.WHITE);
graphics2D.setPaint(Color.WHITE);
graphics2D.fillRect(0, 0, thumbWidth, thumbHeight);
How to get this image as transparent? Please some one help me out.
Color transparent = new Color(0,0,0,0)
by setting Composite . here : AlphaComposite
// Create an image that supports arbitrary levels of transparency
Graphics2D g2d = (Graphics2D) image.getGraphics();
BufferedImage thumbImage = g2d.getDeviceConfiguration().createCompatibleImage(
thumbWidth, thumbHeight, Transparency.TRANSLUCENT);
..BufferedImage.TYPE_INT_RGB
That should be:
..BufferedImage.TYPE_INT_ARGB
I need to create a rectangular BufferedImage with a specified background color, draw some pattern on the background and save it to file. I don't know how to create the background.
I am using a nested loop:
BufferedImage b_img = ...
for every row
for every column
setRGB(r,g,b);
But it's very slow when the image is large.
How to set the color in a more efficient way?
Get the graphics object for the image, set the current paint to the desired colour, then call fillRect(0,0,width,height).
BufferedImage b_img = ...
Graphics2D graphics = b_img.createGraphics();
graphics.setPaint ( new Color ( r, g, b ) );
graphics.fillRect ( 0, 0, b_img.getWidth(), b_img.getHeight() );
Probably something like:
BufferedImage image = new BufferedImage(...);
Graphics2D g2d = image.createGraphics();
g2d.setColor(...);
g2d.fillRect(...);
Use this:
BufferedImage bi = new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB);
Graphics2D ig2 = bi.createGraphics();
ig2.setBackground(Color.WHITE);
ig2.clearRect(0, 0, width, height);
BufferedImage image = new BufferedImage(width,height, BufferedImage.TYPE_INT_ARGB);
int[]data=((DataBufferInt) image.getRaster().getDataBuffer()).getData();
Arrays.fill(data,color.getRGB());
For who want also to save the created image to a file, I have used previous answers and added the file saving part:
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
// Create the image
BufferedImage bi = new BufferedImage(80, 40, ColorSpace.TYPE_RGB);
Graphics2D graphics = bi.createGraphics();
// Fill the background with gray color
Color rgb = new Color(50, 50, 50);
graphics.setColor (rgb);
graphics.fillRect ( 0, 0, bi.getWidth(), bi.getHeight());
// Save the file in PNG format
File outFile = new File("output.png");
ImageIO.write(bi, "png", outFile);
You can also save the image in other formats like bmp, jpg, etc...