Solved

query image dimension

Posted on 2009-05-07
11
541 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
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)

 
LVL 29

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 29

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 29

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 29

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 29

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Trying to run a Python File 11 74
Need To Find FireFox Binary Path For Python Selenium On Windows 7 15 707
Edit linux file using python 4 58
OLD TECH - PKZIP Only Zips 7 Files 17 109
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…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

840 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