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

      status = text;

    } catch (error) {

      status = "ERROR: " + error.toString();


  } else {

    status = "ERROR: No image url specified in the HTTP request";

  return ContentService.createTextOutput(status);