Extract Text from Images with Google Drive OCR

Google Drive supports OCR for image and PDF uploads. That means if you upload a, say, JPEG file containing text, Google Drive can automatically extract the text from the image and save it to an editable Google Document. OCR search is also available in Microsoft OneNote and Evernote except that with Google Docs, the converted text can be saved as well.

There are online tools that let you convert images to text using OCR but did you know that you can use Google Apps Script to easily build a similar tool for free. Build a form that accepts file uploads, publish as a web app and then send the file to Google Drive via Apps Script. The server side script can OCR the image and return the extracted text as output.

/* Credit: https://gist.github.com/tagplus5 */

function doGet(request) {
  var status;

  if (request.parameters.url !== undefined && request.parameters.url !== '') {
    try {
      // Fetch the image data from the web
      var imageBlob = UrlFetchApp.fetch(request.parameters.url).getBlob();

      var resource = {
        title: imageBlob.getName(),
        mimeType: imageBlob.getContentType(),
      };

      // OCR on .jpg, .png, .gif, or .pdf uploads
      var options = {
        ocr: true,
      };

      var docFile = Drive.Files.insert(resource, imageBlob, options);

      var doc = DocumentApp.openById(docFile.id);

      // Extract the text body of the Google Document
      var text = doc.getBody().getText().replace('n', '');

      // Send the document to trash
      Drive.Files.remove(docFile.id);

      status = text;
    } catch (error) {
      status = 'ERROR: ' + error.toString();
    }
  } else {
    status = 'ERROR: No image url specified in the HTTP request';
  }

  return ContentService.createTextOutput(status);
}

Amit Agarwal is a web geek, solo entrepreneur and loves making things on the Internet. Google recently awarded him the Google Developer Expert and Google Cloud Champion title for his work on Google Workspace and Google Apps Script.

Awards & Recognition

Google Developer Expert

Google Developer Expert

Google awarded us the Developer Expert title recogizing our work in Workspace

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional title for 5 years in a row

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator award for technical expertise

Want to stay up to date?
Sign up for our email newsletter.

We will never send any spam emails. Promise 🫶🏻