Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Create digital clock using Python-Turtle
Next article icon

Python is an emerging programming language that consists of many in-built modules and libraries. Which provides support to many web and agile applications. Due to its clean and concise syntax behavior, many gigantic and renowned organizations like Instagram, Netflix so-and-so forth are working in Python work-frame and heading towards magnificent growth. In this article, We are going to build GUI Application for scanning the Visiting card usingTkinter.

Tkinter is a very strong and user-friendly toolkit, as it provides users full control to modify its behavior and attributes of widgets available in the Tkinter library. Since Tkinter provides an object-oriented work-frame it is a far better way of programming for an application. The object-oriented interface in Tkinter satisfies the DRY (Do not repeat yourself) principle and helps to reduce redundancy of the code. 

Py-Libraries required:

Introduction of above-mentioned libraries:

  • Pytesseract library: Pytesseract is an ICR (Intelligent character recognition) and OCR (Optical character recognition) based toolkit available in python. It is a wrapper tool of Google and can"Extract" and "Read" the embedded text in any image. For getting started, Firstly, install and run Pytesseract on your system using the tesseract setup availablehere. To install pytesseract on the shell after installing the application from the above Github link
pip install pytesseract
  • Pillow library: It is a free open source library available in Python for image processing (manipulation, opening, and closing of various file formats i.e. jpeg/png).
pip install Pillow
  • Os module: It is an interactive module that deals with the transaction of files between the program and the operating system. os.path utility module provides the functionality of file sharing.

Program Approach:

  • Firstly, we will create an interface i.e. GUI using various widgets and attributes available in Tkinter like Label, Button, Frame, and so on.
  • After creating a basic layout we are now ready to make it responsive by implementing its main functionality i.e. uploading a file and then converting it.
  • File dialog box logic will work here so that properly formatted images can be uploaded to the software.
  • On successful upload, the system is now ready for conversion and now thePytesseract module role comes into play. Pytesseract module will read and extract the embedded text from the image and will update the text area with that converted text.
  • Also, theFile handling approach in python will create and append a text file with that converted text and will store it in our system's local database.
  • The stored file can be accessed in the future for information and verification purposes.

Below is the full implementation:

Python3
# Visiting Card scanner GUI# imported tkinter libraryfromtkinterimport*importtkinter.messageboxastmsg# Pillow library for importing imagesfromPILimportImage,ImageTk# library for filedialog (For file selection)fromtkinterimportfiledialog# Pytesseract module importingimportpytesseractimportos.pathroot=Tk()# fixing geometry of GUIroot.geometry('800x500')root.maxsize(1000,500)root.minsize(600,500)root.title('Visiting card scanner')# function for uploading file to GUIdefupload_file():globalfilenameglobalstart,lastfilename=filedialog.askopenfilename(initialdir='/Desktop',title='Select a card image',filetypes=(('jpeg files','*.jpg'),('png files','*.png')))iffilename=='':t.delete(1.0,END)t.insert(1.0,'You have not provided any image to convert')tmsg.showwarning(title='Alert!',message='Please provide proper formatted image')returnelse:p_label_var.set('Image uploaded successfully')l.config(fg='#0CDD19')iffilename.endswith('.JPG')orfilename.endswith('.JPEG')orfilename.endswith('.jpg')orfilename.endswith('.jpeg')orfilename.endswith('.PNG')orfilename.endswith('.png'):filename_rev=filename[::-1]last=filename.index('.')start=len(filename)-filename_rev.index('/')-1# function for conversiondefconvert():try:c_label_var.set('Output...')pytesseract.pytesseract.tesseract_cmd=r'C:\Program Files (x86)\Tesseract-OCR\tesseract'text=pytesseract.image_to_string(filename)t.delete(1.0,END)t.insert(1.0,text)root1=Toplevel()root1.title('Uploaded image')img1=ImageTk.PhotoImage(Image.open(filename))Label(root1,image=img1).pack()root1.mainloop()except:t.delete(1.0,END)t.insert(1.0,'You have not provided any image to convert')tmsg.showwarning(title='Alert!',message='Please provide proper formatted image')returnf_name=filename[start+1:last]+'.txt'f_name=os.path.join(r'Database',f_name)f=open(f_name,'w')f.write(text)f.close()# Menu bar and navigation tab creationmainmenu=Menu(root)mainmenu.config(font=('Times',29))m1=Menu(mainmenu,tearoff=0)m1.add_command(label='Scan/Upload Visiting or Business cards and get all the text of cards',font=('Times',13))root.config(menu=mainmenu)mainmenu.add_cascade(label='Aim',menu=m1)m2=Menu(mainmenu,tearoff=0)m2.add_command(label='|| Electronics and Communication engineering student ||',font=('Times',13))m2.add_command(label='|| Coding Enthusiast ||',font=('Times',13))root.config(menu=mainmenu)mainmenu.add_cascade(label='About us',menu=m2)m3=Menu(mainmenu,tearoff=0)m3.add_command(label='E-mail: mathurkartik1234@gmail.com',font=('Times',13))m3.add_separator()m3.add_command(label='Mobile: +91-9587823004',font=('Times',13))m3.add_separator()m3.add_command(label='LinkedIn: https://www.linkedin.com/in/kartik-mathur-97a825160',font=('Times',13))root.config(menu=mainmenu)mainmenu.add_cascade(label='Contact us',menu=m3)Label(text='Visiting card scanner',bg='#FAD2B8',fg='#39322D',font=('Times',18)).pack(fill='x')Label(text='Python GUI',bg='#FAD2B8',fg='#39322D',font=('Times New Roman',12,'italic')).pack(fill='x')f1=Frame()f1.config(bg='white')Label(f1,text='Browse photo to upload',width=20,font=('Times',15),bg='white').pack(side='left')Label(f1,text='format: png/jpeg',bg='white',width=30).pack(side='right',padx=5)Button(f1,text='Upload card',bg='#F58D4B',font=('Times',15),width=70,command=upload_file).pack(side='right')f1.pack(pady=10,fill='x')p_label_var=StringVar()p_label_var.set('Please upload an image to scan')l=Label(textvariable=p_label_var,fg='red',bg='white')l.pack()Label(text='©copyright 2020',bg='#433E3B',fg='white',font=('Times',10)).pack(side='bottom',fill='x')Label(text='Developer: Kartik Mathur',bg='#433E3B',fg='white',font=('Times',10,' italic')).pack(side='bottom',fill='x')t=Text(root,height='9',font=('Times',13))t.pack(side='bottom',fill='x')t.insert(1.0,'Text of converted card will be shown here...',END)c_label_var=StringVar()c_label_var.set('Ready for conversion')c_label=Label(textvariable=c_label_var)c_label.pack(side='bottom',anchor='w')Button(root,text='Scan and Convert',bg='#F58D4B',font=('Times',15),width=70,command=convert).pack(pady='10',side='bottom')root.mainloop()

Output:

Visiting Card Scanner GUI

The above image depicts the application interface.

How to use and How it works?

  • User can scan and upload an image in two formats (i.e. jpeg, png)
  • On clicking "Upload Card" the user will land on the file dialog box and can select a required image to convert.
  • After a successful upload message will be prompted that image uploaded successfully.
  • Now user can convert it by clicking on the button "Scan and Convert".
  • Text information will be shown in the text area and the converted file will be stored in the internal database.

Result and Analysis:

Accuracy and efficiency of conversion are very high, so it is recommended the use of this pytesseract library is a very efficient way of conversion.

Uploaded image
Image embedded text-to-plain text output

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp