Jump to content

How do I keep the order of the photo's that I have taken. [Python, Ubuntu]

Go to solution Solved by tikker,
9 minutes ago, ArchitectTim said:

The file names are decided by google foto's. My question is how can I number de input files in order? It are more than 400 files so I do not want to rename them all manually. Is there any way to look at the meta data and number them in that way? 

Ah so you are taking photos by hand then? Usually cameras will name them by number or put the date in the file name in my experience, so there is probably usuable information that way. If possible using a scanner as suggested above is probably the most straightforward way. If you want to do it programmatically then step 1 is to check what the files are named like. Google Photos doesn't (re)name files as far as I know. It keeps the name you upload to it.

I am working on a project in ubuntu with wsl2. I want to digitize a book. I am planning on making 200 jpg images of the even pages of the book and 200 jpg images of the odd pages. These images wil be saved in google foto's from where I can download them as 2 zip files to ubuntu. I want to give these images to a program that wil make them in to a pdf. This program (img2pdf) wants a .txt file with the names of the img files. To do this I have written a python script that has 2 list of the files, one for even and one for odd and wil merge these list together. the problem is that when I run the code I use the os.listdir method to get the list of files, but this does not give me the files in the order that I have taken the foto's. Bing ai gave me this method to sort them: 

even_files.sort(key=lambda x: os.path.getctime(os.path.join(pathInputEven, x)))

But that does not seam to work. This is the whole python code: 

 

import os

import shutil

  

pathInputEven = "digitizeBoekTemplateProject/InputEven"

pathInputOdd = "digitizeBoekTemplateProject/InputOdd"

outputPath = "digitizeBoekTemplateProject/OrderdInput"

  

if not os.path.exists(outputPath):

    os.makedirs(outputPath)

  
  

# Get the list of files in the input folders

even_files =  os.listdir(pathInputEven)

odd_files = os.listdir(pathInputOdd)

  

# Sort the photos by creation time

even_files.sort(key=lambda x: os.path.getctime(os.path.join(pathInputEven, x)))

odd_files.sort(key=lambda x: os.path.getctime(os.path.join(pathInputOdd, x)))

  

print(even_files)

print(odd_files)

  

# Copy the files to the output folder and rename them

counter = 0

for photo in even_files:

    shutil.copy(pathInputEven + "/" + photo, outputPath + "/" + str(counter) + ".jpg")

    counter += 2

  

counter = 1

for photo in odd_files:

    shutil.copy(pathInputOdd + "/" + photo, outputPath + "/" + str(counter) + ".jpg")

    counter += 2

  
  
  

print("Orderd the photos in OrderInput now creating the txt file")

  
  

orderdPhotos = os.listdir(outputPath)

  
  

txtFile = open(outputPath + "/OrderInputList.txt", "w")

  

i = len(orderdPhotos)

number = 0

while i > 0:

  

    txtFile.write(str(number) + ".jpg\n")

    number += 1

    i -= 1

  

print("Klaar")

TLDR: How do I keep the order of the foto's that I have taken and store this order in a .txt file?

 

I am already working 3 days on this project (not this specific problem) and this is the last problem so if I you can help that would be predicated. 

 

Link to comment
Share on other sites

Link to post
Share on other sites

os.listdir just gives what is in the directory without a guaranteed order. Are the input files numbered in order? If the numbering is in order you can use that to do the sorting on and then iterate over them.

Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9370 | CPU: i5 10510U | RAM: 16 GB

Server: CPU: i5 4690k | RAM: 16 GB | Case: Corsair Graphite 760T White | Storage: 19 TB

Link to comment
Share on other sites

Link to post
Share on other sites

Don't use JPG, use PNG, and scan at 300dpi or whatever the native resolution of your scanner is, if you want to do OCR afterwards. Otherwise, 150 dpi is plenty. 

When the PDF is created, most likely the images will be compressed to JPG at a lower dpi like 100dpi, I guess it depends on the quality settings your tool has defaults or what you choose. 

 

