# Split/Crop 3d Image slices into 3d small objects.

**URL:** https://discourse.itk.org/t/split-crop-3d-image-slices-into-3d-small-objects/4001
**Category:** Beginner Questions
**Tags:** image, python
**Created:** [April 1, 2021, 8:42am UTC](https://discourse.itk.org/t/split-crop-3d-image-slices-into-3d-small-objects/4001 "2021-04-01T08:42:32Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Ali009](https://discourse.itk.org/user_avatar/discourse.itk.org/ali009/32/1998_2.png) [@Ali009](https://discourse.itk.org/u/Ali009)
#### Post date: [April 1, 2021, 8:42am UTC](https://discourse.itk.org/t/split-crop-3d-image-slices-into-3d-small-objects/4001/1 "2021-04-01T08:42:32Z")

</div>

Hi. I have grayscale images 720x720x300(image size where 300 slices of 3d image). To generate more examples from available data i cropped the images into multiple small objects. But i just use WxH and i need Z too. I don’t know how to do it in python. Here is the code in which i need Z too to get squared blocks. Thank You.

```auto
for dirname, _, filenames in os.walk(r'E:\E-Papers\MisData\A3d'):
    for filename in filenames:
                    imagepath=(os.path.join(dirname, filename))
                    print(imagepath)
                    if(filename=="desktop.ini"):
                        continue;
                    img = Image.open(imagepath)
                    size = img.size
                    print(size)

                    weight = int(size[0] // n)
                    height = int(size[1] // n)

                    #print(weight, height)
                    cwd = os.getcwd()
                    for j in range(n):
                        for i in range(n):
                         # you will have to add a code line for creating a folder here
                            parent_dir ="slice_"+str(i)+"_"+str(j)
                            box = (weight * i, height * j, weight * (i + 1), height * (j + 1))
                            region = img.crop(box)
                            curr_path =os.path.join(cwd+'\\'+parent_dir)
                            outputfilename = os.path.join(cwd, parent_dir,filename)
                            region.save(outputfilename,'bmp')

```

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [April 1, 2021, 1:31pm UTC](https://discourse.itk.org/t/split-crop-3d-image-slices-into-3d-small-objects/4001/2 "2021-04-01T13:31:19Z")

</div>

You could convert your slice series into a 3D image, see [DICOM](https://itk.org/ITKExamples/src/IO/GDCM/ReadDICOMSeriesAndWrite3DImage/Documentation.html) example. Dealing with 3rd dimension then becomes much easier.

If you take [this chunked processing example](https://github.com/OpenImaging/demos/blob/master/ProcessImageChunks/Code.py) and replace median filter with RegionOfInterest you can do arbitrary processing with the resulting chunk.
