Open In App

Visiting Card Scanner GUI Application using Python

Improve
Improve
Like Article
Like
Save
Share
Report

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 using Tkinter.

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 available here. 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 the Pytesseract 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, the File 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 library
from tkinter import *
import tkinter.messagebox as tmsg   
 
# Pillow library for importing images
from PIL import Image, ImageTk
 
# library for filedialog (For file selection)
from tkinter import filedialog
 
# Pytesseract module importing
import pytesseract       
import os.path
 
root = Tk()
 
# fixing geometry of GUI
root.geometry('800x500')       
root.maxsize(1000, 500)
root.minsize(600, 500)
root.title('Visiting card scanner')
 
# function for uploading file to GUI
def upload_file():       
    global filename
    global start, last
    filename = filedialog.askopenfilename(
        initialdir='/Desktop', title = 'Select a card image',
      filetypes=(('jpeg files', '*.jpg'), ('png files', '*.png')))
     
    if filename == '':
        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')
        return
       
    else:
        p_label_var.set('Image uploaded successfully')
        l.config(fg='#0CDD19')
     
    if filename.endswith('.JPG') or filename.endswith('.JPEG') or filename.endswith('.jpg') or filename.endswith('.jpeg') or filename.endswith('.PNG') or filename.endswith('.png'):
        filename_rev = filename[::-1]
        last = filename.index('.')
        start = len(filename) - filename_rev.index('/') - 1
 
# function for conversion
def convert():       
    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')
        return
    f_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 creation
mainmenu = 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



Last Updated : 02 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads