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

Amit Agarwal

Google Developer Expert, Google Cloud Champion

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India.

Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

0

Awards & Titles

Digital Inspiration has won several awards since it's launch in 2004.

Google Developer Expert

Google Developer Expert

Google awarded us the Google Developer Expert award recogizing our work in Google Workspace.

ProductHunt Golden Kitty

ProductHunt Golden Kitty

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

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional (MVP) title for 5 years in a row.

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator title recognizing our technical skill and expertise.

Email Newsletter

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

We will never send any spam emails. Promise.