How to speed up this code with Numpy? - python-3.x

Currently I am using the following code to convert all non-black pixels to white:
def convert(self, img):
for i in range(img.shape[0]):
for j in range(img.shape[1]):
if img.item(i, j) != 0:
img.itemset((i, j), 255)
return img
How can I speed it up?

All elements that are not 0 should change to 255:
a[a != 0] = 255

How about using PIL and make the function like this:
def convert (self,image):
return image.convert('1')
Test code:
from PIL import Image
import matplotlib.pyplot as plt
def convert (image):
return image.convert('1')
img = Image.open('./test.png')
plt.figure(); plt.imshow(img)
BW = convert(img)
plt.figure(); plt.imshow(BW)
plt.show()
result :
And btw, in case you needed the numpy array of the PIL image object, you can easily get it using:
matrix_of_img = numpy.asarray(img.convert('L'))

Related

I'm trying to get this code to draw lines on the output window but I can't seem to get it to

import numpy
import cv2
import time as t
from PIL import ImageGrab
import numpy as np
print("\n\n----------------\n\nPress 'Q' to Quit\n\n----------------\n\n")
def draw_line(img, lines):
try:
for line in lines:
coords = line[0]
cv2.line(img, (coords[0]),(coords[1]),(coords[2]),(coords[3]), [255,0,255], 5)
except:
pass
def process_img(o_img):
pro_img = cv2.cvtColor(o_img, cv2.COLOR_BGR2GRAY)
pro_img = cv2.Canny(pro_img, threshold1= 100, threshold2=300)
lines = cv2.HoughLinesP(pro_img, 1, np.pi/180, 180, 3, 15)
draw_line(pro_img, lines)
return pro_img
while(True):
screen = np.array(ImageGrab.grab(bbox = (80, 130, 990, 630)))
new_scr = process_img(screen)
cv2.imshow("window", new_scr)
#cv2.imshow('window 2',cv2.cvtColor(screen, cv2.COLOR_BGR2RGB))
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
I tried the draw function and it didn't work not sure if I need to do something else to the image or if the function isn't placed correctly

Watermark in the photo

I'm making a watermark in the photo. How to make a watermark for several photos at once? and How to save multiple photos at once? which loop should be and in which part of the code?
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
from PIL import ImageOps
def watermark_text(input_image_path,
output_image_path,
text, pos):
photo = Image.open(input_image_path)
drawing = ImageDraw.Draw(photo)
white = (3, 8, 12)
font = ImageFont.truetype("/Roboto-Regular.ttf", 150)
drawing.text(pos, text, fill=white, font=font)
photo.show()
photo.save(output_image_path)
if __name__ == '__main__':
img = 'new7093.JPG'
watermark_text(img, '11112.JPG',
text='sportves.ru',
pos=(300,500))
How about using multiprocessing.
from concurrent.futures import ProcessPoolExecutor
import os
img_list = os.listdir('path-to-image')
output_img_list = [i +'.output' for i in img_list]
text='sportves.ru'
pos=(300,500)
with ThreadPoolExecutor(max_workers=4) as executor:
futures = [executor.submit(watermark_text, input_image_path, output_image_path, text, pos)
for input_image_path, output_image_path
in zip(img_list, output_img_list)
]
It is should work). Replace a save path and append images to put watermarks.
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
def watermark_text(input_image_path,
output_image_path,
text, pos):
global new_images
photo = Image.open(input_image_path)
drawing = ImageDraw.Draw(photo)
white = (3, 8, 12)
font = ImageFont.truetype("/Roboto-Regular.ttf", 150)
drawing.text(pos, text, fill=white, font=font)
photo.show()
new_images.append(photo)
if __name__ == '__main__':
images = ['new7093.JPG', 'something.phg'] # list of photos without watermark
new_images = [] # list of photos with watermark
for img in images:
watermark_text(img, '11112.JPG',
text='sportves.ru',
pos=(300, 500))
new_images[0].save(r'C:\Users\You\Desktop', save_all=True,
append_images=new_images[1:]) # replace by you path

How to extract text from image

