Aran
Aran

Reputation: 45

How do i fix this String formatting error

I use a function to import images but when I call the function a get the error

not enough arguments for format string.

The first images name is i_00000.tiff and the last one is i_12000.tiff

This is the function that I use, I guess I should have as many %s as the variables I have but I do not understand how to do it

for f in range(x_orig.shape[0]):
        img    = Image.open(dirname + 'img_t1_%s.tiff' % str(f))
        img    = np.array(img)
        x_orig[f] = img


    path = 'labels_reg_holo.csv'    
    labels = pd.read_csv(path)
    y_orig = np.array(labels)

    return x_orig, y_orig



x_orig, y_orig = loadImages()
x_orig = x_orig.reshape(-1, x_orig.shape[1], x_orig.shape[2],1)
x_orig.shape, y_orig.shape

and the error message is

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-83-bdd3c1ba7737> in <module>
----> 1 x_orig, y_orig = loadImages()
      2 x_orig = x_orig.reshape(-1, x_orig.shape[1], x_orig.shape[2],1)
      3 x_orig.shape, y_orig.shape

<ipython-input-82-82957c28e02f> in loadImages()
      5 
      6     for f in range(x_orig.shape[0]):
----> 7         img    = Image.open(dirname + 'img_t1_%s%s.tiff' % str(f))
      8         img    = np.array(img)
      9         x_orig[f] = img

TypeError: not enough arguments for format string

Upvotes: 0

Views: 113

Answers (2)

c0lon
c0lon

Reputation: 210

The code in the stacktrace is different than the code you posted, but the issue is the following line :

img = Image.open(dirname + 'img_t1_%s%s.tiff' % str(f))

There are two %s format tokens and only string being passed, str(f).

Upvotes: 1

Flo
Flo

Reputation: 986

Consider using format() function. Easy to use and easy to read.

Image.open(dirname + 'img_t1_{fname}.tiff'.format(fname=str(f)))

Upvotes: 0

Related Questions