• Stars
    star
    110
  • Rank 306,101 (Top 7 %)
  • Language
    CoffeeScript
  • License
    MIT License
  • Created almost 7 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

This is a library of image tools for Google Apps Script.

ImgApp

Code Climate Issue Count MIT License

This is a library of image tools for Google Apps Script.

Methods

  1. getSize() : This method is for retrieving the width and height of image as the unit of pixel.

  2. doResize() : This method is for resizing images. The target files are Images, Movies, Google Docs, Microsoft Docs, Text and so on. About the detail information, please check the principle of this method.

  3. updateThumbnail() : This method is for updating thumbnail of files on Google Drive.

  4. editImage() : Edit an image. The image can be cropped. And also, the several images can be merged as an image.

I would like to add the methods for handling images in the future.

How to install

  • Open Script Editor. And please operate follows by click.
  • -> Resource
  • -> Library
  • -> Input Script ID to text box. Script ID is 1T03nYHRho6XMWYcaumClcWr6ble65mAT8OLJqRFJ5lukPVogAN2NDl-y.
  • -> Add library
  • -> Please select latest version
  • -> Developer mode ON (If you don't want to use latest version, please select others.)
  • -> Identifier is "ImgApp". This is set under the default.

If you want to read about Libraries, please check this..

** * The method of doResize() uses Drive API. But, don't worry. Recently, I confirmed that users can use Drive API by only the authorization for Google Services. Users are not necessary to enable Drive API on Google API console. By the authorization for Google Services, Drive API is enabled automatically.**


Methods

1. getSize()

Overview

This method is for retrieving the width and height of image as the unit of pixel.

Description

Unfortunately, there are no methods to directly retrieve the image size at Google Apps Script. As a workaround, there is a method that it imports the image in Google Document and retrieves the size using getWidth() and getHeight(). [A1] But in this method, it uses much time and resources on Google. So I thought of retrieving the information of image at the binary level, and created this. By this, the low process cost could be achieved.

Demo

This is a demonstration for this method. the size information is retrieved from BMP, GIF, PNG and JPG files. The play speed is the real time. From this demo, you can see the speed for retrieving the size information from files.

This sample image is created by k3-studio.

Usage

var blob = DriveApp.getFileById(fileId).getBlob();
var res = ImgApp.getSize(blob);

At first, please retrieve the file blob of image and give it to ImgApp.getSize(). The results can be retrieved as JSON object like below.

res = {
        identification : ### BMP, GIF, PNG and JPG ###,
        width          : ### pixel ###,
        height         : ### pixel ###,
        filesize       : ### bytes ###
}

So if you want width and height, you can retrieve them using as follows.

var blob = DriveApp.getFileById(fileId).getBlob();
var res = ImgApp.getSize(blob);
var width = res.width;
var height = res.height;

Limitation

This method (getSize()) can retrieve the size information from BMP, GIF, PNG and JPG files.


2. doResize()

Overview

This method is for resizing images.

Description

Unfortunately, there are no methods to resize images at Google Apps Script. As a workaround, there is a method that it imports the image in Google Document and resizes the image using setWidth() and setHeight(). But in this method, the resized blob cannot be retrieved. So although I had thought of other workaround, I had not been able to find it. Recently, I accidentally discovered the other workaround doResize(). Since it was found that this workaround can be surely used, I added this to ImgApp.

Demo

This is a demonstration for this method. As a sample, at first, the size of source image is retrieved using getSize(). Then, the source image is resized by the inputted width. The play speed is the real time.

This sample image is created by k3-studio.

Usage

var res = ImgApp.doResize(fileId, width);
  • fileId (string) : File ID
  • width (integer) : Resized width

About file ID, if you want to convert spreadsheet to an image, you can achieve it by inputting file ID of the spreadsheet. But there are some limitations. So please check the limitations.

The results can be retrieved as JSON object like below.

res = {
        blob           : blob,
        identification : ### BMP, GIF, PNG and JPG ###,
        originalwidth  : ### pixel ###,
        originalheight : ### pixel ###,
        resizedwidth   : ### pixel ###,
        resizedheight  : ### pixel ###
}

blob is the blob of resized image. For example, if you want the width, height and the resized image as a file, you can retrieve them using as follows.

var res = ImgApp.doResize(fileId, width);
var width = res.resizedwidth;
var height = res.resizedheight;
DriveApp.createFile(res.blob.setName("filename"));

Principle

If this method had already been known, I apologize.

When I was investigating thumbnail using Drive API, I paid attention to one query in the link of thumbnail. The thumbnail link is different for Google Docs and files except for them as follows.

  • Google Docs
    • https://docs.google.com/feeds/vt?gd=true&id=### fileId ###&v=##&s=#####&sz=s###
  • Files except for Google Docs
    • https://lh3.googleusercontent.com/#####=s###

For these, I noticed a common point. It is =s### in the query parameters. It was found that this parameter meant the size of thumbnail for files as follows.

  1. When the value of =s### is changed, the size of thumbnail is also changed.
  2. For Google Docs (Spreadsheet, Document and Slide) and pdf files, =s### means the height (pixel) of thumbnail.
  3. For files except for Google Docs, =s### means the width (pixel) of thumbnail.
  4. For image and movie files, the maximum value of =s### is the same to the original size of each image.
  5. For Google Docs, pdf, Microsoft Docs (Excel, Word and Powerpoint), Text and so on, the maximum value of =s### is 1024 pixel in the width. The aspect ratio is maintained.

By above results, this method was created.

Although I also wanted to create this method at binary level, I gave up this by the problem of response speed due to the large amount of calculation.

Limitations

This method (doResize()) cannot resize over the original size of the source file. Namely, this cannot magnify the images.

  1. For image and movie files, the maximum size is the same to the original size of each image.
  2. For Google Docs, pdf, Microsoft Docs (Excel, Word and Powerpoint), Text and so on, the maximum size is 1024 pixel in the width. The aspect ratio is maintained.
  3. The thumbnail of standalone Google Apps Script cannot be retrieved.

3. updateThumbnail()

Overview

This method is for updating thumbnail of files on Google Drive using images you selected.

Description

For example, zip files don't have the thumbnail on Google Drive. An icon is shown as the thumbnail. For the most files, Google Drive can create automatically each thumbnail. But there are sometimes files which cannot be created the thumbnail. Zip file is also one of them. In order to add and update thumbnails to such files, I added this method.

Demo

This is a demonstration for this method. The thumbnails of zip files are updated.

This sample images are created by k3-studio.

Usage

var res = ImgApp.updateThumbnail(imgFileId, srcFileId);
  • imgFileId (string) : File ID of new thumbnail image on Google Drive
  • srcFileId (string) : File ID of file, which is updated thumbnail, on Google Drive

The results can be retrieved as JSON object like below.

res = {
    "id": "### file id ###",
    "name": "### file name ###",
    "mimeType": "### mimeType ###",
    "thumbnailLink": "### thumbnail link ###",
    "thumbnailVersion": "#"
}

This method uses multipart-POST request. If you want to know about this, please check here.

Limitations

You can use PNG, JPEG and GIF files as the thumbnail image. And there are some limitations for updating thumbnail. Please confirm the detail information here. Especially following limitations are important for using this method.

  • If Drive can generate a thumbnail from the file, then it will use the generated one and ignore any you may have uploaded.
    • Although I used this script to Google Docs and images, the updated image was not reflected to them. It is considered that this is the limitation.
  • If it can't generate a thumbnail, it will always use yours if you provided one.
    • hasThumbnail of zip files is false. So zip files can update the thumbnail.

Note

In my environment, at the first update, the update had sometimes been failed. But at that time, the second update had worked fine. I don’t know about the reason. I’m sorry.


4. editImage()

In this method, it is required to use the following scopes and APIs.

  • Scope: https://www.googleapis.com/auth/presentations
  • APIs: Drive API and Slides API.
    • Please enable both Drive API and Slides API at Advanced Google services. Ref

Overview

This method is for editing images. In the current stage, the image can be cropped. And several images can be merged as an image.

Description

In the current stage, the image cannot be cropped and retrieved the cropped image as a blob, and also, several images cannot be merged as one image using Google Apps Script. In this method, these can be achieved. The flows are as follows.

Demo

For cropping image

  1. Create new Google Slides with the custom pagesize.
  2. Put an image to the Google Slides.
    • By this, the slide with the custom pagesize can be used as a window for showing image.
  3. Export the slide as an image.

For merging images

  1. Retrieve the image size from all images for merging.
  2. Create new Google Slides with the custom pagesize using the retrieved image size.
  3. Put an image to the Google Slides.
    • By this, the slide with the custom pagesize can be used as a window for showing image.
  4. Export the slide as an image.

Usage: Crop image

const object = {
  blob: imageBlob, // Please set the blob of the image.
  unit: "pixel",
  crop: { t: 50, b: 100, l: 200, r: 100 },
  outputWidth: 800,
};
const blob = ImgApp.editImage(object);
// DriveApp.createFile(blob); // When this line is used, the output image is put to the root folder as a file.
  • blob (Blob) : Blob of the image.
  • unit (String) : Unit for using this method. You can select one from pixel and point.
  • crop (Object) : t, b, l and r are the size from top, bottom, left and right, respectively. In this case, when unit is "pixel", this is used as the pixel.
  • outputWidth (Integer) : Size of width of output image. In this case, when unit is "pixel", this is used as the pixel.

The results can be retrieved as Blob.

Usage: Merge images

const object = {
  merge: [
    [imageBlob1, imageBlob2],
    [imageBlob3, null],
  ], // Please set the blob of the image.
  outputWidth: 800,
  outputFilename: "sample.png",
};
const blob = ImgApp.editImage(object);
// DriveApp.createFile(blob); // When this line is used, the output image is put to the root folder as a file.
  • merge (Blob[][]) : 2 dimensional array including the image Blobs.
    • If you don't want to put the image, at that time, please put null instead of the blob.
  • outputWidth (Integer) : Size of width of output image. In this case, the unit is "pixel".
  • outputFilename (String) : Filename of output blob.

The results can be retrieved as Blob.

Limitations

  • In this case, the maximum size of width of output image is 1,600 pixel. This is due to the limitation of this.
  • The maximum image size which can use for this method is required to be less than 25,000,000 pixels^2. Please be careful this. Ref

Appendix

1. Retrieving Image Size using Google Document

function getSize_doc(blob) {
  var docfile = Drive.Files.insert({
    title: "temp",
    mimeType: "application/vnd.google-apps.document",
  }).getId();
  var img = DocumentApp.openById(docfile).insertImage(0, blob);
  Drive.Files.remove(docfile);
  return { width: img.getWidth(), height: img.getHeight() };
}

2. Inserting Text on Image using Google Apps Script

This is a sample script for inserting a text on an image using Google Apps Script.

3. Reducing Image Data Size using Google Apps Script

This is a sample script for reducing the image data size using Google Apps Script. You might have a situation where you might want to reduce the data size of image data using Google Apps Script. Here, using Google Apps Script, I would like to introduce a sample script for reducing the data size of the image data by reducing the image quality.


Licence

MIT

Author

Tanaike

Update History

  • v1.0.0 (June 27, 2017)

    Initial release. Added getSize()

  • v1.1.0 (June 29, 2017)

    Added new method. Added doResize()

  • v1.2.0 (August 20, 2017)

    Added new method. Added updateThumbnail()

  • v1.2.1 (November 5, 2018)

    Efficiency of each loop was enhanced by this benchmark.

  • v1.2.2 (April 6, 2019)

    By Google's update, the error of "Malformed multipart body." occurs. This error was resolved.

  • v1.2.3 (January 14, 2020)

    Error message was added by #5

  • v1.3.0 (September 24, 2020)

    Added new method. Added editImage()

  • v1.3.1 (December 20, 2022)

    Updated by this pull request.

  • v1.3.2 (January 27, 2023)

    Updated by this report. From this version, the shared Drive can be used.

TOP

More Repositories

1

goodls

This is a CLI tool to download shared files and folders from Google Drive.
Go
388
star
2

taking-advantage-of-google-apps-script

Here, CLI tools, libraries, Add-ons, Reports, Benchmarks and Sample Scripts for taking advantage of Google Apps Script which are publishing in my blog, Gists and GitHub are summarized.
272
star
3

taking-advantage-of-Web-Apps-with-google-apps-script

This is a report to take advantage of Web Apps with Google Apps Script (GAS).
201
star
4

ggsrun

This is a CLI tool to execute Google Apps Script (GAS) at own terminal on local PC. Also this CLI tool can be used for managing files in Google Drive for OAuth2 and Service Account.
Go
117
star
5

ResumableUploadForGoogleDrive_js

This is a Javascript library to achieve the resumable upload for Google Drive.
JavaScript
71
star
6

goris

This is a CLI tool to search for images with Google Reverse Image Search (goris).
Go
68
star
7

Google-Apps-Script-Library-Database

This is for the Google Apps Script Library Database and a web application for searching the libraries..
HTML
57
star
8

RunAll

This is a library for running the concurrent processing using only native Google Apps Script (GAS).
CoffeeScript
55
star
9

FilesApp

FilesApp is a GAS library for retrieving file and folder list in Google Drive using Google Apps Script (GAS). Also this can create a tree from all files and folders in Google Drive.
JavaScript
42
star
10

Resumable_Upload_For_WebApps

This is a sample script for uploading files with large size (> 50 MB) at Web Apps using Google Apps Script (GAS). The resumable upload method is used for uploading files. This script can be also applied to the script using gapi of javascript.
HTML
39
star
11

DownloadLargeFilesByUrl

DownloadLargeFilesByUrl is a GAS library for downloading large files from URL to Google Drive using Google Apps Script (GAS).
JavaScript
38
star
12

GPhotoApp

This is a GAS library for retrieving and creating the albums and media items using Google Photo API using Google Apps Script (GAS).
JavaScript
38
star
13

DocsServiceApp

This is a Google Apps Script library for supporting Document service, Docs API, Spreadsheet service, Sheets API, Slides service and Slides API. The aim of this library is to compensate the processes that they services cannot achieve.
JavaScript
36
star
14

FetchApp

This is a GAS library for creating and requesting the type of multipart/form-data using Google Apps Script. This library enhances Class UelFetchApp of Google Apps Script.
JavaScript
36
star
15

CopyFolder

This is Google Apps Script library for copying folder on Google Drive.
JavaScript
31
star
16

BatchRequest

This is a library for running Batch Requests using Google Apps Script (GAS).
JavaScript
29
star
17

RichTextApp

This is a GAS library for copying the rich text with the text styles from Google Document to Google Spreadsheet or from Google Spreadsheet to Google Document using Google Apps Script (GAS). And, also the rich texts in the cells can be converted to HTML format.
JavaScript
29
star
18

OnedriveApp

This is a library of Google Apps Script for using Microsoft OneDrive.
HTML
25
star
19

HtmlFormApp

This is a Google Apps Script library for parsing the form object from HTML form and appending the submitted values to the Spreadsheet.
JavaScript
25
star
20

AsynchronousResumableUploadForGoogleDrive

This is a sample script for uploading multiple files with large size (> 50 MB) at the sidebar, dialog of Google Docs and Web Apps using the resumable upload of the asynchronous process with Javascript and Google Apps Script (GAS).
HTML
24
star
21

RangeListApp

RangeListApp is a GAS library for retrieving, putting and replacing values for Spreadsheet by a range list with a1Notation using Google Apps Script (GAS).
JavaScript
22
star
22

ManifestsApp

This is a Manifests library for Google Apps Scripts.
JavaScript
22
star
23

gdoctableapppy

This is a python library to manage the tables on Google Document using Google Docs API.
Python
21
star
24

ProjectApp2

This is a GAS project library for Google Apps Script (GAS). This library can be used for the projects of both standalone script type and container-bound script type.
JavaScript
18
star
25

getfilelistpy

This is a python library to retrieve the file list with the folder tree from the specific folder of Google Drive.
Python
18
star
26

go-getfilelist

This is a Golang library to retrieve the file list with the folder tree from the specific folder of Google Drive.
Go
16
star
27

FieldsBuilderForGoogleAPIs

FieldsBuilderForGoogleAPIs is a Web Application for building the fields value for using Google APIs. This is mainly used for developing the scripts for using Google APIs.
JavaScript
12
star
28

UnzipGs

This is a GAS library for unzipping a Zip file protected by a password using Google Apps Script.
JavaScript
12
star
29

GetEditType

GetEditType is a GAS library for retrieving the edit types of the OnEdit event trigger of Spreadsheet using Google Apps Script (GAS).
JavaScript
12
star
30

RearrangeScripts

This is a GAS application for rearranging Google Apps Scripts (GAS) in a project which can be seen at the script editor.
HTML
11
star
31

GetFileList_js

This is a Javascript library to retrieve the file list with the folder tree from the specific folder (publicly shared folders and own folders) of Google Drive.
JavaScript
10
star
32

Creating-PNG-Image-with-Alpha-Channel-using-Google-Apps-Script

This is a sample script for creating a PNG image with the alpha channel using Google Apps Script.
10
star
33

Enhanced-Custom-Function-for-Google-Spreadsheet-using-Web-Apps-as-Wrapper

This is a proposal of the enhanced custom function for Google Spreadsheet using Web Apps as the wrapper.
JavaScript
10
star
34

syncGoogleScriptRun

This is a Javascript library to use "google.script.run" with the synchronous process.
JavaScript
10
star
35

ZipFolder

This is a library for zipping a folder using Google Apps Scripts.
JavaScript
10
star
36

tanaikech.github.io

Blog (my memorandums)
HTML
9
star
37

GmailToList

This is a library for exporting all messages of Gmail as a list using Google Apps Script (GAS).
JavaScript
9
star
38

SOUWA_GAS

GAS library for summing string elements in an array at the high speed
JavaScript
9
star
39

OwnershipTransfer

This is a Google Apps Script library for achieving the ownership-transfer of the specific folder including the files and sub-folders using Drive API.
JavaScript
9
star
40

ProcessApp

This is a library for retrieving the process and information of Google Apps Script. For example, one of methods retrieves the total execution time of all functions executed by the time-driven trigger at owner's account.
JavaScript
9
star
41

One_Time_Download_for_Google_Drive

This is a sample script for downloading files from Google Drive by the one time download method.
JavaScript
8
star
42

DateFinder

DateFinder is a GAS library for searching the date objects from the cell range on the sheet in the Spreadsheet and retrieving the searched range as the RangeList object using Google Apps Script (GAS).
JavaScript
8
star
43

Linking-Google-Cloud-Platform-Project-to-Google-Apps-Script-Project-for-New-IDE

This is the document for linking Google Cloud Platform Project to Google Apps Script Project for New IDE. And also, several sample scripts using Google Apps Script API and Google Photos API are introduced.
8
star
44

node-getfilelist

This is a Node.js module to retrieve the file list with the folder tree from the specific folder of Google Drive.
JavaScript
7
star
45

ShapeApp

This is an add-on application for manipulating shapes on Google Slide. It can create and update shapes by inputting parameters, and can arrange shapes. This is made of Google Apps Scripts (GAS).
HTML
7
star
46

DocNamedRangeApp

This is a Google Apps Script library for managing the named range on Google Documents.
JavaScript
7
star
47

FileTransfer

How to transfer files for Google Drive without authorization.
JavaScript
7
star
48

Managing-A-Lot-Of-Google-Calendar-Events-using-Batch-Requests-with-Google-Apps-Script

These are the sample scripts for managing a lot of Google Calendar Events using the batch requests with Google Apps Script.
JavaScript
7
star
49

Safe-Uploading-for-Google-Drive-by-HTML-in-External-Server-using-Google-Apps-Script

This is a report for safe-uploading files to Google Drive by HTML put in the external server using Google Apps Script.
JavaScript
7
star
50

HtmlFormObjectParserForGoogleAppsScript_js

This is a Javascript library for sending the HTML form object to Google Apps Script using google.script.run.
JavaScript
7
star
51

Batch-Requests-for-Drive-API-using-Google-Apps-Script

These are the sample scripts of the batch requests for Drive API using Google Apps Script.
JavaScript
7
star
52

go-gdoctableapp

This is a Golang library for managing tables on Google Document using Google Docs API.
Go
6
star
53

File_Picker_using_Google_Apps_Script_and_Javascript_without_3rd_party

This is a sample script for the file picker using Google Apps Script and Javascript without 3rd party.
JavaScript
6
star
54

ProjectApp

This is a project library for Google Apps Script (GAS).
JavaScript
6
star
55

GASProjectApp

This is a Google Apps Script library for creating, updating and exporting Google Apps Script project of the standalone type using Drive API. In this case, Apps Script API is not used.
JavaScript
6
star
56

PDFApp

This is a Google Apps Script library for managing PDFs.
JavaScript
5
star
57

SimplePhotoGalleryUsingGoogleAppsScript

This is a sample script for achieving a simple photo gallery created by Google Slides and Web Apps using Google Apps Script.
HTML
5
star
58

FileUploadBydoPost_GAS

GAS and HTML for file Upload From HTML Form on local PC Using doPost()
HTML
5
star
59

getSpreadsheetByRange

This is a simple way for retrieving Spreadsheet ID from a range. As an application, we introduce the enhanced copyTo() which can copy from a range to other Spreadsheet.
5
star
60

GetAccessTokenFromServiceAccount_js

This is a Javascript library to retrieve the access token from the Google Service Account.
JavaScript
4
star
61

UtlApp

This is a Google Apps Script library including useful scripts for supporting to development of applications by Google Apps Script.
JavaScript
4
star
62

getcode

This is a Golang library to automatically get an authorization code for retrieving access token using OAuth2.
Go
4
star
63

RichTextAssistant

This is a GAS library for supporting editing RichText in Google Spreadsheet using Google Apps Script.
JavaScript
3
star
64

ArrangeStackingOrder

ArrangeStackingOrder is a GAS library for arranging the stacking order of page elements on Google Slides using Google Apps Script (GAS).
JavaScript
3
star
65

CropImageByBorder_js

This is a Javascript library for cropping images by the border.
JavaScript
3
star
66

tarUnarchiver-for-Google-Apps-Script

This is a script for extracting files from a tar file using Google Apps Script. This script was created by native Google Apps Script.
JavaScript
3
star
67

CreateImg

This Google Apps Script (GAS) library creates an image file from coordinate data.
JavaScript
2
star
68

gislacks

This is a plugin of Sublime Text 3 for submitting files to both Gist and Slack.
Python
2
star
69

Notifying-Comments-at-Stackoverflow-by-Email

This is a script for sending an email when users got comments at Stackoverflow.
JavaScript
2
star
70

BatchRequest_js

This is a library for running Batch Requests for Google APIs using Javascript.
JavaScript
2
star
71

go-gettokenbyserviceaccount

This is a Golang library to retrieve access token from Service Account of Google without using Google's OAuth2 package.
Go
2
star
72

node-gdoctableapp

This is a Node.js module to manage the tables on Google Document using Google Docs API.
JavaScript
2
star
73

node-gbatchrequests

This is a Node.js module to run the batch requests of Google APIs.
JavaScript
2
star
74

GistChecker

This is a GAS library for notifying the change of number of comments, stars and forks of own Gists as an email using Google Apps Script.
JavaScript
2
star
75

image-storage

HTML
2
star
76

gogauth

This is a CLI tool to easily retrieve access token for using APIs on Google.
Go
2
star
77

ConvertNFDtoNFC

This is a script for converting strings from NFD (Normalization Form Decomposition) to NFC (Normalization Form Composition) using Google Apps Script.
JavaScript
2
star
78

souwapy

Python library for summing string elements in an array at the high speed
Python
1
star
79

cui4netatmo

DOS batch file and UNIX shell script and one liner curl code for retrieving data from Netatmo
Batchfile
1
star
80

go-rearrange

This is a Golang library to interactively rearrange a text data on a terminal.
Go
1
star
81

gislack

This is a CLI tool to submit files to both Gist and Slack.
Go
1
star
82

gorearrange

This is a CLI tool to interactively rearrange a text data on a terminal.
Go
1
star
83

gonetatmo

This is a CLI tool to retrieve data from a personal weather station of Netatmo.
Go
1
star
84

Processing-Various-Types-of-Invoices-using-Gemini-1.5-API-with-Google-Apps-Script

This repository is for Parsing Invoices using Gemini 1.5 API with Google Apps Script.
JavaScript
1
star
85

TemplateApp

This is a Google Apps Script library for easily managing the template of Google Documents and Google Slides using Google Spreadsheet as a database using Google Apps Script.
JavaScript
1
star