Giter Club home page Giter Club logo

convertingcolortograyscale's Introduction

ConvertingColorToGrayscale

Converting color to grayscale with Python | Coded by Hoyeol Kim

Converting a Single File

If you want to simply covert color to grayscale in a single file, use the following code from gray_single.py:

import cv2

directory1 = r'resized/' # Source Folder
directory2 = r'gray/' # Destination Folder

image = cv2.imread(directory1+'image1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

cv2.imwrite(directory2+'image1_gray.png', gray)

cv2.waitKey(0)
cv2.destroyAllWindows()

Converting Multiple Files

If you want to covert color to grayscale in multiple files, use the following code from gray_multiple.py:

import cv2
import os,glob
from os import listdir,makedirs
from os.path import isfile,join

path = 'resized/' # Source Folder
dstpath = 'gray/' # Destination Folder

try:
    makedirs(dstpath)
except:
    print ("The directory already exists, so the images will be written in the same folder")

files = [f for f in listdir(path) if isfile(join(path,f))]

for image in files:
    try:
        img = cv2.imread(os.path.join(path,image))
        gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        dstPath = join(dstpath,image)
        cv2.imwrite(dstPath,gray)
    except:
        print ("{} is not converted".format(image))

for fil in glob.glob("*.png"):
    try:
        image = cv2.imread(fil)
        gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        cv2.imwrite(os.path.join(dstpath,fil),gray_image)
    except:
        print('{} is not converted')

Color Gray

convertingcolortograyscale's People

Contributors

elibooklover avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.