java code to select 10 pixels randomly from a 32 x 32 pixel source jpg , dont alter the…
- User
java code to select 10 pixels randomly from a 32 x 32 pixel source jpg , dont alter the source jpg physically but use each of the RGB values by altering them randomly by 50% of their original values and then use these 10 RGB values to alter 10 randomly selected pixels of a 32 x 32 target-jpg also by 50% of their original values
- Assistant
import java.io.; import javax.imageio.; import java.awt.image.BufferedImage; import java.util.Random;
public class ImageProcessing {
public static void main(String[] args) throws IOException { File sourceFile = new File("source.jpg"); File targetFile = new File("target.jpg");
BufferedImage sourceImage = ImageIO.read(sourceFile); BufferedImage targetImage = ImageIO.read(targetFile); int width = sourceImage.getWidth(); int height = sourceImage.getHeight(); int[] selectedRGB = new int[10]; int[] selectedX = new int[10]; int[] selectedY = new int[10]; // Select 10 random pixels from sourceImage Random rand = new Random(); for(int i=0; i<10; i++){ int x = rand.nextInt(width); int y = rand.nextInt(height); selectedX[i] = x; selectedY[i] = y; int rgb = sourceImage.getRGB(x,y); selectedRGB[i] = rgb; } // Modify selectedRGB values by 50% for(int i=0; i<10; i++){ int rgb = selectedRGB[i]; int red = (rgb >> 16) & 0xFF; int green = (rgb >> 8) & 0xFF; int blue = (rgb & 0xFF); red = (int)(red * 0.5 + red * rand.nextDouble() * 0.5); green = (int)(green * 0.5 + green * rand.nextDouble() * 0.5); blue = (int)(blue * 0.5 + blue * rand.nextDouble() * 0.5); rgb = (red << 16) | (green << 8) | blue; selectedRGB[i] = rgb; } // Modify targetImage with modified RGB values for(int i=0; i<10; i++){ int x = selectedX[i]; int y = selectedY[i]; int rgb = selectedRGB[i]; targetImage.setRGB(x,y,rgb); } // Save modified targetImage to file ImageIO.write(targetImage, "jpg", targetFile); - User
continue code
- Assistant
Sure, here's the rest of the code:
import java.io.; import javax.imageio.; import java.awt.image.BufferedImage; import java.util.Random;
public class ImageProcessing {
public static void main(String[] args) throws IOException { File sourceFile = new File("source.jpg"); File targetFile = new File("target.jpg");
BufferedImage sourceImage = ImageIO.read(sourceFile); BufferedImage targetImage = ImageIO.read(targetFile); int width = sourceImage.getWidth(); int height = sourceImage.getHeight(); int[] selectedRGB = new int[10]; int[] selectedX = new int[10]; int[] selectedY = new int[10]; // Select 10 random pixels from sourceImage Random rand = new Random(); for(int i=0; i<10; i++){ int x = rand.nextInt(width); int y = rand.nextInt(height); selectedX[i] = x; selectedY[i] = y; int rgb = sourceImage.getRGB(x,y); selectedRGB[i] = rgb; } // Modify selectedRGB values by 50% for(int i=0; i<10; i++){ int rgb = selectedRGB[i]; int red = (rgb >> 16) & 0xFF; int green = (rgb >> 8) & 0xFF; int blue = (rgb & 0xFF); red = (int)(red * 0.5 + red * rand.nextDouble() * 0.5); green = (int)(green * 0.5 + green * rand.nextDouble() * 0.5); blue = (int)(blue * 0.5 + blue * rand.nextDouble() * 0.5); rgb = (red << 16) | (green << 8) | blue; selectedRGB[i] = rgb; } // Modify targetImage with modified RGB values for(int i=0; i<10; i++){ int x = selectedX[i]; int y = selectedY[i]; int rgb = selectedRGB[i]; targetImage.setRGB(x,y,rgb); } // Save modified targetImage to file ImageIO.write(targetImage, "jpg", targetFile); - User
java code for gui to load source and target jpg . both images should be visible on gui
- Assistant