Python code to convert jpg to rgb format

Below utility will help convert
jpeg file to rgb file format

Any question leave a comment.

Save the code to a file "jpg2rgb.py"

How to use: python jpg2rgb.py      


import cv2
import os, sys
import numpy as np
import argparse


def convert_jpg2rgb(ipfile, opfile):

    # Decoding input jpeg file
    ipimg = cv2.imread(ipfile, cv2.IMREAD_COLOR)

    # Shape of input image
    w, h, d = ipimg.shape

    # Reading clip name
    s = ipfile.split('\\')
    clip_name = str(s[len(s)-1])

    s = clip_name.split('.')
    clipName = str(s[0])
   
    # Reading B, G, R buffers
    b, g, r = cv2.split(ipimg)

    opimg = open(opfile + '/' + str('rgb_') + str(clipName) + str('.rgb'), 'wb')

    for i in range(0, h):
        for j in range(0, w):
            opimg.write(r[i,j])
            opimg.write(g[i,j])
            opimg.write(b[i,j])
    opimg.close()

    return True

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('ip_jpg', help="Path to Input jpg file")
    parser.add_argument('op_rgb', help="Path to save rgb file")
   
    args = parser.parse_args()
    return args

if __name__ == "__main__":
 
    args = parse_args()

    ip_jpg_file = args.ip_jpg

    if not os.path.isfile(ip_jpg_file):
        print "jpg file Not found\nExiting..."
        sys.exit()
       
    op_rgb_file = args.op_rgb
    if not os.path.isdir(op_rgb_file):
        print "Invalid path\nExiting..."
        sys.exit()

    if(convert_jpg2rgb(ip_jpg_file, op_rgb_file)):
        print "Conversion Sucessful"
    else:
        print "Conversion Failed"


2 comments:

  1. This information you provided in the blog that was really unique I love it!!, Thanks for sharing such a great blog Corporate training in Embedded Systems | embedded systems training institute in bangalore

    ReplyDelete
  2. I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. z code system discount

    ReplyDelete

Related Posts

Twitter Updates

Random Posts

share this post
Bookmark and Share
| More
Share/Save/Bookmark Share