• Tech Travel Hub is your one stop, ultimate tour guide to all things tech, travel, visas and digital nomads
  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact
Menu
  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact
View Blog
April 19, 2023April 19, 2023

How To Convert A Picture to GrayScale Image In Java

Objective:

In this post we will check out how to convert a normal jpg image to Gray scale.
Or else in a simple word this post talks about how to convert a color image to black and white.

Approach:

The approach will be to calculate the pixel value.
Every image can be decomposed to pixels.say its width is 400 and height is 500. So this can be decomposed to 400X500 pixels. This is nothing but to decompose the picture into matrix structures.

Now, get the basic color of the intersection points like the red,green and blue color value.

If we get the RGB value for a color value as
R=120
G=80
B=240

Now we need to calculate the average value for this position.
Avg value=(120+80+240)/3~107
Now we can set the RGB value as 107,107,107 for this position.

Code Snippet:

How to get the image-

// Save the image file as a BufferedImage object
BufferedImage pic = ImageIO.read(new File("E:\finance.jpg"));

Now decompose the image into pixels

// Loop through all the pixels in the image (w = width, h = height)
for(int w = 0; w < pic.getWidth() ; w++)
{
for(int h = 0 ; h < pic.getHeight() ; h++)
{
}
}

How to calculate the average color value

    
// use the RGB values to get their average.
int averageColorval = ((color.getRed() + color.getGreen() + color.getBlue())/3);
Real Implementation:

Let us see how the real implementation goes:

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class jpgtogray
{
public static void main(String[] args) throws IOException
{
// Save the image file as a BufferedImage object
BufferedImage pic = ImageIO.read(new File("E:\finance.jpg"));
// Loop through all the pixels in the image (w = width, h = height)
for(int w = 0; w < pic.getWidth() ; w++)
{
for(int h = 0 ; h < pic.getHeight() ; h++)
{
// BufferedImage.getRGB() saves the colour of the pixel as a single integer.
// use Color(int) to grab the RGB values individually.
Color color = new Color(pic.getRGB(w, h));
// use the RGB values to get their average.
int averageColorval = ((color.getRed() + color.getGreen() + color.getBlue())/3);
// create a new Color object using the average colour as the red, green and blue
// colour values
Color avg = new Color(averageColorval, averageColorval, averageColorval);
// set the pixel at that position to the new Color object using Color.getRGB().
pic.setRGB(w, h, avg.getRGB());
}
}
// save the newly created image in a new file.
ImageIO.write(pic, "jpg", new File("E:\finance.jpg"));
}
}
Output:

I tried to change the picture by providing the left side picture as input and the program gave output which is there in the right side.

Please let me know if you like this post. The concept is taken from here and  here. If you like the post let me know how do you find this program.

 

 

Post navigation

Previous post
Next post

Umer Umer

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Zoom Clone: 6 Significant Benefits To Lure You In Launching
  • Your Shopify Store – Top 5 Essential Inventory Management Apps
  • All-inclusive You Need To Know About Skirting Beyond 2020
  • Yohuton Health and Fitness Smartwatch for Men Beyond 2020
  • 5 Amazing Benefits On: Enrich Your Life With Yoga Practice

Categories

  • Adventure
  • Agile Methodology
  • AI
  • All world Tour
  • Android Development
  • Ansible
  • App
  • App Development
  • Artificial Intelligence
  • AWS
  • Bitcoin
  • BlockChain
  • Blogging
  • Box Packing
  • Business
  • business
  • Business Communication
  • buying guide
  • C
  • car
  • Care
  • Certification
  • Certification
  • Chatbot
  • cinema
  • Cloud Tech
  • Companies
  • Content Marketing
  • Corporate
  • covid-19
  • crucible
  • Cryptocurrency
  • Database
  • DevOps
  • DevOps
  • Digital Marketing
  • Drawing
  • Dress
  • Earn Money Online
  • Educational
  • Employment
  • Entertainment
  • Exceptions In Java
  • Fashion
  • Festivals
  • Finance
  • Fitness
  • Food
  • Game
  • Gift ideas
  • Graphic Designers
  • Groovy
  • Health
  • Health
  • home
  • home improvement
  • HR
  • IllegalThreadStateException in Java
  • India Tour
  • Instagram
  • Ios
  • ISO
  • Java
  • Javascript
  • job
  • Kids Section
  • Kitchen
  • Law
  • Life Style
  • Lifestyle
  • Lifestyle Home Improvement
  • Magento
  • Main Stories
  • Management
  • Mattress
  • Motorable
  • Movie
  • nursing
  • Office Productivity
  • Pendrive
  • Perspective
  • Pet
  • Popular
  • Programming
  • Python
  • RDT
  • Relationships
  • Selenium
  • SEO
  • Skin
  • SOA
  • SOAPUI
  • Social Media
  • Social Media Marketing
  • Software
  • software development
  • Software Development Model
  • Software Engineering
  • Software Testing
  • Sports
  • Startup
  • T Robot
  • TDD
  • Tech
  • Tech News
  • Tech News
  • Technology
  • Tool
  • Tour Guide
  • Tour Guide
  • Travel
  • UFT
  • Uncategorized
  • USB Cable
  • VBscript
  • VPN
  • Web Development
  • WebHosting
  • WebServices
  • Wildlife
  • Wordpress
  • Yoga
  • Youtube

Tags

Instagram Instagram Marketing Marketing SEO Thoptv App

Categories

  • Tech
  • Travel
  • Visas
  • Digital Nomad
  • Tech
  • Travel
  • Visas
  • Digital Nomad

Publishing

If you would like to publish on our site, please email admin@techtravelhub.com

Quick Links

  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact
  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact

Contact

Tech Travel Hub LLC

Address:
8 The Green STE A
Dover
Delaware, 19901

Phone: +1 302 956 9948