You ca n put the file names in a list  and sort the list using whatever algorithm you like. For a small number of items, any sorting algorithm works. 

 

I would just rename the files 001.jpg , 002.jpg  (pad the file names with 0s so that all files have same length) 

 

Then you can simply do a for (i = 1 ; i<=200; i++ )  { add filename to text file } 

 

In Windows, you can use Irfanview to batch scan a book or something, and you can tell it to auto save each scan and auto name it with a counter (see picture below) 

It works under linux in wine , but not sure scanner drivers will work in wine.

 

image.png.9643e87bb1d13ca8d6811f4956f0d9cb.png

 

In picture above I have Irfanview set so once the OK is hit,  I can just press Alt+S to start scan (I scan whole scanner area, A4) and it will save as ScanImage001, then ScanImage003 and so on.  The scanner window won't close automatically in batch mode, so I don't have to constantly enter file names, I just press Alt+S or hit the Scan button to start scanning.

 

When done with one side, I close the scan tool and start it again, but before hitting OK I say at counter to start from 2, and increment by 2 to get file names ScanImage002, ScanImage004 and so on

 

When done, I can use Irfanview's Batch conversion/Rename feature to crop all odd pages to a specific size, flip or rotate the image (depending how you put the paper on scanner), adjust contrast/colors, then save as PNG in another folder. 

 

