0% found this document useful (0 votes)
116 views

How To Split An Image Into Chunks - Java Imageio: Parallel Computing

This document discusses splitting a single image into multiple chunks that can be processed in parallel. It provides code to: 1) Read an input image file and split it into a specified number of rows and columns, creating "chunk" images of equal size. 2) Write each chunk image out to a new file for parallel processing. 3) Reassemble the chunks back into a single output image after processing.

Uploaded by

Daniel Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views

How To Split An Image Into Chunks - Java Imageio: Parallel Computing

This document discusses splitting a single image into multiple chunks that can be processed in parallel. It provides code to: 1) Read an input image file and split it into a specified number of rows and columns, creating "chunk" images of equal size. 2) Write each chunk image out to a new file for parallel processing. 3) Reassemble the chunks back into a single output image after processing.

Uploaded by

Daniel Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

int rows = 2; //we assume the no.

of rows and cols are known and each chunk


has equal width and height
int cols = 2;
int chunks = rows * cols;

int chunkWidth, chunkHeight;


int type;
//fetching image files
File[] imgFiles = new File[chunks];
for (int i = 0; i < chunks; i++) {
imgFiles[i] = new File("archi" + i + ".jpg");
}

//creating a bufferd image array from image files


BufferedImage[] buffImages = new BufferedImage[chunks];
for (int i = 0; i < chunks; i++) {
buffImages[i] = ImageIO.read(imgFiles[i]);
}
type = buffImages[0].getType();
chunkWidth = buffImages[0].getWidth();
chunkHeight = buffImages[0].getHeight();

//Initializing the final image


BufferedImage finalImg = new BufferedImage(chunkWidth*cols,
chunkHeight*rows, type);

int num = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
finalImg.createGraphics().drawImage(buffImages[num],
chunkWidth * j, chunkHeight * i, null);
num++;
}
}
System.out.println("Image concatenated.....");
ImageIO.write(finalImg, "jpeg", new File("finalImg.jpg"));

How to Split an Image into Chunks - Java ImageIO


Image splitting and concatenating and other image manipulation techniques are important in parallel computing. Say we are

receiving small chunks of an Image which is being manipulated parallely. In such scenarios, we need to concatenate those chunks

together and vice versa.

There is a pretty straight forward way to split an image using Java imageio package. Say you need to split following image into

several chunks (you should decide the no. of rows and columns needed).
Now I am going to split this image into 16 chunks (4 rows and 4 columns). I have shown the code snippet below.

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.awt.*;

public class ImageSplitTest {


public static void main(String[] args) throws IOException {

File file = new File("bear.jpg"); // I have bear.jpg in my working


directory
FileInputStream fis = new FileInputStream(file);
BufferedImage image = ImageIO.read(fis); //reading the image file

int rows = 4; //You should decide the values for rows and cols
variables
int cols = 4;
int chunks = rows * cols;

int chunkWidth = image.getWidth() / cols; // determines the chunk


width and height
int chunkHeight = image.getHeight() / rows;
int count = 0;
BufferedImage imgs[] = new BufferedImage[chunks]; //Image array to
hold image chunks
for (int x = 0; x < rows; x++) {
for (int y = 0; y < cols; y++) {
//Initialize the image array with image chunks
imgs[count] = new BufferedImage(chunkWidth, chunkHeight,
image.getType());

// draws the image chunk


Graphics2D gr = imgs[count++].createGraphics();
gr.drawImage(image, 0, 0, chunkWidth, chunkHeight, chunkWidth
* y, chunkHeight * x, chunkWidth * y + chunkWidth, chunkHeight * x +
chunkHeight, null);
gr.dispose();
}
}
System.out.println("Splitting done");
//writing mini images into image files
for (int i = 0; i < imgs.length; i++) {
ImageIO.write(imgs[i], "jpg", new File("img" + i + ".jpg"));
}
System.out.println("Mini images created");
}
}

Parameter list of "drawImage()" (See line 28)

-- image - The source image


-- 0, 0 - X,Y coordinates of the 1st corner of destination image
-- chunkWidth, chunkHeight - X,Y coordinates of the opposite corner of destination image
-- chunkWidth * y, chunkHeight * x - X,Y coordinates of the 1st corner of source image block
-- chunkWidth * y + chunkWidth, chunkHeight * x + chunkHeight - X,Y coordinates of

You might also like