Theia
A Ruby gem to
- Transform website URL into PNGs or JPEGs using Puppeteer.
- Compare two images using Node JS Library Pixelmatch
- Compress an image using Sharp Sharp
Installation
Ruby Dependencies
Add this line to your application's Gemfile:
gem 'theia'Node dependencies
nvm use 10.16.3
npm install puppeteer pixelmatch sharpUsage
Screenshot [Google Puppeteer]
# Theia screenshot accepts a valid website URL for initialization.
theia_screenshot = Theia::Screenshot.new(<website_url>)
# Get a screenshot
png = theia_screenshot.to_png(<save_location>, options)
jpeg = theia_screenshot.to_jpeg(<save_location>, options) <save_location> must be in the format of output_file.png or output_file.jpg
Options
options is an object literal with the following properties:
-
full_pageWhen true, takes a screenshot of the full scrollable page. Defaults to false, values:true/false -
deviceDevices which will be used to generate screenshot, values:mobile, iphone, ipad, desktopand their respective landscapes by appending_landscape -
urlValid website URL, eg:https://www.google.com -
qualityThe quality of the image, between0-100. Not applicable to png images, values: 0-100 -
omit_backgroundHides default white background and allows capturing screenshots with transparency. Defaults to false, values:true/false
Image Compare [Pixelmatch]
theia_compare = Theia::ImageCompare.new
# Compare 2 pngs
theia_compare.compare(<file1.png>, <file2.png>, <output.png>, options)Only PNG compare is supported now. JPEG will be added in the near future.
Options
options is an object literal with the following properties:
-
thresholdMatching threshold, ranges from0to1. Smaller values make the comparison more sensitive.0.1by default. -
include_a_aIftrue, disables detecting and ignoring anti-aliased pixels.falseby default. -
alphaBlending factor of unchanged pixels in the diff output. Ranges from0for pure white to1for original brightness.0.1by default. -
aa_colorThe color of anti-aliased pixels in the diff output in[R, G, B]format.[255, 255, 0]by default. -
diff_colorThe color of differing pixels in the diff output in[R, G, B]format.[255, 0, 0]by default. -
diff_maskDraw the diff over a transparent background (a mask), rather than over the original image. Will not draw anti-aliased pixels (if detected).
Compares two images, writes the output diff and returns the number of mismatched pixels.
Image Compress [Sharp]
theia_compress = Theia::ImageCompress.new(<image_type>)
# Compress an image
theia_compare.compress(<file1>, <output>, options)Currently png and jpeg images are supported.
options is an object literal with the following properties:
-
compression_levelzlib compression level, 0-9 (optional, default 9). Only for PNGs -
qualityUse the lowest number of colours needed to achieve given quality, requires libvips compiled with support for libimagequant (optional, default 100) -
forceBoolean force JPEG/PNG output, otherwise attempt to use input format (optional, default true) -
adaptive_filteringUse adaptive row filtering (optional, default false) -
paletteQuantise to a palette-based image with alpha transparency (boolean) -
coloursMaximum number of palette entries, requires libvips compiled with support for libimagequant (optional, default 256) -
progressiveOnly for JPEG. Use progressive (interlace) scan (optional, default false) -
optimise_codingOnly for JPEG. Optimise Huffman coding tables (optional, default true)
Contributions
Feel free to send suggestions, point out issues, or submit pull requests.