0

I'm trying to use Rasterio to crop all of the images within a folder. I can crop 1 image individually, but I can't embed/nest my 'with' statement after my 'for' statement so that I can run through a list of file paths - I'd really appreciate any pointers with where I am going wrong with this!

TRAIN_PATH = '/Users/Google Drive/IMG_DATA/Train/'
names = os.listdir(TRAIN_PATH)
all_paths = []
for fn in names:
    all_paths.append(os.path.join(TRAIN_PATH, fn))

I've tried:

for f in all_paths:
   with rasterio.open(f) as src:
       out_image, out_transform = rasterio.mask.mask(src, shapes, crop=True)
       out_meta = src.meta

and

for f in all_paths:
   src = rio.open(f)
   out_image, out_transform = rasterio.mask.mask(src, shapes, crop=True)
   out_meta = src.meta

But get the same error message of: ' not recognized as a supported file format.

I'm using Pycharm 2021.1.1 (if that makes any difference) and I've omitted the rest of the code which actually crops the images since its the 'with' statement which is causing the issue.

GenieV
  • 33
  • 5
  • 4
    You are opening the file twice? Once with `src = rio.open(f)` and other with `with rasterio.open(f) as src:` The script doesn't make sense. – Yoshikage Kira Jun 04 '21 at 17:21
  • 1
    What is `src = rio.open(f)` doing? – wwii Jun 04 '21 at 17:21
  • why are you using the 'with' rather than declaring src = rasterio.open(f)? – K-man Jun 04 '21 at 17:22
  • 3
    I suggest omit `src = rio.open(f)`. using `with` is better since it automatically closes file later. – Yoshikage Kira Jun 04 '21 at 17:24
  • It's not clear exactly what you problem is. **what isn't working?** – juanpa.arrivillaga Jun 04 '21 at 17:29
  • I suspect you want to be looking at what is in all_paths. How do you initialize that? – AMG Jun 04 '21 at 17:50
  • Thanks, I've tried using just src = ... and with rio.open... but seem to get the same error message either way, I've updated my question to show that. @K-man I tried to use the 'with' as it was how I'd already written the code which cropped the images, but I see now that I really only need either the 'for' or the 'with' - not both? – GenieV Jun 04 '21 at 17:53
  • @AMG I've added in how I called my file paths at the top of my question, if that makes it any clearer? – GenieV Jun 04 '21 at 18:09
  • any chance you have a hidden folder in your Train folder? You could check first to see if the file is supported, or wrap the call to rasterio in a try/except to skip the non image files. – AMG Jun 04 '21 at 18:57
  • Thanks for the advice - I only have .jp2 files in my train folder... – GenieV Jun 08 '21 at 14:25
  • I've ran it through again and it appears to crop a random selection of maybe 1 or 2 images, saves them in the folder they were originally in, not the one I designated them to be written to, and produces the following error: ` filepath/ .DS_Store' not recognized as a supported file format.` - but the images it did crop are just the outline, with no actual colour etc. Any pointers would be appreciated! – GenieV Jun 08 '21 at 14:33

0 Answers0