Hamed
Hamed

Reputation: 83

How to change an image name in for loop into specific number

My code generates different images in different folders. Each folder contains one image . The name of image in each folders is named sequentially for example folder 0 contains image 0.png and folder 1 contains 1.png and folder 2 contains 2.png and so on. But I do not want the code enumerate images and name them sequentially. Instead I want name each image in each folder by 0. Like folder 0 contains 0.png, folder 1 contains 0.png and folder 2 contains 0.png and so on.

from skimage import io

import matplotlib.pyplot as plt

import scipy.io as spio

import numpy as np

import os


pixels = 600

my_dpi = 100

num_geo=10

## Load coordinates

mat = spio.loadmat('coordinateXY.mat', squeeze_me=True)
coord = mat['coordxy']*10

# -----------------------Initialize Geometry--------------------------------------------- 

def geometry_5x3x1024(num_geo):
    for i in range(num_geo):
        fig = plt.figure(num_geo,figsize=( pixels/my_dpi,  pixels/my_dpi),facecolor='k', dpi=my_dpi)  
        plt.axes([0,0,1,1])
        rectangle = plt.Rectangle((-300, -300), 600, 600, fc='k')
        plt.gca().add_patch(rectangle)
        polygon = plt.Polygon(coord[:, :, i],color='w')
        plt.gca().add_patch(polygon)
        plt.axis('off')
        plt.axis([-300,300,-300,300])

        os.mkdir("fig/%d" % i)

        plt.savefig('fig/%d/%d.png' % (i,i), dpi=my_dpi)
        plt.close() 

    return fig 

Upvotes: 0

Views: 372

Answers (1)

Sheldore
Sheldore

Reputation: 39042

Why don't you simply use a hardcoded value as

plt.savefig('fig/%d/0.png' % (i), dpi=my_dpi)

or with a variable

count = 0
plt.savefig('fig/%d/%d.png' % (i, count), dpi=my_dpi)

Upvotes: 1

Related Questions