-1

this code allows you to resize and view the file named "lena-gray.png" in the same folder.

from PIL import Image

jpgfile = Image.open("lena-gray.png")

high = input("Genişliğini giriniz : ")
wid = input("Yüksekliğini giriniz : ");

out = jpgfile.resize((int(high), int(wid)))

out.show()

But instead of entering these values step by step, I want to enter

$ python mywork.py lena-gray.png 50 100 

So I want to run it without opening the file and see the result. Can this be done on Phyton? Can you help me?

----------------------------------------Edit----------------------------------

I updated my code like down. 

import sys
from PIL import Image

firstarg = str(sys.argv[1])
secondarg = int(sys.argv[2])
thirdarg = int(sys.argv[3])

jpgfile = Image.open(firstarg)

yuks = secondarg

gen = thirdarg

out = jpgfile.resize((int(yuks), int(gen)))

out.show()

enter image description here

And my codes work!

enter image description here

Mahmut Bedir
  • 487
  • 1
  • 5
  • 23
  • You may want to update the question. The problem has nothing to do with image processing or algorithms... – NichtJens Oct 09 '17 at 20:08
  • Well, the tags were only an example. The title is also completely off. You are asking about command line arguments in Python. What are "datas" supposed to be? – NichtJens Oct 09 '17 at 20:47
  • I solved! I will edit my answer. That doesn't hardly. – Mahmut Bedir Oct 09 '17 at 21:36
  • The problem with your question is that because tags and title were/are completely off, it's not obvious that this is a duplicate of many older questions (example: https://stackoverflow.com/questions/1009860/how-to-read-process-command-line-arguments). If you/I/someone fixes that, it needed to be flagged as such right away. At first I thought pointers to the docs should be enough, since it seems you simple didn't know the terminology to ask the correct question. If you had known it, a Google search ("python command line arguments") would have been enough. – NichtJens Oct 09 '17 at 22:22

2 Answers2

1

You want to look into argparse.

It's the standard Python way to handle command line arguments.

EDIT: And if you want to check if the file is actually there or allow giving a list of files, look into glob.

EDIT2: This should do:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("filename", help="an image file", type=str)
parser.add_argument("width", help="display width", type=int)
parser.add_argument("height", help="display height", type=int)
clargs = parser.parse_args()


from PIL import Image

jpgfile = Image.open(clargs.filename)
out = jpgfile.resize(clargs.height, clargs.width)
out.show()

EDIT3: If you really want to do it according to the answer by @tendstoZero, this should be correct:

import sys

filename = sys.argv[1]
height = int(sys.argv[2])
width = int(sys.argv[3])


from PIL import Image

jpgfile = Image.open(filename)
out = jpgfile.resize(height, width)
out.show()

You shouldn't cast to int twice, and you shouldn't use meaningless intermediate variables.

NichtJens
  • 1,709
  • 19
  • 27
-1
import sys
firstarg=sys.argv[1]
secondarg=sys.argv[2]
thirdarg=sys.argv[3]
sys.argv[0] should be the script name .

You should be using something like this above snippet in your script.

tendstoZero
  • 146
  • 3
  • Sure. That's possible too. But it has no built-in checks if the arguments are valid, etc. So, argparse is certainly the better choice. – NichtJens Oct 09 '17 at 20:10
  • I want to find that answer, but my problem is so critical. So I must to find my answer quickly. Do you want to help edit my codes? – Mahmut Bedir Oct 09 '17 at 20:17
  • I added a full solution to my answer. I'm still sure you want to read the docs about the two modules I mentioned. – NichtJens Oct 09 '17 at 20:45