I'm a beginner at python.
I would like to import several modules as below and use them in for, but I checked how to pass the value, but I couldn't find it.
Professor, please.
Below, we process multiple images using modules.
At this time, the img, name, etc. cannot be passed to module A, and the directory or file created with func_1.A cannot be passed to module B.
What should I do? (Is it a name-space problem?)
Also, I don't know how to write arguments in this case, and I don't know what to do with the return value.
If you write the required function under for and make it into a single file, you can run it, but I don't want to do it because there are many functions and it's long.
What to do:
importos
import glob
from PIL import Image
....
import module A
import module B
import module C
....
## Read all data
file=glob.glob('dir-name/*.png')
## Per data action
For path in file:
name=os.path.splitext(os.path.basename(path))[0]
img=Image.open(path)
A. func_1(img,name)
B. func_2(img_dir0)
C. func_3(img_dir1)
....
module A.py contains:
importos
from PIL import Image
....
def func_1(img,name):
# create directory
img_dir0='./images/'+'1_'+name+'/1_crop/'
if notos.path.exists(img_dir0):
os.madeirs(img_dir0)
# get size
W = img.width
H=img.height
# crop
Ax = 10
Ay = 15
img1_=img.crop((Ax,Ay,Ax+512,Ay+512))
# Cut out the data into small pieces.
for i in range (0,12):
## double-digit index
N = '{0:02d}'.format(i)
if i<4:
# print('1st row:i=',i)
img1_1 = img1_.crop((0+i*128,0,128+i*128,128))
img1_1.save(img_dir0+N+".png")
.....
return img_dir0
module B.py contains:
importos
import glob
from PIL import Image, ImageOps
....
def func_2(img_dir0):
# create directory
img_dir1=img_dir0+'2_inv/'
if notos.path.exists(img_dir1):
os.madeirs(img_dir1)
# read date created by func_1
file=glob.glob(img_dir0+"*.png")
# image processing:inverted etc.
For path in file:
im_invert=ImageOps.invert(path)
im_invert.save(img_dir1+name+'_inv.png')
.....
return img_dir1
Module B specifies the return value for module A, but it doesn't work.As you pointed out, if the return value of A.func_1 is required, I will prepare two lines of variables for work. What should I do?
"NameError: name 'img_dir0' is not defined"
I understand.
So I should prepare a working variable to receive the return value.
However, this time I cannot receive the name (name of file) used in the interaction in B. Do I need to specify it as a return value for A.func_1 as well?
All I have to do is add it to the argument.
## Work by data
For path in file:
name=os.path.splitext(os.path.basename(path))[0]
img=Image.open(path)
A. func_1(img,name)
img_dir0 = A.func1(img,name)
B. func_2(img_dir0,name)
img_dir1 = B.func2(img_dir0,name)
C. func_3(img_dir1)
varC = func.C(img_dir1)
Simply return the variable value of the folder name (in this case img_dir0
) as the return value of func_1
and specify it as the parameter of func_2
.
func_1
in B.func_2(A.func_1(img,name))
in the main program, you can call both in one line, and you don't need any working variables to remember the return value.A.func_1
is required for another processing of the main program, prepare two lines of variables for work as wellBy the way, the source code of the question article is missing or wrong in many ways, so it does not work as it is.(In order to check only the parameters, I cut the details of the PIL, so I did not check that check.)
Main Program
glob.blob
is a spelling error of glob.glob
func_1.A
and func_2.B
have opposite names before and after .
, and A.func_1
and B.func_2
are correctA.py
func_1
default value for argument with default value is invalid (undefined variable) =XXX
is not required, only arguments can be defined as def func_1(img,name):
B.py
Insufficient import glob
func_2
has an invalid value (undefined variable) as the default value for the default argument.
def func_2(img_dir0):
just like func_1
in A.py
img_dir1=img_dir0+'/2_inv/'
before '/2_inv/'
but not '2_inv/'
?
The indentation is strange for the last three lines, but it must be a transcription error.
Main Program
glob.blob
is a spelling error of glob.glob
func_1.A
and func_2.B
have opposite names before and after .
, and A.func_1
and B.func_2
are correctA.py
func_1
default value for argument with default value is invalid (undefined variable) =XXX
is not required, only arguments can be defined as def func_1(img,name):
B.py
Insufficient import glob
func_2
has an invalid value (undefined variable) as the default value for the default argument.
def func_2(img_dir0):
just like func_1
in A.py
img_dir1=img_dir0+'/2_inv/'
before '/2_inv/'
but not '2_inv/'
?
The indentation is strange for the last three lines, but it must be a transcription error.
Insufficient import glob
func_2
default value for argument with default value is invalid (undefined variable)
def func_2(img_dir0):
just like func_1
in A.py
img_dir1=img_dir0+'/2_inv/'
before '/2_inv/'
and '2_inv/'
should be '2_inv/'
instead of ?
The indentation for the bottom three lines is strange, but it must be a transcription error.
Comments and postscripts:
If A.func_1
→B.func_2
→C.func_3
calls the return value of each previous function as a parameter for the following function, you can do it in one line:
C.func_3(B.func_2(A.func_1(img,name)))
If that's hard to understand or you can't see the way of thinking, please think as follows:
The name of a variable has a valid range to handle it and is called a scope.
For example, the variable img_dir0
in A.func_1()
and B.func_2()
img_dir1 and its name are valid only in each function.
If you return returnimg_dir0
in A.func_1()
, the main program returning from A.func_1()
will not accept the variable named img_dir0
.
The only way to do this is to prepare and store the variables on the main program side that accept the return value of A.func_1()
.
Variables on the main program side can be defined as long as they do not conflict with others.
That's how you put it in the next function parameter, and you put the returned value in the variable and you put it in the next function parameter.
img_dir0=A.func_1(img,name)
img_dir1 = B.func_2(img_dir0)
img_dir2=C.func_3(img_dir1)
add more:
Regarding specifying the name
parameter for B.func_2()
, if it is the same as the name
parameter used for A.func_1()
, you can specify it by using B.func_2(img_dir0,name)
.
A.func_1()
and the name of the N
that you cut out little by little in the img_dir0
folder, so you can cut it out of the path
as you do in the main program.
for path in file:
name=os.path.splitext(os.path.basename(path))[0]
im_invert=ImageOps.invert(path)
im_invert.save(img_dir1+name+'_inv.png')
© 2024 OneMinuteCode. All rights reserved.