简体   繁体   中英

find image pixels in python using Terminal?

Consider I have an image image.png in a folder XYZ . I need a Python script which asks the user to enter the image name and the output should be the image pixel and size.

I tried on terminal using sips command as following:

sips -g pixelWidth -g pixelHeight image.png

I need to incorporate the same on Python. I tried with this code

import os
for rootpath,dir,file in os.walk("/Volumes/Macintosh HD/users/username/myfolder"):
    for files in file:      
        def test(name):
            return (os.system(sips -g pixelWidth -g pixelHeight name))

But this is not helpful. I am new to Python and doing some experiments. So any help/idea will be appreciated :)

Your logic should be:

  1. Check to see if the image file exists, if it doesn't, exit with appropriate message.
  2. Call your function with the image's full path
  3. Display the results back to the user.

Here's how to do that:

import os
from subprocess import Popen, PIPE

def run_command(user_input):
    image_path = '/home/user/images/'
    command_to_run = '/usr/bin/sips'

    if not user_input:
        return "You didn't enter anything!"

    if not os.path.exists(image_page+user_input):
        return "I cannot find that image!"
    else:
        output = Popen([command_to_run,
                        "-g", "PixelWidth",
                        "-g", "PixelHeight",
                        image_path+user_input], stdout=PIPE).communicate()[0]
        return "The result is: ",output

 user_input = raw_input("Please enter the image name: ")
 print run_command(user_input)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM