21

How does one find the image width and height in pixels?

image.x_resolution and image.y_resolution both return 0 for some reason.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
fiiv
  • 1,267
  • 1
  • 8
  • 16

2 Answers2

54
require 'rmagick'
img = Magick::Image.ping( 'demo.png' ).first
width = img.columns
height = img.rows

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.

Neil Slater
  • 26,512
  • 6
  • 76
  • 94
  • 2
    If you are only interested in the image metadata, but don't care about the pixel data, you should use `ping` instead of `read`. See [the doc page](http://www.imagemagick.org/RMagick/doc/ilist.html#ping) – Kelvin Sep 18 '13 at 23:21
  • 1
    Slightly cleaner imo: `img = Magick::Image.read('demo.png')[0]; width, height = image.columns, image.rows` – mahemoff May 08 '14 at 09:46
  • 1
    @mahemoff: Edited to remove extra `[0]`s. Answer is really about the slightly obscure method names `columns` and `rows` . . . totally up to you how you want to use them. – Neil Slater May 08 '14 at 09:55
  • Indeed, it's a side issue. I just find the idiom of width, height cleaner here, but hey it's Ruby - timtowtdi – mahemoff May 08 '14 at 10:08
  • Just my 2c here - I'm dubious about the claim that this only reads part of the header block in order to report this data. I base this on a customer experience loading 16 images, each JPG, and the time taken to "ping" ranged from 0.023s up to 1.25s - if only a small header was being read, one would expect this to be a lot more consistent, and definitely less that 1.25s – Nathan Crause Dec 03 '19 at 21:51
  • @Nathan: It may depend on the file format, and JPEG support for the header info in a convenient part of the file may be optional - decided at the time the file was written. If you are getting 1.25s to read a file (which presumably was <100MB even if the whole file was being read) from any modern storage system, then likely something else is wrong in any case. – Neil Slater Dec 04 '19 at 08:40
  • How to get these info when using the R package? The object I get from `image_read` seems like a damn black box, but the properties are displayed in the description!!!! – Andry May 01 '20 at 11:59
  • @Andry: This answer is for Ruby. If you cannot find this question answered for R, then you might like to ask a new question on the site. – Neil Slater May 01 '20 at 12:15
  • @NeilSlater yep... just for the record... function ‘image_info’ should be used – Andry May 01 '20 at 12:17
-3

You can get the image dimensions from img.inspect - instructions included at http://www.imagemagick.org/RMagick/doc/image2.html

An example would be

 f.inspect » "images/Flower_Hat.jpg JPEG 200x250 DirectClass 8-bit 9kb"
MrSynAckSter
  • 1,681
  • 1
  • 18
  • 34
  • The op specifically asked for Rmagick and you gave answer for ImageMagick. Although s/he can figure out the answer from it, this is not the way you should answer a question. – defiant Mar 08 '19 at 11:13