0

image.pngフォルダに画像があるとしますXYZ。ユーザーに画像名を入力するように求める 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が初めてで、いくつかの実験を行っています。だからどんな助け/アイデアも大歓迎です:)

4

1 に答える 1

1

ロジックは次のようになります。

  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)
于 2012-09-26T07:24:41.817 に答える