Creating a JPG to text converter website would typically involve a user uploading an image and the server processing it using OCR (Optical Character Recognition) software. However, for educational or small-scale use, we can use a client-side JavaScript library called Tesseract.js to perform OCR directly in the browser.
Below is an example of how to create a simple webpage that converts an uploaded JPG image to text using HTML, JavaScript, and a little CSS.
HTML (index.html):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JPG to Text Converter</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div id="content">
<h1>JPG to Text Converter</h1>
<input type="file" id="fileInput" accept=".jpg, .jpeg">
<button id="convertBtn" onclick="convertImage()">Convert to Text</button>
<p id="textOutput"></p>
</div>
<script src="scripts.js"></script>
<!-- Include Tesseract.js from CDN for OCR -->
<script src="https://cdn.jsdelivr.net/npm/tesseract.js@2.1.1/dist/tesseract.min.js"></script>
</body>
</html>
CSS (styles.css):
body {
font-family: 'Arial', sans-serif;
padding: 20px;
background: #f5f5f5;
}
#content {
max-width: 600px;
margin: auto;
background: white;
padding: 20px;
box-shadow: 0 5px 10px rgba(0, 0, 0, 0.1);
}
h1 {
color: #333;
}
#fileInput {
display: block;
margin-bottom: 20px;
}
#textOutput {
white-space: pre-wrap; /* To preserve spaces and line breaks */
background: #ececec;
padding: 10px;
border: 1px solid #ddd;
min-height: 200px;
margin-top: 20px;
}
JavaScript (scripts.js):
function convertImage() {
const outputText = document.getElementById("textOutput");
const fileInput = document.getElementById("fileInput");
if (fileInput.files.length === 0) {
alert("Please select an image file first.");
return;
}
outputText.textContent = "Processing...";
const file = fileInput.files[0];
Tesseract.recognize(
file,
'eng',
{
logger: m => console.log(m) // Log progress to console
}
).then(({ data: { text } }) => {
outputText.textContent = text;
}).catch(err => {
outputText.textContent = "Error: " + err;
});
}
To use the above code, save the HTML to an index.html file, CSS to a styles.css file, and JavaScript to a scripts.js file.
When the user selects an image file and clicks "Convert to Text," the JavaScript code uses Tesseract.js to perform OCR on the client side and display the extracted text. This example requires internet access for loading Tesseract.js from the CDN. Additionally, it's important to note that processing time depends on the size of the image and the device's processing power.
Please be advised that Tesseract.js may not be the best performer for all types of images and its accuracy can vary. For a production environment or if you need high accuracy and speed, you may want to set up a back-end service that uses a more powerful OCR solution.