I have tried most of the solutions on the site to extract data from the image,
only this script worked with the format *.tif, and gave me correct data
'''
from PIL import Image
import glob
import pytesseract
image_list = []
for filename in glob.glob(my_image):
im=Image.open(filename)
image_list.append(im)
pytesseract.pytesseract.tesseract_cmd="C:\\Program Files\\Tesseract-OCR\\tesseract.exe"
texts = [pytesseract.image_to_string(img,lang = 'eng') for img in image_list]
'''
However, this is not working with *.png and *.jpg, I tried the following:
'''
import cv2
import numpy as np
image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
sharpen_kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
sharpen = cv2.filter2D(gray, -1, sharpen_kernel)
thresh = cv2.threshold(sharpen, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
close = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=1)
result = 255 - close
'''
And like,
'''
import os
from PIL import Image
import cv2
import pytesseract
import ftfy
import uuid
filename = img
image = cv2.imread(os.path.join(filename))
gray = cv2.threshold(image, 200, 255, cv2.THRESH_BINARY)[1]
gray = cv2.resize(gray, (0, 0), fx=3, fy=3)
gray = cv2.medianBlur(gray, 9)
filename = str(uuid.uuid4())+".jpg"
cv2.imwrite(os.path.join(filename), gray)
config = ("-l eng --oem 3 --psm 11")
text = pytesseract.image_to_string(Image.open(os.path.join(filename)), config=config)
text = ftfy.fix_text(text)
text = ftfy.fix_encoding(text)
text = text.replace('-\n', '')
print(text)
'''
and such, but not given me data, how can I extract text from image like of invoice?
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract'
print(pytesseract.image_to_string(r'D:\examplepdf2image.png'))
def escape(html):
"""Returns the given HTML with ampersands, quotes and carets encoded."""
return mark_safe(force_unicode(html).replace('&', '&').replace('<', '<').replace('>', '>').replace('"', '"').replace("'", '''))
this a sample code put instead of trying to print text through many different variables from this to that just try to print the image itself first. Then work on how to improve from there. One last thing is that this will let python work without errors making it easy to understand as well. The second piece of code with the def escape shows how to import an html file which you have to put your pieces of code into so you change it to your liking.

Python cv2.imread() by url

i want to get images by url here on 6 and 7 lines, any help or ideas?
import urllib
import numpy as np
mkembed = ""
ourembed = ""
mkpic = cv2.imread("image.jpg")
ourpic = cv2.imread("image2.jpg")
difference = cv2.subtract(mkpic, ourpic)
b, g, r = cv2.split(difference)
if cv2.countNonZero(b) == 0 and cv2.countNonZero(g) == 0 and cv2.countNonZero(r) == 0:
print("The images are completely Equal")```
Use the following code to get an image by url with cv2:
#import necessary packages
import numpy as np
import urllib.request as urllib
import cv2
#get image by url
resp = urllib.urlopen("https://homepages.cae.wisc.edu/~ece533/images/airplane.png")
image = np.asarray(bytearray(resp.read()), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
#show image
cv2.imshow("Image", image)
cv2.waitKey()

How to detect objects which have almost similar color with their background?

original image
image after kmeans clustering
image I get as result
I am working on malaria parasite detection using thick blood microscopy image. I have tried to segment the parasite objects but it is difficult since they have almost similar background color. I have used vv2.kmeans() to cluster the parasite and non parasite.
import csv as csv
import matplotlib.pyplot as plt
def smooth(img):
dest=cv2.medianBlur(img,7)
#dest=cv2.GaussianBlur(img, (7,7),0)
return dest
def process(path,img):
image=cv2.imread(path+img,1)
image=smooth(image)
return image
def kmeans(img,name):
output=[]
image=img.reshape(img.shape[0]*img.shape[1],3)
image=np.float32(image)
nclusters=5
criteria=(cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER,10,1.0)
attempts=10
flags=cv2.KMEANS_RANDOM_CENTERS
compactness,labels,centers=cv2.kmeans(image,nclusters,None,criteria,attempts,flags)
centers = np.uint8(centers)
res = centers[labels.flatten()]
res2 = res.reshape((img.shape))
cv2.imwrite(dest+name[:-4]+'.png', res2)
im_color=cv2.imread(dest+name[:-4]+'.png',cv2.IMREAD_COLOR)
im_gray = cv2.cvtColor(im_color, cv2.COLOR_BGR2GRAY)
_, mask = cv2.threshold(im_gray, thresh=100, maxval=255, type=cv2.THRESH_BINARY_INV)
mask3 = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR) # 3 channel mask
im_thresh_color = cv2.bitwise_and(img, mask3)
cv2.imwrite("C:\\Users\\user\\Desktop\\lbim2\\"+name[:-4] +".png",im_thresh_color)
def preprocess(path):
images=[]
j=0
print ("Median Blur")
for i in os.listdir(path):
print(i)
images.append(process(path,i))
print(images[j].shape)
#print(images[1].shape)
images[j]=kmeans(images[j],i)
j+=1
print(i)
dest='../output1/'
print ("Preprocess")
preprocess('../input1/')
I have get a image with all pixel value 0. black output

Resources