RMagick - 如何找出图像的像素尺寸 [英] RMagick - how do I find out the pixel dimension of an image
问题描述
这可能是一个愚蠢的问题,但是如何找到以像素为单位的图像宽度和高度?
This may be a stupid question, but how does one find the image width and height in pixels?
image.x_resolution
和 image.y_resolution
由于某种原因都返回 0.
image.x_resolution
and image.y_resolution
both return 0 for some reason.
推荐答案
require 'rmagick'
img = Magick::Image.ping( 'demo.png' ).first
width = img.columns
height = img.rows
注意从注释导入的 .ping
方法.如果您需要读取图像进行处理,则使用 Magick::Image.read( 'demo.png' ).first
- ping
的使用在此有效独立代码并加快某些图像类型的处理速度,其中 IM 只能读取标题块.这对于您不需要加载图像本身来处理它的情况很有用.
Note .ping
method imported from comments. If you need to read the image to process it, then use Magick::Image.read( 'demo.png' ).first
- the use of ping
works in this stand-alone code and speeds processing up for some image types where IM can just read a header block. This is useful for cases where you don't need to load the image itself to do work on it.
这篇关于RMagick - 如何找出图像的像素尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!