Language Detection Using Python

Language detection using python

Introduction

We will be demonstrating you how to create the project “language detection using python” in this article. Using Tkinter, langdetect, langcodes, and language_data, this project was made. The Tkinter package makes it possible to create GUI programs quickly and simply. A library for language detection is called Langdetect. To work with language codes, use the langcodes library. Langcodes utilize the language_data package to obtain language information. This software features a simple button and label design.

Explanation

A basic Tkinter-based GUI with buttons, labels, and an entry are produced by this code. Users may enter text into the application’s straightforward graphical user interface (GUI), click a button to identify the text’s language, and then see the result presented in the GUI. Language identification is accomplished with the langdetect library, while display name extraction is accomplished with the langcodes library. To handle errors that can arise during the language detection process, the program has error handling. In the event of an error, a message alerting the user to the problem is shown in the result label.

Source Code

import os
from tkinter import Tk, Button, Label, filedialog
from PIL import Image
from reportlab.pdfgen import canvas

class ImageToPDFConverter:
    def __init__(self, master):
        self.master = master
        master.title("Image to PDF Converter")

        self.label = Label(master, text="Select an image file:")
        self.label.pack()

        self.select_button = Button(master, text="Select Image", command=self.select_image)
        self.select_button.pack()

        self.convert_button = Button(master, text="Convert to PDF", command=self.convert_to_pdf, state='disabled')
        self.convert_button.pack()

    def select_image(self):
        file_path = filedialog.askopenfilename(title="Select an image file", filetypes=[("Image files", "*.png;*.jpg;*.jpeg;*.gif;*.bmp")])
        if file_path:
            self.image_path = file_path
            self.convert_button.config(state='normal')
            self.label.config(text=f"Selected image: {os.path.basename(self.image_path)}")

    def convert_to_pdf(self):
        if hasattr(self, 'image_path'):
            image = Image.open(self.image_path)
            pdf_filename = os.path.splitext(os.path.basename(self.image_path))[0] + '.pdf'
            pdf_path = os.path.join(os.path.expanduser("~"), "Downloads", pdf_filename)

            with open(pdf_path, 'wb') as pdf_file:
                pdf = canvas.Canvas(pdf_file)
                pdf.setPageSize((image.width, image.height))

                pdf.drawInlineImage(self.image_path, 0, 0, width=image.width, height=image.height)

                pdf.save()

            self.label.config(text=f"Conversion successful! PDF saved at: {pdf_path}")

if __name__ == "__main__":
    root = Tk()
    app = ImageToPDFConverter(root)
    root.mainloop()

Output

Language detection using python
language detection using python

Leave a Comment