0

I am converting a input file (PDF,TIFF) to Output (TIFF) file by using PDFBox (PDF to BufferedImage) and using twelve monkeys for converting Buffered image to Tiff file by resizing using Imagewriter with IIOImage. File is converting but losing an quality on the image.And after changed the imagetype BufferedImage.TYPE_BYTE_GRAY to BufferedImage.TYPE_BYTE_BINARY my text highlighters on the file lost.

Below is the code used. How to convert the image without losing quality?

I am converting the image file size 1648*2338 with 200 dpi and i wanted to set photometric interpretation to min_is_white but not able to achieve my problem.

File inputFile = new File(inputImagePath);
BufferedImage inputImage = ImageIO.read(inputFile);

final int imageType = BufferedImage.TYPE_BYTE_BINARY;

// creates output image
BufferedImage outputImage = new BufferedImage(scaledWidth, scaledHeight,imageType);

// scales the input image to the output image
Graphics2D g2d = outputImage.createGraphics();
g2d.drawImage(inputImage, 0, 0, scaledWidth, scaledHeight, null);
g2d.dispose();

// writes to output file                    
final List<Entry> entries =new ArrayList<Entry>();
entries.add(new TIFFEntry(TIFF.TAG_X_RESOLUTION, new Rational(200)));
entries.add(new TIFFEntry(TIFF.TAG_Y_RESOLUTION, new Rational(200)));
entries.add(new TIFFEntry(TIFF.TAG_PHOTOMETRIC_INTERPRETATION, TIFF.TYPE_SHORT, 0));
final IIOMetadata tiffImageMetadata =new TIFFImageMetadata(entries);

ImageWriter writer = ImageIO.getImageWritersByFormatName("tiff").next();
FileImageOutputStream fio = new FileImageOutputStream(new File(outputImagePath));
ImageWriteParam params = writer.getDefaultWriteParam();
params.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
fio.setByteOrder(ByteOrder.LITTLE_ENDIAN);
IIOMetadata metadata = writer.getDefaultImageMetadata(new ImageTypeSpecifier(outputImage), params);


writer.setOutput(fio);

IIOImage iioimage = new IIOImage(outputImage, null, tiffImageMetadata);

writer.write(null, iioimage, params);
fio.close();
writer.dispose();
sathish p
  • 1
  • 3
  • This is a bit confusing, you're setting resolution of 300 and 200 ?! Why not render the image with 200 directly in PDFBox? And yes it will lose something when converting to b/w. Yellow will likely be white. I don't see where you are setting the compression, ImageIOUtils.writeImage() will set the best one (G4 for b/w images). – Tilman Hausherr Mar 16 '20 at 13:01
  • Removed unused TiffUtil codes and is there any other possibilities to convert file from pdf to tiff or tiff to tiff. We are not using ImageIOUtils because it doesn't support to convert the file to tiff,it supports only jpeg or png file format.Correct me if i am wrong.Used Jai with PDFBox nothing worked. Java version used 1.8 – sathish p Mar 17 '20 at 02:46
  • 1
    ImageIOUtils does support TIFF. You need to have a TIFF writer in your class path, either from twelvemonkeys or jai, see https://pdfbox.apache.org/1.8/dependencies.html / https://pdfbox.apache.org/2.0/dependencies.html . – Tilman Hausherr Mar 17 '20 at 06:08
  • First of all, the "quality loss" happens in your own conversion. As Tilman says, using only B/W, you *will* lose colors... This shouldn't be a surprise. Second, you don't set compression in your code (you only set compression mode, which allows you to set it later). Did you mean to specify `"CCITT T.6"` (aka Group 4) through `params`? Finally, Java2D standard image types are "black is zero". If you want to output "white is zero", you need to [use a color model that has white in index 0](https://github.com/haraldk/TwelveMonkeys/issues/533#issuecomment-598137390). – Harald K Mar 24 '20 at 17:12
  • 1
    On the other hand, if you want to keep your highlights, you need to either use index color model (`PhotometricInterpretation: 3/Palette` in TIFF speak) or true color. Or perhaps keeping the highlight black with inverted text, if that is an option or you need to have B/W. – Harald K Mar 24 '20 at 17:15

0 Answers0