繁体   English   中英

使用终端在python中找到图像像素?

[英]find image pixels in python using Terminal?

考虑我在XYZ文件夹中有一个image image.png图像。 我需要一个Python脚本,要求用户输入图像名称,输出应为图像像素和大小。

我在终端上使用sips命令尝试如下:

sips -g pixelWidth -g pixelHeight image.png

我需要在Python上包含相同的内容。 我尝试了这段代码

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

但这没有帮助。 我是Python的新手,正在做一些实验。 因此,任何帮助/想法将不胜感激:)

您的逻辑应为:

  1. 检查图像文件是否存在,如果不存在,请退出并显示相应的消息。
  2. 使用图像的完整路径调用函数
  3. 将结果显示给用户。

这样做的方法如下:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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