百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
P

piet

> 编程语言
开源

适用于 Ruby 的完美图像优化器

189 stars0 点赞3 次浏览
访问官网GitHub

工具介绍

适用于 Ruby 的完美图像优化器

Piet

Description

Piet is a gem that optimizes an image stored in a file, and it has integration with CarrierWave uploaders.

This gem is named after the minimalist Dutch painter Piet Mondrian.

Installation

This gem requires two image optimization utilities: optipng and jpegoptim, available in various platforms such as Unix or Windows. You can install them by following the instructions on each authors' page:

  • Installation for optipng
  • Installation for jpegoptim

After installing both utils, simply install the gem:

gem install piet

Easy installation of binaries

While install binaries (like optipng, jpegoptim) is not a rocket science, it can be hard (or even impossible) for some people. Thinking on this a gem called piet-binary was created with theses binaries packed. This is a good approach if you are using Heroku as your host provider, or if you are lazy or impatient ;)

After install piet, just install piet-binary and add to your Gemfile (if you are using Rails):

bash
gem install piet-binary

And in your Gemfile

ruby
gem 'piet'
gem 'piet-binary'

PS 1: it's optional to call piet in your Gemfile, because piet-binary already do this. The same is valid to install the gem via command-line: just install the piet-gem and it will install the piet for you.

PS 2: don't forget to call 'bundle install' if you are using Rails

Usage

You simply require the gem

ruby
require 'piet'

and then call the optimize method:

ruby
Piet.optimize(path, opts)

Not that this will work not by just using the file extension, but by MIME type detection, which should be more reliable.

The options are:

  • verbose: Whether you want to get the output of the command or not. It is interpreted as a Boolean value. Default: false.

  • quality: Adjust the output compression for JPEGs. Valid values are any integer between 0 and 100 (100 means no compression and highest quality). Default: 100

  • level: Adjust the optimization level for PNGs. Valid values are any integer between 0 and 7 (7 means highest compression and longest processing time). Default: 7

CarrierWave integration

As stated before, Piet can be integrated into CarrierWave uploaders. This way, you can optimize the original image or a version.

In order to do that, firstly add piet to your Gemfile:

ruby
gem 'piet'

Then go to your CarrierWave uploader and include Piet's extension:

ruby
class ImageUploader  true)

would optimize that PNG/GIF file and output something similar to this one:

** Processing: piggy.png
340x340 pixels, 4x8 bits/pixel, RGB+alpha
Input IDAT size = 157369 bytes
Input file size = 157426 bytes

Trying:
  zc = 9  zm = 9  zs = 0  f = 1   IDAT size = 156966
  zc = 9  zm = 8  zs = 0  f = 1   IDAT size = 156932

Selecting parameters:
  zc = 9  zm = 8  zs = 0  f = 1   IDAT size = 156932

Output IDAT size = 156932 bytes (437 bytes decrease)
Output file size = 156989 bytes (437 bytes = 0.28% decrease)
  • Optimizing JPEG and getting feedback
ruby
Piet.optimize('/my/wonderful/pics/pony.jpg', :verbose => true)

would optimize that JPEG file and ouput similar to this one:

/my/wonderful/pics/pony.jpg 235x314 24bit JFIF  [OK] 15305 --> 13012 bytes (14.98%), optimized.

Pngquant

You can use Piet to convert 24/32-bit PNG images to paletted (8-bit) PNGs. The conversion reduces file sizes significantly and preserves full alpha transparency.

Simply use Piet like this:

ruby
Piet.pngquant('/a/path/where/you/store/the/file/to/convert')

Please note you have to install the binary in order to use the tool. Simply follow the instructions (and read more info about it) in the official site.

Thanks to @rogercampos for providing the awesome png_quantizator gem, which you can find here.

TODO

  • Add SVG optimization
  • Leave testing files out of the bundled gem
  • Binary tool for optimizing a file
  • Add some testing!

Changelog

  • v.0.2.6 Strip metadata for PNGs (same way we do with JPEGs) thanks to @PikachuEXE.
  • v.0.2.5 Improved CI coverage & dropped support for Ruby 1.9
  • v.0.2.4 Fixed Gemfile issues!
  • v.0.2.3 More efficient treatment of open files thanks to @lavrovdv.
  • v.0.2.2 Lighter gem weight (to be improved in the future).
  • v.0.2.1 More reliable file type detection, by not using the extension but the MIME type. Thanks to @jewlofthelotus! Also, the Carrierwave extension is now compatible with RMagick thanks to @YoranBrondsema.
  • v.0.2.0 Users of the gem can now use piet-binary gem, and bug with filenames containing spaces, parentheses and some other characters is solved too, thanks to @loureirorg. Requiring png_quantizator when it's due, thanks to @jayzes. Finally, specifying the gem version due to @jigfox interest.
  • v.0.1.3 Use png_quantizator gem instead of the own implementation.
  • v.0.1.2 Fixed some problems with missing processing, thanks to @lentg.
  • v.0.1.1 Added support for GIFs. Added an extra option to use pngquant (thanks @rogercampos). Solved problems with Carrierwave >= 0.6 (thanks @mllocs and @huacnlee).
  • v.0.1.0 Optimization of PNGs and JPEGs, including an integration with Carrierwave

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言