Open CV cv.imread modifying image - python

I use cv.imread to read a png file in python. When I then use cv.imwrite function to immediately save the image i then find that the colours in the image have changed slightly. I am trying to perform character recognition on this image and the OCR performs far less well on the image in python than the original image.
The first image is the original, and the second is the saved one with OpenCV.
We can see that the green has changed slightly and whilst this does not seem important it affects the OCR and I therefore imagine that other changes are happening to the png. Does anyone know why this might be and how i can resolve this.
The code is as follows
img = cv2.imread('file.png')
cv2.imwrite('out.png', img)
When I run file.png in tesseract for character recognition I get great results but when I run out.png in tesseract far less words get recognised correctly.

When you have a .png image file you ought to read as a .png file.
I downloaded your image and did some analysis myself.
First, I read the image as you did:
img = cv2.imread('file.png')
img.shape returns (446, 864, 3) i.e an image with 3 channels.
Next I read the same image using cv2.IMREAD_UNCHANGED:
img = cv2.imread('file.png', cv2.IMREAD_UNCHANGED)
img.shape returns (446, 864, 4) i.e an image with 4 channels.
.png files have an additional transparency channel. So next you come accross a .png file read it using cv2.IMREAD_UNCHANGED flag
UPDATE:
Enlisting the various ways to read an image:
for var in dir(cv2):
if var.startswith('IMREAD'):
print(var)
returns:
IMREAD_ANYCOLOR
IMREAD_ANYDEPTH
IMREAD_COLOR
IMREAD_GRAYSCALE
IMREAD_LOAD_GDAL
IMREAD_UNCHANGED

Related

How to Increase image quality in streamlit

I am viewing images by using streamlit without converting to RGB below is the line of code which I am using
st.image("image.jpg")
But the problem is I need to read some numbers inside the image but the image is blurred which means that I am not able to read the numbers present in the image.
Increasing the size of the image does not work.

Processed image using openCV but can't save changes as a new image

I have this simple code that reads in an image
image = cv2.imread(input_path)
Then changes the color to gray
image_corrected = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
I can actually display the new gray image to my Jupyter notebook output
plt.imshow(img_corrected)
However, when I try to write the changes as a new image, the changes don't appear in the image i.e the image is still colored instead of gray.
cv2.imwrite(new_image_path, image_corrected)
I have tried to upgrade my openCV library but this doesn't solve the problem.
First of all, you need to make sure that the directory exists. If you wish to convert RGB image to Grayscale in OpenCV, there is a simpler way.
Check if the directory exist, just create one if doesn't exists
import os
check_dir = os.path.split(new_image_path)[0]
if not os.path.isdir(check_dir):
os.mkdir(check_dir)
Read the image as grayscale
img_gray = cv2.imread(image_path, 0)
Save it immediately
cv2.imwrite(new_image_path, img_gray)

How to read 8 bit greyscale TIFF images in OpenCV for object detection?

i have a list of images which are in greyscale 8 bit resolution. i have a python script which successfully detects objects in colored images. Now i want to use this script to detect objects (people) in these 8 bit images. However, when i try to read an image through imread , i see a black window. How can i read this type of image in opencv and see the details inside it? Here is my code for reading an image:
for imagePath in imagePaths:
# load the image and resize it to (1) reduce detection time
# and (2) improve detection accuracy
image = cv2.imread(imagePath)
image = imutils.resize(image, width=min(400, image.shape[1]))

PIL shows black image when attempting to view image

The current goal of what I am writing is to download an image from S3, open it using Pillow, and show the image. I am able to download the image fine; it can be viewed properly from my photos. However, when I try to use the image with PIL, all of the pixel values are black. It does however, retain the shape of the image, which leads me to know that the image is at least being read. the code is shown below:
s3.Bucket(bucket).download_file(key, key) # downloaded perfectly fine
img = Image.open(key)
img.show() # shows all black with the Images's dimensions
I know I can read from bytes, but that will give me a 1d array of all the bytes instead of the dimensions needed for an image.

Stacking Transparent GIF with Pillow Errors [Fixed!]

I'm trying to make a simple application that fetches radar images from NOAA, then stacks them to make an image for display. I've never used the PILLOW library before, but I do have some experience with Python.
The images are GIF files and have transparent parts. You can see example images at the urls in my code below. I've got a white background and an overlay made from stacking several of the overlays on their site as a single transparent image on my computer.
Here is my current code:
from PIL import Image
import urllib.request
urllib.request.urlretrieve('http://radar.weather.gov/ridge/Warnings/Short/EWX_Warnings_0.gif', 'warnings_now.gif')
urllib.request.urlretrieve('http://radar.weather.gov/ridge/RadarImg/N0R/EWX_N0R_0.gif', 'radar_now.gif')
radar = Image.open('radar_now.gif')
warnings = Image.open('warnings_now.gif')
background = Image.open('Background.gif')
overlay = Image.open('Overlay.gif')
background.paste(radar, (0,0), radar)
background.paste(overlay, (0,0), overlay)
background.paste(warnings, (0,0), warnings)
background.save('radar_final.gif', 'GIF')
I've read in a lot of places, both here and on other sites that to properly stack transparent images, you need to do image_name.paste(transparent_image, (0,0), transparent_image) and that by using a third parameter, PIL will interpret the alpha channel of transparent_image as a mask. However, whenever I run this, I get the following error instead of getting an image as output.
Traceback (most recent call last):
File "radar2.py", line 9, in <module>
background.paste(radar, (0,0), radar)
File "/usr/lib64/python3.3/site-packages/PIL/Image.py", line 1314, in paste
self.im.paste(im, box, mask.im)
ValueError: bad transparency mask
I've searched all over for a solution, and I've only come across a solutions for a similar error happening with PNG images, where they were converted to RGBA mode. When I tried this with my images, I got an identical error (except for line numbers). I wasn't expecting this to work as I've read that GIF images are only P or L mode.
These are all P mode images, and they all have one alpha layer. I also tried splitting off the alpha layer to use as a mask, but that also resulted in a ValueError: bad transparency mask.
I can't seem to find much about transparent GIF images and Pillow. There is this one thing that keeps popping up about converting to PNG, but the solutions in that don't seem relevant.
Thanks for any help!
Note: This is on a 64-bit Gentoo Linux system running Python 3.3 and whatever is the most recent version of Pillow in the Gentoo repos.

Categories