Solved

query image dimension

Posted on 2009-05-07
11
536 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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
 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need a python script 5 96
Copy Files - Python 7 83
Need help with python script - Insert data from user input into MS SQL 35 96
wipe a usb using python 5 48
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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…

809 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