public static void scaleImageTo256kb(Path img) throws IOException {
        File imgFile = img.toFile();
        if (imgFile.length() <= BYTE) {
            return;
        }

        File newFile = Constants.PROGRAM_TMP_PATH.resolve(img.getFileName()).toFile();

        BufferedImage i = ImageIO.read(imgFile);
        if (i.getWidth() > MAXWIDTH) {
            BufferedImage j = new BufferedImage(MAXWIDTH, (int) (i.getHeight() * ((float) MAXWIDTH / i.getWidth())), i.getType());
            Graphics2D g2 = (Graphics2D) j.getGraphics();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            g2.drawImage(i, 0, 0, j.getWidth(), j.getHeight(), null);
            ImageIO.write(j, "jpg", imgFile);
        }

        float quality = 1f;
        float eps = 0.2f;
        int c = 0;

        Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg");
        ImageWriter writer = iter.next();
        ImageWriteParam iwp = writer.getDefaultWriteParam();
        iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        FileImageOutputStream output;
        IIOImage image;

        do {
            c++;
            FileUtil.deleteFile(newFile.toPath());
            iwp.setCompressionQuality(quality);
            output = new FileImageOutputStream(newFile);
            writer.setOutput(output);
            image = new IIOImage(ImageIO.read(imgFile), null, null);
            writer.write(null, image, iwp);
            writer.reset();
            output.flush();
            output.close();
            quality -= eps / c;

        } while ((newFile.length() > BYTE) && (quality > 0.1));

        FileUtil.deleteFile(img);
        FileUtil.moveFile(newFile.toPath(), img);
    }