I am trying to make an image transparent.
This is my image
from PIL import Image
img = Image.open('Frame 0001.png')
img = img.convert("RGBA")
datas = img.getdata()
newData = []
for item in datas:
if item[0] == 255 and item[1] == 255 and item[2] == 255:
newData.append((255, 255, 255, 0))
else:
newData.append(item)
img.putdata(newData)
img.save("img2.png", "PNG")
My output image is this:
Well Googled a bit and found a package called cv2. Had a tough time installing that package, but the thing I was trying to do was possible
import cv2
img1=cv2.imread('m1.jpg')
img2=cv2.imread('logo.jpg')
dst=cv2.addWeighted(img1,0.7,img2,0.3,0)
cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
Related
I'm trying to use tesseract to read text from a game with poor results.
What I would like to accomplish is to remove the background from the image so that only the text is visible to improve OCR results.
I've tried cv2.inRange, thresholding yet I can't seem to get it to work.
import numpy as np
import pytesseract
from tesserocr import PyTessBaseAPI, OEM
def _img_to_bytes(image: np.ndarray, colorspace: str = 'LAB'):
# Sets an OpenCV-style image for recognition: https://github.com/sirfz/tesserocr/issues/198
bytes_per_pixel = image.shape[2] if len(image.shape) == 3 else 1
height, width = image.shape[:2]
bytes_per_line = bytes_per_pixel * width
if bytes_per_pixel != 1 and colorspace != 'RGB':
# non-RGB color image -> convert to RGB
image = cv2.cvtColor(image, getattr(cv2, f'COLOR_{colorspace}2RGB'))
elif bytes_per_pixel == 1 and image.dtype == bool:
# binary image -> convert to bitstream
image = np.packbits(image, axis=1)
bytes_per_line = image.shape[1]
width = bytes_per_line * 8
bytes_per_pixel = 0
# else image already RGB or grayscale
return image.tobytes(), width, height, bytes_per_pixel, bytes_per_line
img = cv2.imread("ref.png")
img = ~img
clahe = cv2.createCLAHE(clipLimit=3., tileGridSize=(8,8))
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
l, a, b = cv2.split(lab)
l2 = clahe.apply(l)
lab = cv2.merge((l2,a,b))
img = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR)
with PyTessBaseAPI(psm=3, oem=OEM.LSTM_ONLY, path=f"ocr", lang=d2r ) as api:
api.ReadConfigFile("ocr/config.txt")
api.SetVariable("user_words_file","ocr/dict.txt")
api.SetImageBytes(*_img_to_bytes(img))
print(api.GetUTF8Text())
cv2.imshow('res',img)
cv2.waitKey()```
Inverting color may help? try this & let me know.
import cv2
image = cv2.imread("Bytelock.jpg")
image = ~image
cv2.imwrite("Bytelock.jpg",image)
Inverted image
Red varient
import numpy as np
import imutils
import cv2
img_rgb = cv2.imread('ss.jpg')
Conv_hsv_Gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(Conv_hsv_Gray, 0, 255,cv2.THRESH_BINARY_INV |cv2.THRESH_OTSU)
img_rgb[mask == 255] = [0, 0, 255]
cv2.imshow("red", img_rgb)
cv2.imwrite("red.jpg", img_rgb)
More sharpen? Try
import numpy as np
import imutils
import cv2
img_rgb = cv2.imread('ss.jpg')
Conv_hsv_Gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(Conv_hsv_Gray, 0, 255,cv2.THRESH_BINARY_INV |cv2.THRESH_OTSU)
img_rgb[mask == 255] = [0, 0, 255]
cv2.imwrite("mask.jpg", mask)
cv2.imshow("mask", mask) # show windows
cv2.waitKey(0)
**
Much more better option
**
import cv2
image = cv2.imread("ss1.jpg")
image = ~image
img = image
clahe = cv2.createCLAHE(clipLimit=3., tileGridSize=(8,8))
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
l, a, b = cv2.split(lab)
l2 = clahe.apply(l)
lab = cv2.merge((l2,a,b))
img2 = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR)
cv2.imshow('Increased contrast', img2)
cv2.waitKey(0)
cv2.destroyAllWindows()
More sharpen
I am trying to extract text from an image that looks like OCR should be able to easily extract but it just extracting nothing or garbage in some cases.
I have tried the following OpenCV techniques from other stackoverflow resources but nothing seems to help.
Image Resisizing
GrayScaling
Dilation and Erosion
adaptiveThreshold
If someone could help me how to extract text from the attached image using OpenCV that would be really helpful
My Code
import pytesseract
import cv2
import numpy as np
# Configuration
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
image_path = "C:/Users//opencv_test/images/"
ocr_path = "C:/Users//opencv_test/ocr/"
doc_file_name = "Image.jpeg"
ocr_file_name = doc_file_name[:-4] + "txt"
# To read image from disk, we use
# cv2.imread function, in below method,
img = cv2.imread(image_path + doc_file_name, 1)
# Creating GUI window to display an image on screen
# first Parameter is windows title (should be in string format)
# Second Parameter is image array
cv2.imshow("Actual Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
def remove_noise(img):
filtered = cv2.adaptiveThreshold(img.astype(np.uint8), 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 199, 5)
cv2.imshow("Noise Free Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
return filtered
def remove_noise_2(img):
img = cv2.resize(img, None, fx=.9, fy=.8, interpolation=cv2.INTER_CUBIC)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
kernel = np.ones((1, 1), np.uint8)
img = cv2.dilate(img, kernel, iterations=1)
img = cv2.erode(img, kernel, iterations=1)
cv2.imshow("Pre-Processed Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
return img
noise_free_img = remove_noise_2(img)
# Run Tesseract
f = open(ocr_path + ocr_file_name, 'w')
text = str((pytesseract.image_to_string(noise_free_img, config='--psm 3')))
text = text.replace('-\n', '')
f.write(text)
Default Image(befor cropping)
***How to convert this image to the cropped image ***
Image after cropping
You can use pillow module in python to create transparent background.
from PIL import Image
def convertImage():
img = Image.open("7OUBR.png")
img = img.convert("RGBA")
datas = img.getdata()
newData = []
for item in datas:
if item[0] == 255 and item[1] == 255 and item[2] == 255:
newData.append((255, 255, 255, 0))
else:
newData.append(item)
img.putdata(newData)
img.save("glass.png", format="png")
print("Successful")
convertImage()
The code I use is this:
from PIL import Image
import os
path = 'C:/Users/User/Desktop/GF_BSIF/temp'
newData = []
for image in os.listdir(path):
img = Image.open(path+'/'+image)
img = img.convert("RGBA")
datas = img.getdata()
for item in datas:
if item[0] == 255 and item[1] == 255 and item[2] == 255:
newData.append((255, 255, 255, 0))
else:
if item[0] > 150:
newData.append((0, 0, 0, 255))
else:
newData.append(item)
print(item)
img.putdata(newData)
img.save('C:/Users/User/Desktop/GF_BSIF/temp'+'/'+"open_science_logo_transparent.png", "PNG")
Original:
Result:
The result does not exactly make the background transparent.
How to improve the code and make the black background into transparent?
EDIT:
from PIL import Image
import matplotlib.pyplot as plt
import os
import shutil
path = 'C:/Users/User/Desktop/GF_BSIF/temp'
out_put = 'C:/Users/User/Desktop/data science/cropped'
newData = []
for image in os.listdir(path):
img = Image.open(path+'/'+image)
img = img.convert("RGBA")
datas = img.getdata()
for item in datas:
if all(i == 0 for i in datas[0:3]):
newData.append((0, 0, 0, 0))
else:
etcetcetc
img.putdata(newData)
img.save('C:/Users/User/Desktop/GF_BSIF/temp'+'/'+"open_science_logo_transparent.png", "PNG")
#NotActuallyErik
It shows an error
TypeError Traceback (most recent call last)
in 14 15 for item in datas: ---> 16 if all(i == 0 for i in datas[0:3]): 17 newData.append((0, 0, 0, 0)) 18 else:
TypeError: sequence index must be integer, not 'slice'.
How to make it right?
Your code is telling PIL to remove replace every white pixel with a black one: [255,255,255,0] -> [0,0,0,255] which is why your black background remains while only the full-white spots get removed. You need to do the opposite to remove the background, i.e [0,0,0,255] -> [0,0,0,0]
this might work
for item in datas:
if all(i == 0 for i in datas[0:3]):
newData.append((0, 0, 0, 0))
else:
etcetcetc
I am trying to rotate my transparent gif using the PIL rotate() but I am getting a diamond after rotating it.
Similar problems on SO were solved by using
transparency = img.info['transparency']
img.save('result.gif',transparency=transparency)
or by using
img.save('result.gif', **img.info)
But I am getting the following results as in the images.
My code is
from PIL import Image
file = 'change2_1.gif'
with Image.open(file) as im:
transparency = im.info['transparency']
img = im.rotate(45, expand=True)
img.save('result.gif', transparency=transparency)
Post edited: Try to use this to make black backgroud transparent
from PIL import Image
img = Image.open('img.png')
img = img.convert("RGBA")
datas = img.getdata()
with Image.open(file) as im:
transparency = im.info['transparency']
img = im.rotate(90, expand=True)
img.save('result.gif', transparency=transparency)
newData = []
for item in datas:
if item[0] == 0 and item[1] == 0 and item[2] == 0:
newData.append((255, 255, 255, 0))
else:
newData.append(item)
To avoid having a black frame after rotation, simply change the default mode ('RGB') to 'RGBA' after opening :
img = Image.open('image.png').convert('RGBA')
img.rotate(90, expand = True)