0

I have used python to create a huge set of files based on pixels in an image. They are name by their coordinates in the image. when trying to list them by name python recognises the numbers differently to the Windows ordering (which is correct). example

A:) Windows sees 1, 1.png then 1, 2.png then 1, 3.png B:)Python sees 1, 1.png then 1. 10.png then 1, 100.png

A is the list I need as I would like to then rename these images into a convention like Image0000001.png

For context, there are 200 columns and 300 rows.

to combat this, I tried ordering by date created. unfortunately, as the images were created by python there are hundreds created a second and they default to the system above,

is there a way to "see" the number before the comma and after separately so that the script runs through all the numbers in 1, xxx in order before moving onto 2, xxx

Below is the full code with what I've tried so far

# Importing Image from PIL package
from PIL import Image
 
# Creating image object
im = Image.open("C:\\Users\\joeco\\Desktop\\Python-test-image\\joe2.jpg")
px = im.load()

# Defining image width and height
imageSizeW, imageSizeH = im.size

# Running through each pixel coord by column then row
for i in range(1, imageSizeW):
    for j in range(1, imageSizeH):
    
        if px[i, j] == (255, 255, 255):
         pass
        else:
         img = Image.new('RGB', (100, 100), color = (px[i, j]))
         img.save(f"C:\\Users\\joeco\\Desktop\\Python-test-image\\ML\\{i}, {j}.png")
devtoform
  • 17
  • 7
  • 1
    `{x}, {y}.png` is probably a bad choice of filename, specifically the comma. Maybe you could use `{x}-{y}.png` where each co-ordinate has up to N leading zeroes e.g. interpolated as `%04d` or similar, depending on max width/height. Also, why do you need them sorted? – jarmod Jul 25 '22 at 14:04
  • 1
    Does this answer your question? [How to sort glob.glob numerically?](https://stackoverflow.com/questions/62941378/how-to-sort-glob-glob-numerically) – Pranav Hosangadi Jul 25 '22 at 14:21
  • 1
    Note that in the interest of providing a [mre], your `os.chdir...` and `for index, oldfile...` lines should be uncommented – Pranav Hosangadi Jul 25 '22 at 14:22
  • @jarmod thank you. how would I add the %04d formatting to both variables I and J? – devtoform Jul 25 '22 at 14:53
  • 1
    Example: `f"C:\\Users\\joeco\\Desktop\\Python-test-image\\ML\\{i:04}-{j:04}.png"` – jarmod Jul 25 '22 at 14:57
  • @jarmod perfect. i've used `f"C:\\Users\\joeco\\Desktop\\Python-test-image\\ML\\{i}{j:04}.png" ` – devtoform Jul 25 '22 at 15:05

2 Answers2

0

You could run a custom sort over a list with the filenames and then iterate. The statement:

s_imglist = sorted(imglist, key=lambda x: (int(x.split(',')[0]),int(x.split(',')[1].split('.')[0])))

Would reorder the list according to your preference. For example a list with values:

imglist = ['1, 1.png', '1, 10.png', '1, 100.png', '1, 2.png', '1, 3.png', '2, 1.png']

Would turn into:

s_imglist = ['1, 1.png', '1, 2.png', '1, 3.png', '1, 10.png', '1, 100.png', '2, 1.png']

Which you can then iterate.

For reference on custom sorted statements: Python docs on sorting Sorting based on two fields

puigde
  • 16
  • 4
-1

Define the path once:

path = f"C:\\Users\\joeco\\Desktop\\Python-test-image\\ML\\

Then in the for loop:


for ...
img_name = 'image_{0:06d}_{0:06d}'.png'.format(i,j)
img.save(path+img_name )

YScharf
  • 1,638
  • 15
  • 20
  • Thanks for your answer. this however gives me a total of 200 files in the format image_000001_000001 image_000002_000002 etc – devtoform Jul 25 '22 at 14:52