1👍
Welcome @ingIT.
You can accomplish nearly the same code in your view. Since Django is primarily a web framework, you’d probably want to upload an image file, have Django process it with pytesseract, and render the text in the output.
Your html file will need to present the form to upload the file, then post to the Django view to validate and process the file.
I’ve created a sample git repository for you to check out a sample Django application if you like. It produces an experience like the following:
Some of the important bits of the code are shown below:
forms.py nearly direct from docs
from django import forms
class UploadFileForm(forms.Form):
"""
Simplest of forms to provide file upload
"""
file_upload = forms.FileField()
views.py
import tempfile
from PIL import Image
from django.shortcuts import render
from pytesseract import pytesseract
from ocr_sample.forms import UploadFileForm
def ocr_index(request):
context = dict()
# Windows users need to add something like:
# pytesseract.pytesseract.tesseract.cmd = "C:/Program Files/Tesseract-OCR/tesseract.exe"
if request.method == "POST":
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
image = Image.open(request.FILES['file_upload'])
image_text = pytesseract.image_to_string(image)
# If you were handling larger files, perhaps something like this would be better
# with tempfile.TemporaryFile() as temp_file:
# for chunk in request.FILES['file_upload'].chunks():
# temp_file.write(chunk)
# image = Image.open(temp_file)
# image_text = pytesseract.image_to_string(image)
context.update({'image_text': image_text, })
# reset the form: typically we'd redirect elsewhere to prevent multiple
# submissions of data, but there is no database interaction here - only
# sending the text back
form = UploadFileForm()
else:
form = UploadFileForm()
context.update({'form': form, })
return render(request, "ocr_sample/ocr_index.html", context)
👤AMG
Source:stackexchange.com