-1

#I want to make a loop as in my images folder there are thousands of images (image name is like t000001xy1c1.tif, t000001xy1c2.tif, t000001xy1c3.tif). How can I manipulate the following scripts to make something so that I can skip every 2nd images sequentially from a list.##

##generating gzip file (black images), 
##keeping it in the same directory and deleting the raw file##

import gzip
import os


#input file name

in_file = "t000001xy1c4.tif"

#reading input file

in_data = open(in_file, "rb").read()

#output file format (making gzip file and deleting the in_file)

out_gz = "t000001xy1c4.tif.gz"

gzf = gzip.open(out_gz, "wb")

gzf.write(in_data)

gzf.close()

#delete the original file after the gzip is done##

os.unlink(in_file)
Mace
  • 1,355
  • 8
  • 13
Taufiq
  • 23
  • 4
  • This script is for zipping a file. If you want to work with a list of files then start searching for examples to get all files in a directory. Look at os.listdir for example. – Mace Nov 03 '20 at 18:25
  • Thanks. Actually I am a beginner in this area. I want to zip my file and delete the original one but struggling to read the files as the file names are big and the folder contains thousands of images. I look for os.listdir and any other suggestion related to my queries will be highly appreciated. – Taufiq Nov 03 '20 at 18:36
  • Here https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory are a lot of examples. If you don't have to keep the images lossless it would be more convenient to convert them to jpg's. – Mace Nov 03 '20 at 18:56
  • Thanks for assisting. I just confused yet I know its simple. From listdir I get the output like- T000001XY01C2 T000001XY02C2 T000001XY03C2 T000001XY04C2 ………. I want to make a loop so that skipping one and work on another image. I can do it if the file name is 1,2,3,4…… How can I do it with this type of file name. – Taufiq Nov 03 '20 at 21:49
  • I have written the solution I would use in the answer. I hope it will help. – Mace Nov 03 '20 at 23:35

1 Answers1

0

According to your comment you already figured out how to use listdir. I myself always use the following function. It can search on multiple extensions at once like ['.tif', '.jpg'].

import os
import re

def get_all_file_paths(dir_path, extensions=[]):
    found_file_paths = []
    dir_entries = os.listdir(dir_path)
    for dir_entry in dir_entries:
        dir_entry_path = os.path.join(dir_path, dir_entry)
        if os.path.isfile(dir_entry_path):
            if extensions:
                # accept files with required extensions --------------------------
                extension = os.path.splitext(dir_entry)[1]
                if extension.lower() in extensions:
                    found_file_paths.append(dir_entry_path)
            else:
                # accept all files -----------------------
                found_file_paths.append(dir_entry_path)

    return found_file_paths


file_paths = get_all_file_paths(r'C:\dir', extensions=['.tif'])

Suppose it returns a list like this

file_paths = [
    r'C:\dir\T000001XY01C2.tif',
    r'C:\dir\T000001XY02C2.tif',
    r'C:\dir\T000001XY03C2.tif',
    r'C:\dir\T000001XY04C2.tif',
    ]

Then I would use regular expressions to extract the numbers you want.

for file in file_paths:

    # get the XY**C*. part
    re_xy_c = r'XY\d+C\d+\.'
    result = re.findall(re_xy_c, os.path.split(file)[1])
    if result:
        xy_c_string = result[0]
        # now get the numbers from the xy_c_string
        re_numbers = r'\d+'
        result = re.findall(re_numbers, xy_c_string)
        first_nr = int(result[0])
        second_nr = int(result[1])
        print(first_nr, second_nr)
        # now make your choice
        if first_nr % 2 == 0:
            # even nr
            print('do even')
        else:
            # odd nr
            print('skip odd')
        print('------------')

Result

1 2
skip odd
------------
2 2
do even
------------
3 2
skip odd
------------
4 2
do even
------------

Use can test your regular expressions here regex101.com

Mace
  • 1,355
  • 8
  • 13