Repeat for the other set (you may have different region to crop or rotate a different way, batch process to another folder...

 

Once done and you're happy with the results, you can merge the two folders to a final folder. In windows, a simple DIR /B /ON *.PNG >list.txt would dump only the file names, in alphabetical order  (/B = bare format, just the filename , /O means sort, N means sort by filename) 

 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

even_files.sort(key=lambda x: os.path.getctime(os.path.join(pathInputEven, x)))

This should sort the files by their creation date. Of course that only helps if the creation date of the files is actually the order they were taken in and hasn't been modified by copying them or something. Your best bet would be to name the files appropriately, then sort them alphabetically.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, tikker said:

os.listdir just gives what is in the directory without a guaranteed order. Are the input files numbered in order? If the numbering is in order you can use that to do the sorting on and then iterate over them.

The file names are decided by google foto's. My question is how can I number de input files in order? It are more than 400 files so I do not want to rename them all manually. Is there any way to look at the meta data and number them in that way? 

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, mariushm said:

Don't use JPG, use PNG, and scan at 300dpi or whatever the native resolution of your scanner is, if you want to do OCR afterwards. Otherwise, 150 dpi is plenty. 

When the PDF is created, most likely the images will be compressed to JPG at a lower dpi like 100dpi, I guess it depends on the quality settings your tool has defaults or what you choose. 

 

You ca n put the file names in a list  and sort the list using whatever algorithm you like. For a small number of items, any sorting algorithm works. 

 

I would just rename the files 001.jpg , 002.jpg  (pad the file names with 0s so that all files have same length) 

 

Then you can simply do a for (i = 1 ; i<=200; i++ )  { add filename to text file } 

 

In Windows, you can use Irfanview to batch scan a book or something, and you can tell it to auto save each scan and auto name it with a counter (see picture below) 

It works under linux in wine , but not sure scanner drivers will work in wine.

 

image.png.9643e87bb1d13ca8d6811f4956f0d9cb.png

 

In picture above I have Irfanview set so once the OK is hit,  I can just press Alt+S to start scan (I scan whole scanner area, A4) and it will save as ScanImage001, then ScanImage003 and so on.  The scanner window won't close automatically in batch mode, so I don't have to constantly enter file names, I just press Alt+S or hit the Scan button to start scanning.

 

When done with one side, I close the scan tool and start it again, but before hitting OK I say at counter to start from 2, and increment by 2 to get file names ScanImage002, ScanImage004 and so on

 

When done, I can use Irfanview's Batch conversion/Rename feature to crop all odd pages to a specific size, flip or rotate the image (depending how you put the paper on scanner), adjust contrast/colors, then save as PNG in another folder. 

 

Repeat for the other set (you may have different region to crop or rotate a different way, batch process to another folder...

 

Once done and you're happy with the results, you can merge the two folders to a final folder. In windows, a simple DIR /B /ON *.PNG >list.txt would dump only the file names, in alphabetical order  (/B = bare format, just the filename , /O means sort, N means sort by filename) 

 

 

 

 

This is a interesting method of scanning, I was thinking of scanning the pages by making photo's of them with my phone in a standard using the document scan feature in the photo's app on my phone. The book that I want to scan is old and a little fragile so I preferably don't want to press it against the printer to scan. The naming of files your program uses is exactly what I am looking for, do you know of I could enable that on my phone? I have tested on a few manually sorted jpg files and the OCR worked, but I might try this method with a less fragile book. 

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, ArchitectTim said:

The file names are decided by google foto's. My question is how can I number de input files in order? It are more than 400 files so I do not want to rename them all manually. Is there any way to look at the meta data and number them in that way? 

Ah so you are taking photos by hand then? Usually cameras will name them by number or put the date in the file name in my experience, so there is probably usuable information that way. If possible using a scanner as suggested above is probably the most straightforward way. If you want to do it programmatically then step 1 is to check what the files are named like. Google Photos doesn't (re)name files as far as I know. It keeps the name you upload to it.

Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9370 | CPU: i5 10510U | RAM: 16 GB

Server: CPU: i5 4690k | RAM: 16 GB | Case: Corsair Graphite 760T White | Storage: 19 TB

Link to comment
Share on other sites

Link to post
Share on other sites

15 minutes ago, tikker said:

Ah so you are taking photos by hand then? Usually cameras will name them by number or put the date in the file name in my experience, so there is probably usuable information that way. If possible using a scanner as suggested above is probably the most straightforward way. If you want to do it programmatically then step 1 is to check what the files are named like. Google Photos doesn't (re)name files as far as I know. It keeps the name you upload to it.

the files are named the folowing: IMG_20240105_132940.jpg 

IMG_ means that it is a image

2024 is the year

01 is the month

05 is the day 

_ is seperator

13 is the hour

29 is the minute

40 is the second. 

 

Knowing this als long as I am slower than taking 1 photo a second I think I can sort them by name. Thank you for the suggestion of looking at the file name. I thought that It were just random numbers. I am going to test if this works. 

 

Link to comment
Share on other sites

Link to post
Share on other sites

If you want to use a camera, a simple trick would be to get a transparent plastic storage box  and flip it upside down and make a cutout for the lens of the camera. 

make a cutout on the side of the box to be able to insert the book or flip pages.

 

This way the phone stays at same height in same position (could even use some tape to lock it in place) and the flash of the camera will reflect well on the plastic walls of the box if you decide to use flash. 

 

example of a cheap plastic storage box : https://www.ikea.com/gb/en/p/samla-box-transparent-70102972/

 

There are scanners that have a curved or flat side (some call the feature "zero edge") so you can put books on the side without bending the middle too hard. tech tangents recently made a video with such scanner, he bought a used one ... may be worth looking for one. 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

27 minutes ago, mariushm said:

If you want to use a camera, a simple trick would be to get a transparent plastic storage box  and flip it upside down and make a cutout for the lens of the camera. 

make a cutout on the side of the box to be able to insert the book or flip pages.

 

This way the phone stays at same height in same position (could even use some tape to lock it in place) and the flash of the camera will reflect well on the plastic walls of the box if you decide to use flash. 

 

example of a cheap plastic storage box : https://www.ikea.com/gb/en/p/samla-box-transparent-70102972/

 

There are scanners that have a curved or flat side (some call the feature "zero edge") so you can put books on the side without bending the middle too hard. tech tangents recently made a video with such scanner, he bought a used one ... may be worth looking for one. 

 

 

I already have a phone holder to hold it stil and a big lamp so I have continuity between photo's but thanks for the tip. If I am going to do this more often I wil read more about these but my current budget for this project is 0 euro so I think I wil try first with what I have. 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×