Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 562
  • Last Modified:

query image dimension

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
eyeqube
Asked:
eyeqube
  • 5
  • 5
1 Solution
 
flubbsterCommented:
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
 
eyequbeAuthor Commented:
true, imagemagick does it. My question is how do i query the dimension value....
0
 
eyequbeAuthor Commented:
specifically, my question is as i mentioned first....how do i query attributes to a file in windows explorer
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
peprCommented:
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
 
peprCommented:
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
 
peprCommented:
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
 
eyequbeAuthor Commented:
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
 
peprCommented:
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
 
eyequbeAuthor Commented:
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
 
peprCommented:
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
 
eyequbeAuthor Commented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now