Loading repository data…
Loading repository data…
tanaikech / repository
A powerful image processing library for Google Apps Script (GAS) to retrieve image size/DPI via binary parsing, resize images, update thumbnails, crop/merge images, and perform OCR using Google Drive API v3.
A transparent discovery signal based on current public GitHub metadata.
This score does not audit code, security, maintainers, documentation quality, or suitability. Verify the repository and its current documentation before adoption.
This is a library of image tools for Google Apps Script.
[UPDATE v2.0.1] The entire internal architecture has been modernized to ES6+ standards natively leveraging the GAS V8 runtime and Google Drive API v3. High-speed binary parsing logic using DataView has drastically reduced execution times and memory constraints. Backward compatibility of all existing methods remains 100% intact. Now includes native TIFF dimension/DPI parsing, JPEG EXIF/JFIF DPI parsing, doResize() direct Blob input support, and optional target mimeType format conversions.
getSize() : This method is for retrieving the width and height of image as the unit of pixel.
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.
updateThumbnail() : This method is for updating thumbnail of files on Google Drive.
editImage() : Edit an image. The image can be cropped. And also, the several images can be merged as an image.
extractText() : [NEW] Extract text directly from an image (OCR capability) using Google Drive API v3 without needing external Vision APIs.
1T03nYHRho6XMWYcaumClcWr6ble65mAT8OLJqRFJ5lukPVogAN2NDl-y.
(Note: If you have deployed your own customized build of ImgApp, use the Script ID of your deployed deployment after sharing it as "Anyone with link can view").ImgApp". This is set under the default.** * 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.**
This method is for retrieving the width and height of image as the unit of pixel. Now leverages native DataView array buffers for lightning-fast parsing.
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.
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.
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, JPG, and TIFF ###,
width : ### pixel ###,
height : ### pixel ###,
filesize : ### bytes ###,
dpi : ### resolution in DPI (number or null) ###
}
So if you want width, height, and DPI, 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;
var dpi = res.dpi;
This method (getSize()) can retrieve the size and DPI information from BMP, GIF, PNG, JPG, and TIFF files. Note that Google Apps Script restricts standard in-memory blobs to 50MB.
This method is for resizing images.
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.
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.
// Resize using a file ID
var res = ImgApp.doResize(fileId, width);
// Resize using a Blob directly (automatically handles temporary Drive upload/cleanup)
var resBlob = ImgApp.doResize(blob, width);
// Resize and convert the target format to PNG
var resPng = ImgApp.doResize(fileId, width, "image/png");
fileIdOrBlob (string|Blob) : File ID on Google Drive or image Blob.width (integer) : Resized width.mimeType (string) : [Optional] The target mimeType to convert the output image to (e.g. "image/png", "image/jpeg").The results can be retrieved as JSON object like below.
res = {
blob : blob,
identification : ### BMP, GIF, PNG, JPG, or TIFF ###,
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"));
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.
https://docs.google.com/feeds/vt?gd=true&id=### fileId ###&v=##&s=#####&sz=s###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.
=s### is changed, the size of thumbnail is also changed.=s### means the height (pixel) of thumbnail.=s### means the width (pixel) of thumbnail.=s### is the same to the original size of each image.=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.
This method (doResize()) cannot resize over the original size of the source file. Namely, this cannot magnify the images.
This method is for updating thumbnail of files on Google Drive using images you selected.
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.
This is a demonstration for this method. The thumbnails of zip files are updated.
This sample images are created by k3-studio.
var res = ImgApp.updateThumbnail(imgFileId, srcFileId);
imgFileId (string) : File ID of new thumbnail image on Google DrivesrcFileId (string) : File ID of file, which is updated thumbnail, on Google DriveThe 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.
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.
hasThumbnail of zip files is false. So zip files can update the thumbnail.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.
In this method, it is required to use the following scopes and APIs.
https://www.googleapis.com/auth/presentationsThis method is for editing images. In the current stage, the image can be cropped. And several images can be merged as an image.
In the current stage, the