RMagick - how do I find out the pixel dimension of an image
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.
Is there a simple way to get image dimensions in Ruby?
libimage-size is a Ruby library for calculating image sizes for a wide variety of graphical formats. A gem is available, or you can download the source tarball and extract the image_size.rb
file.
resizing image with rmagick/imagemagick to be less than a specified product of its width and height
You need to properly calculate the ratio
, which is a square root from your desired dimension divided by (row
multiplied by col
):
row, col = [2793, 1970]
ratio = Math.sqrt(4_000_000.0 / (row * col))
[row, col].map &ratio.method(:*)
#⇒ [
# [0] 2381.400006266842,
# [1] 1679.6842149465374
#]
[row, col].map(&ratio.method(:*)).reduce(:*)
#∞ 3999999.9999999995
RMagick: How to check if all pixels in a particular area of the image are transparent?
I would take your existing image and shrink it to that 3x3 size:
play = image.resize(3, 3, CubicFilter, 0.5)
Then you can create the code by checking each remaining pixel using:
code = ""
(0..2).each do |ix|
(0..2).each do |iy|
code += play.pixel_color(ix,iy).opacity == 65535 ? "0" : "1"
code += iy == 2 ? "-" : "" unless ix == 2 && iy == 2
end
end
I compare opacity to 65535
because when I inspected a pixel I knew was transparent this is what returned:
=> red=65535, green=65535, blue=65535, opacity=65535
Get the colour of a pixel in rMagick
This is done by:
logo.pixel_color(0,0)
Can ImageMagick return the image size?
You could use an extra call to identify
:
convert -size 320x240 image.jpg; identify -format "%[fx:w]x%[fx:h]" image.jpg
rmagick pixel color value
They are stored in a 'quantum depth' of 16-bits. You can rebuild the library to change this. Or you can simply divide each value by 257.
There's a function called MagickExportImagePixels which can get you the 8-bit pixel data that you want. Whenever you perform a transformation etc on an image it will get converted back to 16-bit pixels.
Rmagick each_pixel, how does it work?
The thing is, the array you get back from each_pixel is a new dataset. The data needs to be stored back to the image.
Use get_pixels and store_pixels instead:
img = Magick::ImageList.new('img.jpg').first
pixels = img.get_pixels(0,0,img.columns,img.rows)
for pixel in pixels
avg = (pixel.red + pixel.green + pixel.blue) / 3
pixel.red = avg
pixel.blue = avg
pixel.green = avg
end
img.store_pixels(0,0, img.columns, img.rows, pixels)
img.display
Related Topics
Installing Ruby-2.1.2: Cannot Load Such File -- Openssl (Loaderror)
What Is the Purpose of 'Kernel'
Getting a List of Classes That Include a Module
Error in Celluloid Gem Installation
Ruby on Rails - Doesn't Create Script/Server
Creating a Setter Method That Takes Extra Arguments in Ruby
In Ruby, What Are the Vertical Lines
Rails 3. Simple_Format Do Not Wrap Result in Paragraph Tags
Preserving Case in Http Headers with Ruby's Net:Http
How to Ignore a Folder in Zeitwerk for Rails 6
Ruby on Rails 4 Select Multiple
Check to See If an Array Is Already Sorted
Capistrano and API Keys in Env Variables
How to Get the Current Test Filename from Rspec
What Does the Term "Vendoring" or "To Vendor" Mean for Ruby on Rails