In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to separate pictures and text in python". In daily operation, I believe many people have doubts about how to separate pictures and text in python. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to separate pictures and text in python". Next, please follow the editor to study!
This paper gives you an example to share the specific code of python for simple image text segmentation, for your reference, the specific contents are as follows
Original picture:
Image preprocessing: image binarization and picture noise reduction.
# def binarization (img,threshold): # Image binarization operation width,height=img.size im_new = img.copy () for i in range (width): for j in range (height): a = img.getpixel ((I) J)) aa = 0.30a [0] + 0.59a [1] + 0.11a [2] if (aa 0: l = I flag = True if flag and count = = 0: r = I flag 1 flag = False cuts.append ((l) R)) # record the boundary point t + = 1 # print (t) return cuts,t# after the binarization is passed into def horizontal (img): "" the image after binarization is projected horizontally "" pixdata = img.load () w H = img.size ver_list = [] # start projection for y in range (h): black = 0 for x in range (w): if pixdata [x for y] [0] = 0: black + = 1 ver_list.append (black) # to judge the boundary, r = 0 flag = False # number of segmented regions tweak 0 cuts = [] for I Count in enumerate (ver_list): # threshold here is 0 if flag is False and count > 0: l = I flag = True if flag and count = = 0: r = iMurray 1 flag = False cuts.append ((lmemr)) t + = 1 return cuts,t
The main purpose of these two pieces of code is to segment the size of each word in the horizontal and vertical position, and then to segment the preprocessed picture text.
# create the function def get_im_path (): OpenFile = tk.Tk () # create a new window OpenFile.withdraw () file_path = filedialog.askopenfilename () im = Image.open (file_path) # threshold th = getthreshold (im)-16 print (th) # directly binarize the original image im_new1 = binarization (im) Th) im_new1.show () # histogram equalization im1 = his_bal (im) im1.show () im_new_np = np.array (his_bal (im)) th2 = getthreshold (im1)-16 print (th2) # binary im_new = binarization (im1 Th2) # noise reduction im_new_cn = clear_noise (im_new) height = im_new_cn.size [1] print (height) # calculate the values of horizontal projection and vertical projection v, vt = vertical (im_new1) h, ht = horizontal (im_new1) # calculate the segmented region a = [] for i in range (vt): a.append ((v [I] [0], 0) V [I] [1], height) print (a) im_new.show () # histogram equalization and then binary # cut for I, n in enumerate (a, 1): temp = im_new_cn.crop (n) # call the crop function to cut temp.show () temp.save ("c/%s.png"% I)
It's probably done at this point.
Here is the entire code for the file:
Import numpy as npfrom PIL import Imageimport queueimport matplotlib.pyplot as pltimport tkinter as tkfrom tkinter import filedialog# Import File dialog function library window = tk.Tk () window.title ('picture selection interface') window.geometry ('400x100') var = tk.StringVar () # create the image path and process the picture function def get_im_path (): OpenFile = tk.Tk () # create a new window OpenFile.withdraw () file_path = filedialog .askopenfilename () im = Image.open (file_path) # threshold th = getthreshold (im)-16 print (th) # Direct binarization of the original im_new1 = binarization (im Th) im_new1.show () # histogram equalization im1 = his_bal (im) im1.show () im_new_np = np.array (his_bal (im)) th2 = getthreshold (im1)-16 print (th2) # binary im_new = binarization (im1 Th2) # noise reduction im_new_cn = clear_noise (im_new) height = im_new_cn.size [1] print (height) # calculate the values of horizontal projection and vertical projection v, vt = vertical (im_new1) h, ht = horizontal (im_new1) # calculate the segmented region a = [] for i in range (vt): a.append ((v [I] [0], 0) V [I] [1], height) print (a) im_new.show () # histogram equalization and then binarization # cut for I, n in enumerate (a) 1): temp = im_new_cn.crop (n) # call crop function to cut temp.show () temp.save ("c/%s.png"% I) # Vertical projection def vertical (img): "Vertical projection of binarized image" pixdata = img.load () w H = img.size ver_list = [] # start projection for x in range (w): black = 0 for y in range (h): if pixdata [x for y] [0] = 0: black + = 1 ver_list.append (black) # determine the boundary, r = 0 flag = False tweak, judge the number of segments cuts = [] for I Count in enumerate (ver_list): # threshold here is 0 if flag is False and count > 0: l = I flag = True if flag and count = = 0: r = iMurray 1 flag = False cuts.append ((lmemr)) # recording boundary point t + = 1 # print (t) return cuts T # horizontal projection def horizontal (img): "horizontal projection of binarized images" pixdata = img.load () w img.size ver_list = [] # start projection for y in range (h): black = 0 for x in range (w): if pixdata [x Y] [0] = = 0: black + = 1 ver_list.append (black) # judging the boundary, r = 0 flag = False # number of segmented regions, cuts = [] for I Count in enumerate (ver_list): # threshold here is 0 if flag is False and count > 0: l = I flag = True if flag and count = = 0: r = iMurray 1 flag = False cuts.append ((lmemr)) t + = 1 return cuts T # get threshold to calculate average pixel def getthreshold (im): # get threshold to calculate average pixel wid, hei = im.size hist = [0] * 256 th = 0 for i in range (wid): for j in range (hei): gray = int (0.3 * im.getpixel ((I, j)) [0] + 0.59 * im.getpixel ((I) J)) [1] + 0.11 * im.getpixel ((I, j)) [2]) th = gray + th hist [gray] + = 1 threshold = int (th/ (wid*hei)) return threshold# histogram equalization increases contrast def his_bal (im): # histogram equalization increases contrast # Statistical grayscale histogram im_new = im.copy () wid Hei = im.size hist = [0] * 256 for i in range (wid): for j in range (hei): gray = int (0.3*im.getpixel (I) [0] + 0.59*im.getpixel (I) [1] + 0.11*im.getpixel ((I J) [2]) hist [gray] + = 1 # calculate the cumulative distribution function cdf = [0] * 256 for i in range: if I = 0: cdf [I] = hist [I] else: cdf [I] = cdf [I-1] + hist [I] # calculate the output grayscale image with the cumulative distribution function LUT new_gray = [0] * 256 for i in range (256): new_ Gray [I] = int (CDF [I] / (wid * hei) * 255 + 0.5) # traversing the original image Calculate the pixel values of the new image point by point through LUT: for i in range (wid): for j in range (hei): gray = int (0.3*im.getpixel ((iMagee j)) [0] + 0.59*im.getpixel ((iMaginj)) [1] + 0.11*im.getpixel ((iMagnej)) [2]) im_new.putpixel ((iMagee j)) New_ grays [gray]) return im_new# image binarization def binarization (img,threshold): # Image binarization operation width,height=img.size im_new = img.copy () for i in range (width): for j in range (height): a = img.getpixel ((I) J)) aa = 0.30a [0] + 0.59a [1] + 0.11a [2] if (aa
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.