thumbnailator icon indicating copy to clipboard operation
thumbnailator copied to clipboard

color loss(turn red) when resizing some specific images

Open zdjray opened this issue 10 years ago • 3 comments

I use the following code to resize the jpg file: Thumbnails.of(new File("src/main/resources/images").listFiles()) .size(640, 480) .outputFormat("jpg") .toFiles(Rename.PREFIX_DOT_THUMBNAIL);

Images which have problem: original image resized image

zdjray avatar Aug 07 '15 08:08 zdjray

I did the following to remove Alpha channel on PNG files:

                // REMOVE Alpha channel. Source: http://stackoverflow.com/a/4388542
                Image img = Toolkit.getDefaultToolkit().createImage(fileBytes);
                PixelGrabber pg = new PixelGrabber(img, 0, 0, -1, -1, true);
                pg.grabPixels();
                int w = pg.getWidth();
                int h = pg.getHeight();
                DataBuffer buffer = new DataBufferInt((int[]) pg.getPixels(), pg.getWidth() * pg.getHeight());
                WritableRaster raster = Raster.createPackedRaster(buffer, w, h, w, RGB_MASKS, null);
                BufferedImage bi = new BufferedImage(RGB_OPAQUE, raster, false, null);

                // Resize
                BufferedImage bufferedImage1 = Thumbnails
                        .of(bi)
                        .width(thumbnailWidth)
                        .height(thumbnailHeight)
                        .keepAspectRatio(true)
                        .outputQuality(0.8)
                        .outputFormat(thumbnailType)
                        .asBufferedImage();

maksimu avatar Feb 17 '16 21:02 maksimu

@zdjray did you find any solution?

theoziran avatar Jun 06 '16 12:06 theoziran

`File in = new File("c:\img\in.jpg"); // 原图片 FileInputStream inStream = new FileInputStream(in); ImageWrapper imageWrapper = ImageReadHelper.read(inStream);//alibaba simpleimage lib Thumbnails.of(imageWrapper.getAsBufferedImage()).size(160, 160) .toFile("c:\img\out.jpg");

`you can use this code

yangxiaojun1 avatar Aug 17 '16 13:08 yangxiaojun1