-1

(NOTE: I can not use any library which directly resize the image, I want to know the core logic of resizing )

I have an grayscale image with dimensions 256*256 . I want to modify it and create following three images a) image with dimensions 128*128 b) image with dimensions 64*64 c) image with dimensions 32*32.

pseudo code

File fi = new File("E:\\input.raw");
byte[] fileContent = Files.readAllBytes(fi.toPath());

File fo= new File("E:\\output.raw");
FileOutputStream stream = new FileOutputStream(fo);

int i=0;
  for(;i<fileContent.length;i++){
  //dividing by 2 to create image with dimensions 128*128
  fileContent[i]= (byte) ((fileContent[i])/2);
    stream.write(fileContent[i]);
  }
    stream.close();

Above Code does not work.It is creating image with 256*256 dimensions. Due to some reasons I am not allowed to use any library that directly reduces the dimensions. I want to know how can I convert 256*256 image to 128*128 dimensions ?

sar
  • 1,277
  • 3
  • 21
  • 48
  • "Due to some reasons". You mean this is some sort of interview or homework question. While homework questions are allowed here, you need to show your work first. You haven't written basically anything. If this is an interview question, you're not a very good fit for the place. – Kayaman Sep 25 '17 at 06:33
  • @Kayaman I have added code snippet of my work in question. – sar Sep 25 '17 at 06:36
  • 1
    Why would you think dividing the byte values somehow affects the *size* of the image? That doesn't make sense in any way. – Kayaman Sep 25 '17 at 06:41
  • just a side note - if you want to resize from 256x256 to 128x128 you'll have to divide by 4 (hence you'^re taking of squares and thus need to divide by sq(2)=4) – Martin Frank Sep 25 '17 at 06:43
  • You basically take one pixel (e.g. the top left) from each square of 4 pixels. For better results filter the original image first to remove high frequency content. – Henry Sep 25 '17 at 06:46

2 Answers2

-1

Hope this will help

/**
 * scale image
 * 
 * @param sbi image to scale
 * @param imageType type of image
 * @param dWidth width of destination image
 * @param dHeight height of destination image
 * @param fWidth x-factor for transformation / scaling
 * @param fHeight y-factor for transformation / scaling
 * @return scaled image
 */
public static BufferedImage scale(BufferedImage sbi, int imageType, int dWidth, int dHeight, double fWidth, double fHeight) {
    BufferedImage dbi = null;
    if(sbi != null) {
        dbi = new BufferedImage(dWidth, dHeight, imageType);
        Graphics2D g = dbi.createGraphics();
        AffineTransform at = AffineTransform.getScaleInstance(fWidth, fHeight);
        g.drawRenderedImage(sbi, at);
    }
    return dbi;
}
Jeredriq Demas
  • 616
  • 1
  • 9
  • 36
-1

Using ImageIO and Graphics2D it can be done as follows

BufferedImage originalImage = ImageIO.read(new File("c:\\image\\test.jpg"));
int[] dims = {128, 64, 32};

for(int dim : dims) {
    BufferedImage resizedImage = new BufferedImage(dim, dim, type);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, dim, dim, null);
    g.dispose();
    ImageIO.write(resizeImageJpg, "jpg", new File("c:\\image\\test_" + dim + "x" + dim + ".jpg"));
}
Saqib Javed
  • 177
  • 4
  • 17
  • *"I am not allowed to use any library that directly reduces the dimensions."* – Filburt Sep 26 '17 at 11:31
  • [ImageIO](https://docs.oracle.com/javase/7/docs/api/javax/imageio/ImageIO.html) and [Graphics2D](https://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html) are java internal libraries not external – Saqib Javed Sep 26 '17 at 12:26
  • Please re-read my quote - it does not refer to **external libraries** but libraries that **directly reduce the dimensions**. It is obvious that the OP was tasked with a rather academic request just to show he can solve the problem. The OP even edited his post to specifically point this out. – Filburt Sep 26 '17 at 14:16
  • check image processing [algorithms](https://en.wikipedia.org/wiki/Image_scaling#Algorithms) then – Saqib Javed Sep 27 '17 at 06:59