Python Forum
Converting an RGBA image to Grayscale and Binary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converting an RGBA image to Grayscale and Binary
#1
Hi, I have a RGBA image which I need to convert to Grayscale and then to binary with a threshold to get binary histogram but I have 2D Grayscale image in my code which I don't understand how to convert to binary.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Import libraries
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
import cv2
 
# Reading an Image
image = Image.open('Board1_1.png')
 
# Properties of an Image
print(image.size)
print(image.format)
print(image.mode)
 
# Grayscale conversion
image_gray = image.convert('LA')
 
image_gray_array = 255 - np.asarray(image_gray)
 
image_gray_array_1 = image_gray_array[:,:,0]
image_gray_array_2 = image_gray_array[:,:,1]
 
image_gray.show()
Reply
#2
I believe Python's module cv2 can do just about anything with images, and a lot more besides!

This code below gives me a threshold image. It's an excerpt from my own little amateurish OMR programme.

I save the steps on the way just so I can see what is going on.

This is all a bit on the "threshold" of my understanding! But it works!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import cv2
 
destination = '/home/pedro/omr_steps/'
 
f = jpg_files[0]
original = cv2.imread(f)
name = 'step0_original.png'
cv2.imwrite(destination + name, original)
 
# crop the bottom part of the image, the part with the choices, the part to mark
# f is just a file path: savename = savepathMarked + 'temp_cropped_image.png'
cropped_image = cropAll(f, destination, tup)
# im is the image to write the stroked contours on
im = cv2.imread(cropped_image)
name = 'step1_cropped_image.png'
cv2.imwrite(destination + name, im)
 
# make a grey image
grey = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
name = 'step2_grey.png'
cv2.imwrite(destination + name, grey)
 
# blur the grey image this helps the next step: Canny
blurred = cv2.GaussianBlur(grey, (5, 5), 0)
name = 'step3_blurred.png'
cv2.imwrite(destination + name, blurred)
 
# get the edges of the blurred image
edged = cv2.Canny(blurred, 75, 200)
name = 'step4_edged.png'
cv2.imwrite(destination + name, edged)
 
# find contours in the edge map, then use the contours on the thresh
     
edged2 = edged.copy()
contours, hierarchy = cv2.findContours(edged2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
print('reading edged2, there are ', len(contours), ' contours')
 
thresh = cv2.threshold(grey, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
name = 'step5_thresh.png'
cv2.imwrite(destination + name, thresh)
Reply
#3
Hi, I get an error "ModuleNotFoundError: No module named 'cv2'"

I have already installed "pip install opencv-python" but still get an error
Reply
#4
In your post you had it imported! Why, if you are not using it?

I only use Linux, can't help if you use Windoze!

Normally, I install a module with pip, then it is available.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Binary data to Image convert Nuwan16 1 8,034 Aug-24-2020, 06:03 AM
Last Post: millpond
  Maximas in grayscale image yliats 2 2,774 Aug-13-2020, 01:11 PM
Last Post: yliats
  Convert 400 grayscale pixels into RGB python420 1 3,177 Jan-02-2020, 04:19 PM
Last Post: Clunk_Head
  hex file to binary or pcap to binary baran01 1 7,064 Dec-11-2019, 10:19 PM
Last Post: Larz60+
  Converting str to binary ebolisa 1 2,644 Jun-17-2019, 11:50 PM
Last Post: scidam
  python opencv grayscale conversion error Spandora 1 10,728 May-26-2019, 10:43 AM
Last Post: heiner55
  converting binary b'0x31303032\n' to "1002" string amygdalas 2 3,309 Nov-07-2018, 03:50 AM
Last Post: amygdalas
  Converting Decimal to Binary emerger 1 3,301 Feb-27-2018, 08:06 PM
Last Post: Gribouillis
  Converting an 8-bit binary to decimal WITHOUT the shortcuts EIA 5 8,199 Dec-01-2017, 09:04 AM
Last Post: stranac
  show grayscale image using matplotlib zyb1003 1 17,438 Nov-01-2017, 11:45 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020