「ruby/MiniMagick」用MiniMagick处理图片
包的选择和配置
想用RMagick,但据说内存泄露的问题比较厉害,作为替代品MiniMagick不存在内存泄露的问题。而二者都是使用ImageMagick的,所以需要下载并安装ImageMagick。
下面安装ImageMagick:
sudo apt-get install imagemagick
安装gem··「mini_magick」
gem install mini_magick --no-ri --no-rdoc
测试和使用mini_magick
引入gem MiniMagick :
require "mini_magick"
MiniMagick中的Image对象
MiniMagick::Image
img=MiniMagick::Image.open("./pic1.jpg")
[](value)
A rather low-level way to interact with the “identify” command. No nice API here, just the crazy stuff you find in ImageMagick. See the examples listed!
@example
image["format"] #=> "TIFF"
image["height"] #=> 41 (pixels)
image["width"] #=> 50 (pixels)
image["dimensions"] #=> [50, 41]
image["size"] #=> 2050 (bits)
image["original_at"] #=> 2005-02-23 23:17:24 +0000 (Read from Exif data)
image["EXIF:ExifVersion"] #=> "0220" (Can read anything from Exif)
@param format [String] A format for the “identify” command @see For reference seewww.imagemagick.org/script/command-line-options.php#format @return [String, Numeric, Array, Time, Object] Depends on the method called! Defaults to String for unknown commands
write(output_to)
Writes the temporary file out to either a file location (by passing in a String) or by passing in a Stream that you can write(chunk) to repeatedly
@param output_to [IOStream, String] Some kind of stream object that needs to be read or a file path as a String @return [IOStream, Boolean] If you pass in a file location [String] then you get a success boolean. If its a stream, you get it back. Writes the temporary image that we are using for processing to the output path
shave("宽x高")
require ‘mini_magick’
img = MiniMagick::Image.from_file “1.jpg” w,h = img[:width],img[:height] #=> [2048, 1536]#取得宽度和高度
shaved_off = ((w-h)/2).round #=> 256
img.shave “#{shaved_off}x0″ #此处表示宽度上左右各截取256个像素,高度上截取0像素 img.write “2.jpg”
#!/usr/bin/ruby require "mini_magick" def resize_and_crop(image, square_size)
if image[:width]<image[:height]
shave_off=((image[:height]-image[:width])/2).round
image.shave("0x#{shave_off}")
elsif image[:width]>image[:height]
shave_off=((image[:width]-image[:height])/2).round
end
geometry=to_geometry(square_size,square_size)
image.resize(geometry
return image
end new_image=resize_and_crop(MiniMagick::Image.from_file("1.jpg"))
「ruby/MiniMagick」用MiniMagick处理图片的更多相关文章
- 「Ruby && Sqlite3」How to install sqlite3 for ruby? (solve: sqlite-ruby no such file...)
error message: no such file .... 安装 gem install sqlite3-ruby -- --with-sqlite3-dir=/usr/lo ...
- 前端构建工具之gulp(一)「图片压缩」
前端构建工具之gulp(一)「图片压缩」 已经很久没有写过博客了,现下终于事情少了,开始写博吧 今天网站要做一些优化:图片压缩,资源合并等 以前一直使用百度的FIS工具,但是FIS还没有提供图片压缩的 ...
- fir.im Weekly - 如何打造 Github 「爆款」开源项目
最近 Android 转用 Swift 的传闻甚嚣尘上,Swift 的 Github 主页上已经有了一次 merge>>「Port to Android」,让我们对 Swift 的想象又多 ...
- 更新日志 - fir.im「高级统计」功能上线
距离 2016 年到来只剩 10 个日夜,fir.im 也准备了一些新鲜的东西,比如「高级统计」功能和「跳转应用商店」功能,帮助你更好地管理.优化应用,欢迎大家试用反馈:) 新增高级统计功能 这次更新 ...
- Notepad++ 开启「切分窗口」同时检视、比对两份文件
Notepad++ 是个相当好用的免费纯文本编辑器,除了内建的功能相当多之外,也支持外挂模块的方式扩充各方面的应用.以前我都用 UltraEdit 跟 Emeditor,后来都改用免费的 Notepa ...
- 「zigbee - 1」工欲善其事必先利其器 - IAR for 8051 IDE customization
最近在实验室做一些 Zigbee 相关的事情,然而一直没在博客上记录啥东西,也不像原来在公司有动力在 Confluence wiki 上扯东扯西.直到前些阵子,跑到 feibit 论坛上(国内较大的一 ...
- 「C语言」文件的概念与简单数据流的读写函数
写完「C语言」单链表/双向链表的建立/遍历/插入/删除 后,如何将内存中的链表信息及时的保存到文件中,又能够及时的从文件中读取出来进行处理,便需要用到”文件“的相关知识点进行文件的输入.输出. 其实, ...
- 「C语言」Windows+EclipseCDT下的C语言开发环境准备
之前写过一篇 「C语言」在Windows平台搭建C语言开发环境的多种方式 ,讨论了如何在Windows下用DEV C++.EclipseCDT.VisualStudio.Sublime Test.Cl ...
- 如何对抗 WhatsApp「蓝色双勾」-- 3 个方法让你偷偷看讯息
WhatsApp 强制推出新功能「蓝色双勾 (✔✔)」 ,让对方知道你已经看过讯息.一众用户反应极大,因为以后不能再藉口说未看到讯息而不回覆.究竟以后 WhatsApp 是否真的「更难用」? 幸好还有 ...
随机推荐
- 在CentOS6.5上安装Tomcat6
Tomcat安装一向方便,linux的比win的更是这样,基本就是拷贝,类似于win中备受青睐的绿色软件,下面只是记录一下过程. 1.从 http://mirrors.cnnic.cn/apache/ ...
- TCP定时器
http://network.51cto.com/art/201412/459352.htm TCP 是提供可靠的传输层,它使用的方法之一就是确认从另一端收到的数据.但是数据和确认都可能会丢失.TCP ...
- What algorithm to use to normalize someone's face on image
http://stackoverflow.com/questions/7066104/what-algorithm-to-use-to-normalize-someones-face-on-image
- Oracle ORA-12519: TNS:no appropriate service handler found 解决
有时候连得上数据库,有时候又连不上. 可能是数据库上当前的连接数目已经超过了它能够处理的最大值. select count(*) from v$process --当前的连接数select value ...
- QT中静态库的生成与使用
一. 静态库的生成 1. 测试目录: lib 2. 源码文件名: mywindow.h, mywindow.cpp, 类MyWindow继承于QPushButton, 并将文字设置为&qu ...
- linux内核编译相关
参考:http://www.arm.linux.org.uk/docs/kerncomp.php 一. 内核编译1) linux 2.4make clean/make mrpropermake dep ...
- 第八篇 SQL Server安全数据加密
本篇文章是SQL Server安全系列的第八篇,详细内容请参考原文. Relational databases are used in an amazing variety of applicatio ...
- 离线下载Windows 调试符号 Symbols
公司开发机没有不能连接到互联网.调试程序时那些Windows模块(如ntdll.dll)不能加载符号,而程序总是崩在这些模块里.想看一眼到底崩在了什么地方. 需要把对应的符号下载下来. 使用工具sym ...
- NSTimer 线程操作
http://www.jianshu.com/p/0c050af6c5ee 2.NSTimer的创建与撤销必须在同一个线程操作.performSelector的创建与撤销必须在同一个线程操作.
- Virtualbox+UbuntuServer+Xshell搭建Linux开发环境
需求背景 嵌入式开发环境以编译服务器(Linux系统)为核心, 开发人员较多使用Windows系统, 通过若干工具, 可以实现开发人员使用Windows的开发工具,编写代码,然后在Linux系统上编译 ...