Skip to content

Commit 21b523e

Browse files
authored
Projects
1 parent e54dca9 commit 21b523e

3 files changed

+271
-0
lines changed

ImageEditor.java

+271
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
import java.awt.image.BufferedImage;
2+
import java.io.File;
3+
import java.io.IOException;
4+
//import java.util.Arrays;
5+
import java.util.Scanner;
6+
import javax.imageio.ImageIO;
7+
import java.awt.*;
8+
public class ImageEditor {
9+
public static BufferedImage convertToGrayScale(BufferedImage inputImage) {
10+
int height = inputImage.getHeight();
11+
int width = inputImage.getWidth();
12+
BufferedImage outputImage = new BufferedImage(width, height,BufferedImage.TYPE_BYTE_GRAY);
13+
for (int i = 0; i < height; i++) {
14+
for (int j = 0; j < width; j++) {
15+
outputImage.setRGB(j, i, inputImage.getRGB(j, i));
16+
}
17+
}
18+
return outputImage;
19+
}
20+
public static BufferedImage colorInvert(BufferedImage inputImage) {
21+
int height = inputImage.getHeight();
22+
int width = inputImage.getWidth();
23+
BufferedImage outputImage = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
24+
for (int i = 0; i < height; i++) {
25+
for (int j = 0; j < width; j++) {
26+
Color pixel = new Color(inputImage.getRGB(j, i));
27+
int red = pixel.getRed();
28+
int blue = pixel.getBlue();
29+
int green = pixel.getGreen();
30+
int finalred = 255 - red;
31+
int finalblue = 255 -blue;
32+
int finalgreen = 255- green;
33+
Color newPixel = new Color (finalred,finalgreen,finalblue);
34+
outputImage.setRGB(j, i, newPixel.getRGB());
35+
}
36+
}
37+
return outputImage;
38+
}
39+
public static BufferedImage rotateImage(BufferedImage inputImage) {
40+
int height = inputImage.getHeight();
41+
int width = inputImage.getWidth();
42+
BufferedImage outputImage = new BufferedImage(height, width,BufferedImage.TYPE_INT_RGB);
43+
int r=0;
44+
for (int i = 0; i < width; i++) {
45+
int c=0;
46+
for (int j = height-1; j>=0; j--) {
47+
Color pixel = new Color(inputImage.getRGB(i, j));
48+
outputImage.setRGB(c, r, pixel.getRGB());
49+
c++;
50+
}r++;
51+
}return outputImage;
52+
}
53+
public static BufferedImage horizontalSwitch(BufferedImage inputImage) {
54+
int height = inputImage.getHeight();
55+
int width = inputImage.getWidth();
56+
BufferedImage outputImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
57+
int r=0;
58+
for (int i=0; i<height; i++) {
59+
int c=0;
60+
for (int j = width-1; j>=0; j--) {
61+
Color pixel = new Color(inputImage.getRGB(j, i));
62+
outputImage.setRGB(c, r, pixel.getRGB());
63+
c++;
64+
}r++;
65+
}return outputImage;
66+
}
67+
public static BufferedImage verticalSwitch(BufferedImage inputImage) {
68+
int height = inputImage.getHeight();
69+
int width = inputImage.getWidth();
70+
BufferedImage outputImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
71+
int r=0;
72+
for (int i=height-1; i>=0; i--) {
73+
int c=0;
74+
for (int j = 0; j<width; j++) {
75+
Color pixel = new Color(inputImage.getRGB(j, i));
76+
outputImage.setRGB(c, r, pixel.getRGB());
77+
c++;
78+
}r++;
79+
}return outputImage;
80+
}
81+
public static void blurSquare(BufferedImage inputImage, BufferedImage outputImage, int start_row, int end_row, int start_col, int end_col){
82+
int r=0,b=0,g=0,c=0;
83+
for(int i=start_row;i< end_row;i++){
84+
for(int j=start_col;j<end_col;j++){
85+
Color pixel = new Color (inputImage.getRGB(j, i));
86+
r+=pixel.getRed();
87+
b+=pixel.getBlue();
88+
g+=pixel.getGreen();
89+
c++;
90+
}
91+
}
92+
Color newPixel = new Color(r/c,b/c,g/c);
93+
for(int k=start_row;k<end_row;k++){
94+
for(int u=start_col;u<end_col;u++){
95+
outputImage.setRGB(u, k, newPixel.getRGB());
96+
}
97+
}
98+
}
99+
public static BufferedImage blurImage(BufferedImage inputImage, int blur) {
100+
int height = inputImage.getHeight();
101+
int width = inputImage.getWidth();
102+
BufferedImage outputImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
103+
for(int row=0;row+blur<height;row+=blur){
104+
for(int col=0;col+blur<width;col+=blur){
105+
blurSquare(inputImage,outputImage,row,row+blur,col,col+blur);
106+
}
107+
}if ((height*width)%(blur*blur)!=0){
108+
109+
}
110+
return outputImage;
111+
}
112+
113+
public static BufferedImage changeBrightness(BufferedImage inputImage,int increase) {
114+
int height = inputImage.getHeight();
115+
int width = inputImage.getWidth();
116+
BufferedImage outputImage = new BufferedImage(width, height,BufferedImage.TYPE_3BYTE_BGR);
117+
for (int i = 0; i < height; i++) {
118+
for (int j = 0; j < width; j++) {
119+
Color pixel = new Color(inputImage.getRGB(j, i));
120+
int red = pixel.getRed();
121+
int blue = pixel.getBlue();
122+
int green = pixel.getGreen();
123+
red = red + (increase * red / 100);
124+
blue = blue + (increase * blue) / 100;
125+
green = green + (increase * green) / 100;
126+
if (red > 255) red = 255;
127+
if (blue > 255) blue = 255;
128+
if (green > 255) green = 255;
129+
if (red < 0) red = 0;
130+
if (blue < 0) blue = 0;
131+
if (green < 0) green = 0;
132+
Color newPixel = new Color(red, green, blue);
133+
outputImage.setRGB(j, i, newPixel.getRGB());
134+
}
135+
}return outputImage;
136+
}
137+
public static void main(String args[]) {
138+
System.out.println();
139+
System.out.println("WELCOME!!");
140+
Scanner sc = new Scanner (System.in);
141+
System.out.print("Enter the path of the image ::");
142+
String s = sc.nextLine();
143+
boolean ch = true;
144+
try {
145+
while(ch){
146+
System.out.println();
147+
System.out.print("Enter the path to save the image ::");
148+
String p;
149+
p = sc.next();
150+
String o = "";
151+
if(p.length()>4){
152+
for (int i=p.length()-4;i<p.length();i++){
153+
o+=p.charAt(i);
154+
}
155+
if (o.equals(".jpg")){
156+
p=p+"";
157+
}else{
158+
p=p+".jpg";
159+
}
160+
}else{
161+
p=p+".jpg";
162+
}
163+
System.out.println();
164+
System.out.println("Enter 1 to convert the image to grayscale.");
165+
System.out.println("Enter 2 to change the brightness.");
166+
System.out.println("Enter 3 to rotate the image clockwise.");
167+
System.out.println("Enter 4 to rotate the image anti-clockwise.");
168+
System.out.println("Enter 5 to horizontally flip the image.");
169+
System.out.println("Enter 6 to vertically flip the image.");
170+
System.out.println("Enter 7 to blur the image.");
171+
System.out.println("Enter 8 to invert the image.");
172+
System.out.println("Enter 0 to exit.");
173+
System.out.print("Enter the choice::");
174+
int choice = sc.nextInt();
175+
File inputFile = new File(s);
176+
BufferedImage inputImage = ImageIO.read(inputFile);
177+
if (choice==1){
178+
BufferedImage grayScale = convertToGrayScale(inputImage);
179+
File graScaleImage = new File(p);
180+
ImageIO.write(grayScale, "jpg", graScaleImage);
181+
System.out.println("Image changed to GrayScale Successfully!!");
182+
}else if (choice==2){
183+
System.out.print("Enter the brightness value to be modified::");
184+
int N = sc.nextInt();
185+
BufferedImage changedBrightness = changeBrightness(inputImage,N);
186+
File changedBrightnessImage = new File(p);
187+
ImageIO.write(changedBrightness, "jpg", changedBrightnessImage);
188+
System.out.println("Image Brightness Changed Successfully!!");
189+
}else if (choice ==3){
190+
BufferedImage rotatedImageC = rotateImage(inputImage);
191+
File RotatedImageC = new File(p);
192+
ImageIO.write(rotatedImageC, "jpg", RotatedImageC);
193+
System.out.println("Image Rotated clockwise Successfully!!");
194+
}else if (choice==4){
195+
BufferedImage rotatedImageAC = verticalSwitch(horizontalSwitch(rotateImage(inputImage)));
196+
File RotatedImageAC = new File(p);
197+
ImageIO.write(rotatedImageAC, "jpg", RotatedImageAC);
198+
System.out.println("Image Rotated anti-clockwise Successfully!!");
199+
}else if (choice ==5){
200+
BufferedImage HoriImage = horizontalSwitch(inputImage);
201+
File HorizontalImage = new File(p);
202+
ImageIO.write(HoriImage, "jpg", HorizontalImage);
203+
System.out.println("Image Flipped Horizontally Successfully!!");
204+
}else if (choice ==6){
205+
BufferedImage VertiImage = verticalSwitch(inputImage);
206+
File VerticalImage = new File(p);
207+
ImageIO.write(VertiImage, "jpg", VerticalImage);
208+
System.out.println("Image Flipped Vertically Successfully!!");
209+
}else if (choice ==7){
210+
System.out.print("Enter the Blurring value::");
211+
int N = sc.nextInt();
212+
if (N>inputImage.getHeight() && N>inputImage.getWidth()){
213+
if(inputImage.getHeight()>inputImage.getWidth()){
214+
N=inputImage.getWidth();
215+
}else{
216+
N=inputImage.getHeight();
217+
}
218+
}else if(N>inputImage.getHeight()){
219+
N=inputImage.getHeight();
220+
}else if (N>inputImage.getWidth()){
221+
N=inputImage.getWidth();
222+
}else if (N<0){
223+
System.out.println("Invalid input !!");
224+
break;
225+
}
226+
BufferedImage BlurImage = blurImage(inputImage,N);
227+
File BlooImage = new File(p);
228+
ImageIO.write(BlurImage, "jpg", BlooImage);
229+
System.out.println("Image Blurred Successfully!!");
230+
}else if (choice ==8){
231+
BufferedImage InvertImage = colorInvert(inputImage);
232+
File InvertedImage = new File(p);
233+
ImageIO.write(InvertImage, "jpg", InvertedImage);
234+
System.out.println("Image Inverted Successfully!!");
235+
}
236+
else if(choice==0){
237+
ch=false;
238+
System.out.println("Thank You for visiting!!");
239+
break;
240+
}else{
241+
System.out.println("Invalid choice!!");
242+
}
243+
System.out.println();
244+
System.out.print("Do you want to continue?(Enter Y to continue)");
245+
char cont = sc.next().charAt(0);
246+
char yes = 'Y';
247+
if (cont != yes && ch){
248+
ch=false;
249+
System.out.println("Thank You for visiting!!");
250+
break;
251+
}else{
252+
System.out.println("Enter 9 to work on the modified image");
253+
System.out.println("Enter 10 to work on original image.");
254+
System.out.println("Enter 11 to work on new image.");
255+
}
256+
System.out.print("Enter the choice::");
257+
int K = sc.nextInt();
258+
if (K==9){
259+
s=""+p;
260+
}else if (K==11){
261+
String t="";
262+
System.out.print("Enter the path of the image::");
263+
t=sc.next();
264+
s=t;
265+
}
266+
}
267+
}catch (IOException e) {
268+
e.printStackTrace();
269+
}
270+
}
271+
}
325 KB
Binary file not shown.

sample.jpg

13.6 KB
Loading

0 commit comments

Comments
 (0)