DEV Community

Cover image for Converting from jpg to png using OpenCv
es404020
es404020

Posted on

Converting from jpg to png using OpenCv

The image transformation tool seamlessly converts images JPEG to PNG format while preserving optimal quality and file size.

In this tutorial we would build a simple python code to convert jpg and png.

In openCv coloured images have 3 channels while grayscale image have just one channel with is black and white.But sometimes you check the channel of a coloured image and it is 4 channels.This occurs when the coloured image is save as png.The fourth channel is called the alpha channel.The first three change are

  • red

  • green

  • yellow

  • Alpha (only for png) optional

So the difference between jpg and png is that png have an alpha channel .

`

import cv2
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
%matplotlib inline
from IPython.display import Image

img_kang_org = cv2.imread('a/kangaroo.jpg',cv2.IMREAD_COLOR)
img_kang = cv2.imread('a/kangaroo_sign_with_green_marker.jpg',cv2.IMREAD_COLOR)

print('Shape of the color image: ', img_kang.shape)

img_kang_gray = cv2.cvtColor(img_kang, cv2.COLOR_BGR2GRAY)

plt.figure(figsize = (16,10))
plt.subplot(131); plt.imshow(img_kang_org[:, :, ::-1]); plt.title('Original')
plt.subplot(132); plt.imshow(img_kang[:, :, ::-1]); plt.title('Original with')
plt.subplot(133); plt.imshow(img_kang_gray); plt.title('Grayscale');
`

A1

Next create a threshold and use the bitwise not operator to invert the image .Threshold extract the black and white from image ie image must be grayscale, After which you can now invert using bitwise_not ie changing every white colour to black and black to white

`
ret1, thresh1 = cv2.threshold(img_kang_gray, 150, 255, cv2.THRESH_BINARY)
ret2, thresh2 = cv2.threshold(img_kang_gray, 230, 255, cv2.THRESH_BINARY)
ret3, thresh3 = cv2.threshold(img_kang_gray, 230, 200, cv2.THRESH_BINARY) # Note max value of mask is set to 200

kang_inv_mask_full = cv2.bitwise_not(thresh2) # Fully transparent
kang_inv_mask_semi = cv2.bitwise_not(thresh3) # Semi-transparent (due to max value of thresh3 = 200)

print('Inverse Mask (full) at (100,100): ', kang_inv_mask_full[100,100])
print('Inverse Mask (semi) at (100,100): ', kang_inv_mask_semi[100,100])

plt.figure(figsize = (15, 3))
plt.subplot(141); plt.imshow(img_kang_gray); plt.title('Grayscale Image')
plt.subplot(142); plt.imshow(thresh1); plt.title('Thresh = 150 (max 255)')
plt.subplot(143); plt.imshow(thresh2); plt.title('Thresh = 230 (max 255)')
plt.subplot(144); plt.imshow(thresh3); plt.title('Thresh = 230 (max 200)')

plt.figure(figsize = (15, 7))
plt.subplot(245); plt.imshow(kang_inv_mask_full); plt.title('Fully Transparent Inv. Mask')
plt.subplot(246); plt.imshow(kang_inv_mask_semi); plt.title('Semi Transparent Inv. Mask');

`

A2

Finally split image into RGB , merge back while including the alpha channel which is the threshold and save image as png

`
bk,gk,rk=cv2.split(img_kang)
mat = [bk,gk,rk,kang_inv_mask_full]

mat2= [bk,gk,rk,kang_inv_mask_semi]

kangoroo_transparent = cv2.merge(mat)
kangoroo_semi = cv2.merge(mat2)

cv2.imwrite('full.png',kangoroo_transparent)
cv2.imwrite('full2.png',kangoroo_semi)

`

dd

Thanks for reading

Top comments (0)