Last active
December 10, 2021 11:44
-
-
Save vlantonov/623b3c05643fc566c7d723d54a8efa40 to your computer and use it in GitHub Desktop.
Image encoder/decoder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| import cv2 as cv | |
| import numpy as np | |
| argvs=sys.argv | |
| if (len(argvs) != 2): | |
| print(f'Usage: {argvs[0]} image_file') | |
| quit() | |
| image_file_name = argvs[1] | |
| img=cv.imread(image_file_name, cv.IMREAD_COLOR) | |
| if img is None: | |
| print (f'failed to load {image_file_name}') | |
| quit() | |
| # The files could be encoded with losses in: | |
| # JPG, PNG, WEBP, PAM, TIFF | |
| # Encode to jpeg format | |
| # cv.IMWRITE_JPEG_QUALITY # 0 - 100 | |
| # cv.IMWRITE_JPEG_PROGRESSIVE # True / False | |
| # cv.IMWRITE_JPEG_OPTIMIZE # True / False | |
| # cv.IMWRITE_JPEG_RST_INTERVAL # 0 - 65535 | |
| # cv.IMWRITE_JPEG_LUMA_QUALITY # 0 - 100 | |
| # cv.IMWRITE_JPEG_CHROMA_QUALITY # 0 - 100 | |
| encode_params_jpg = [int(cv.IMWRITE_JPEG_QUALITY), 90] | |
| result_jpg, enc_img_jpg = cv.imencode('.jpg',img, encode_params_jpg) | |
| if not result_jpg: | |
| print('could not encode image to jpg') | |
| quit() | |
| print('JPG encoded image size: ', len(enc_img_jpg)) | |
| # Raw buffer directly saved as JPG image | |
| enc_img_jpg.tofile('encoded.jpg') | |
| # Decode from jpeg format | |
| decoded_img_jpg = cv.imdecode(enc_img_jpg, cv.IMREAD_COLOR) | |
| # Calculate the normalized SAD | |
| abs_diff = np.sum((img.astype(np.float) - decoded_img_jpg.astype(np.float)) ** 2) | |
| abs_diff /= float(img.shape[0] * img.shape[1]) | |
| print("Absolute difference: ", abs_diff) | |
| cv.imshow('Source Image', img) | |
| cv.imshow('Decoded Image JPG', decoded_img_jpg) | |
| cv.waitKey(0) | |
| cv.destroyAllWindows() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # http://jamesgregson.ca/16-bit-image-io-with-python.html | |
| import cv2 | |
| import numpy as np | |
| I = np.ones( (10,20,3) )*65535 | |
| cv2.imwrite( 'output16.png', I.astype(np.uint16) ) | |
| I2 = cv2.imread( 'output16.png', cv2.IMREAD_UNCHANGED ) | |
| print(I2.shape) | |
| # prints (10, 20, 3) | |
| print(I2[1,1,1]) | |
| # prints 65535 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # https://docs.opencv.org/master/d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce | |
| # imwrite | |
| import cv2 | |
| import numpy as np | |
| I = np.ones( (10,20,3), dtype=np.float32 )*1.25 | |
| cv2.imwrite( 'output_float.tiff', I ) | |
| print(I[1,1,1]) | |
| I2 = cv2.imread( 'output_float.tiff', cv2.IMREAD_UNCHANGED ) | |
| print(I2.shape) | |
| # prints (10, 20, 3) | |
| print(I2[1,1,1]) | |
| # prints 1.2468312 | |
| # note the reduced accuarcy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment