Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R determine image width and height in pixels

Suppose I have an image on disk, image.jpg. How can I determine image width in pixels using R?

like image 498
Nicholas Hamilton Avatar asked Jul 27 '14 06:07

Nicholas Hamilton


People also ask

How do you find the width and height of an image in pixels?

Open the page with your feed in Chrome. Right-click the image whose size you want to know and select Inspect. View your image's width and height displayed in the Chrome DevTools. (Note, the first number is always the width).

How do you find the resolution with height and width?

You can count this as follows: Full HD is a resolution of 1,920 by 1,080 pixels. Multiply the width of the image by its height (both in pixels) to get the pixel count. Hence, pixel count = 1,920 × 1,080 pixels .

How do I find the physical size of an image?

Formula: Pixels ÷ DPI = Inches. If you know the pixel width and height of an image, this section will calculate the physical size (in inches) of the image when it is printed or displayed on various devices.


1 Answers

You can use the jpeg package. Code should be pretty self-explanatory:

require(jpeg)
img <- readJPEG("myimage.jpg") 

dim(img)
[1] 700 700   3

The same author (Simon Urbanek) also provided the png and tiff package, that have functions with similar syntax (readPNG and readTIFF)

like image 89
nico Avatar answered Nov 10 '22 00:11

nico