Solved

query image dimension

Posted on 2009-05-07
11
518 Views
Last Modified: 2012-05-06
Hi,

           I need to query image file dimension. How is it possible if i have to use python. Alternatively any other way. If not possible with python.

dimension, genre, camera model, file version, product version, product name are attributes that appear in windows explorer right click.

Brgds,

kNish
0
Comment
Question by:eyeqube
  • 5
  • 5
11 Comments
 
LVL 30

Expert Comment

by:flubbster
ID: 24334997
Imagemagick will do everything that you want, I believe. It is open source and can run via python, C++, Perl, and many others including windows. See the link below for details about it and download location, including installation process and requirements.

http://www.imagemagick.org/script/index.php
0
 

Author Comment

by:eyeqube
ID: 24335235
true, imagemagick does it. My question is how do i query the dimension value....
0
 

Author Comment

by:eyeqube
ID: 24335251
specifically, my question is as i mentioned first....how do i query attributes to a file in windows explorer
0
 
LVL 28

Expert Comment

by:pepr
ID: 24339481
I do not have the PythonMagick installed (i.e. the Python wrapper around ImageMagick), so I cannot try that. However, the plain ImageMagick command line utilities are fine for that. Let's start with the dimension.

You want to use the 'identify' utility. It can be also used for setting the values. However, you should focus on the -format option. This one is used for extracting the info from the image. See the snippet below. It accepts one command-line argument -- the filename. Save
it to, say, imgattr.py.

In Windows Explorer, just drag the image over the icon of the script. It will show the cmd console window with the dimensions and then it asks you to press Enter.

This is only to get the idea. Of course, it is possible to extract the information into a variable.
import subprocess

import sys
 

fname = sys.argv[1]

subprocess.call('identify -format "%w x %h" ' + fname )
 

raw_input('\n\n\nEnter...')

Open in new window

0
 
LVL 28

Expert Comment

by:pepr
ID: 24339678
Here is the example how to get the string data into a variable.
import subprocess

import sys
 

fname = sys.argv[1]  # get the argument = image file name
 

# Redirect the standard output of the 'identify' to the pipe

pipe = subprocess.Popen('identify -format "%w x %h" ' + fname, 

                         stdout=subprocess.PIPE).stdout
 

s = pipe.read()  # read the data from the pipe

                 # (the above produces one line, but could be more)

s = s.rstrip()   # strip the line ending

print repr(s)    # use the variable with the dimensions

Open in new window

0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 28

Expert Comment

by:pepr
ID: 24339813
Here is some more complex example that shows some EXIF information. It displays the following for one of my images

C:\tmp\_Python\eyeqube>imgattr.py IMG.JPG
width:  3264
height: 2448
Model:  Canon PowerShot A590 IS
Date:   2009:05:03 11:39:42
import subprocess

import sys
 

fname = sys.argv[1]  # get the argument = image file name
 

# Build the command to get the info

format = 'width:\t%w\nheight:\t%h\nModel:\t%[EXIF:Model]\nDate:\t%[EXIF:DateTime]'

cmd = 'identify -format "%s" %s' % (format, fname)
 

# Redirect the standard output of the 'identify' to the pipe

pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE).stdout
 

# Display the data from the pipe

for line in pipe:

    sys.stdout.write(line)

Open in new window

0
 

Author Comment

by:eyeqube
ID: 24351678
Hi,
        Thank you for sharing thus far. Do you have pythonmagick for windows. I googled it but there is no straight answer for it.
0
 
LVL 28

Expert Comment

by:pepr
ID: 24351949
No, I do not use PythonMagick for Windows. If I recall correctly, there is no binary ready for Windows and I was too lazy to prepare it ;)  Anyway, I do use ImageMagic for automatic conversion of images for our documentation. Calling the command-line utilities (via scripts) works just fine for me and I had no need to invest my time into building PythonMagick binaries.
0
 

Author Comment

by:eyeqube
ID: 24353219
Hi,

         Thank you for sharing. in that case what command will help query the size of an image, using the utility imconvert. I fail to see a query option in this utility
0
 
LVL 28

Accepted Solution

by:
pepr earned 250 total points
ID: 24354598
You can get the size of the image via "identify myimage.jpg" -- then you get one-line answer like

c:\tmp>identify myimage.jpg
myimage.jpg JPEG 560x372 560x372+0+0 DirectClass 8-bit 54.5332kb

In other words, it uses some default format option. If you want to get another form of output, you have to define your own format option, like

c:\tmp>identify -format "%w x %h" myimage.jpg
560 x 372

Notice that %w is a mark that will be replaced by the width, %h by height. The "identify ...." was used in the above samples.

The convert utility cannot be used for querying the image dimensions (i.e. asking for the existing dimensions). The convert utility is used for transformations -- like resampling the image.
0
 

Author Closing Comment

by:eyeqube
ID: 31579329
Thank you for your help. At present I am using the MImage class of maya to get the width and height. I would need a general solution i.e. something which i can run not only in maya(3d software) but also from windows.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now