-1

I am creating image having caption, but before applying text caption on images, the size of image is 1 mb but after that it increase so much approx 4-5 mb.

File f = new File("E:/picTest/08062016_110130_0.jpg");

final BufferedImage image = ImageIO.read(f);

String s ="hello";
Graphics g = image.getGraphics();
g.setFont(g.getFont().deriveFont(30f));
g.setColor(Color.black);
g.drawString(s, 100, 100);
g.dispose();

ImageIO.write(image, "png", new File("E:/picTest/test2.jpg"));

So, i want my image size not increase and if it increase, minimum size of image increse.

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
007
  • 115
  • 1
  • 13
  • 2
    You appear to be changing image format from jpg to png, although confusingly the file you're writing to still has the jpg extension. I would expect chance of a significant file size change if you change format, especially if going from a lossy file format to a lossless type. – Hovercraft Full Of Eels Jun 08 '16 at 14:05
  • See this question and answer: [PNG vs. GIF vs. JPEG vs. SVG - When best to use?](http://stackoverflow.com/questions/2336522/png-vs-gif-vs-jpeg-vs-svg-when-best-to-use), for more on the differences between formats. – Hovercraft Full Of Eels Jun 08 '16 at 14:08

1 Answers1

0

I can't find a decent close duplicate, so I'll post this as an answer. Your problem is due to you not just writing a new image but that you're also converting file formats from a lossly format, jpg, to a lossless format, png, when you write the new file, and for this reason your file is much bigger since png files are almost always bigger than their jpg equivalent. If you care most about image quality over file size, then continue what you're doing, although you should change the extension of the file that you're writing to, to png. Otherwise if file size is of paramount importance, then write your output file as a jpg by changing

ImageIO.write(image, "png", new File("E:/picTest/test2.jpg"));

to:

ImageIO.write(image, "jpg", new File("E:/picTest/test2.jpg"));

Again for more on the difference between these two file formats, and other formats as well, please look at PNG vs. GIF vs. JPEG vs. SVG - When best to use?

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373