Guanlin Chen
Guanlin Chen

Reputation: 69

How to re-write the python loop code into a simpler code?

I am trying to crop a square shape area from a single image(.jpg). And I want to crop the pictures from left top and go right then to the next 'layer' like thisSave them with name 1.jpg, 2.jpg....to n.jpg The code below works, however I would like to learn how to shorten the code I use. Thanks!!

And how to crop a circle shape from the image with the same process?

# Improting Image class from PIL module 
from PIL import Image 
import os  
# Opens a image in RGB mode 
im = Image.open(r"C:/Users/User/Desktop/1.jpg") 
out_put_dir = 'C:/Users/User/Desktop/cropped image'
os.chdir(out_put_dir)
# Setting the points for cropped image 
w, h = im.size
print(w, h)
count = 0
for a in range(h+1):
    left = a
    top = 0  ## First layer
    right = 100 + a**strong text**
    bottom = 100  ## First layer
    im1 = im.crop((left, top, right, bottom))
    im1.save('layer 1_'+str(count)+'.'+'jpg')
    count += 1
count = 0
for a in range(h+1):
    left = a
    top = 100 ## Second layer
    right = 100 + a
    bottom = 200  ## Second layer
    im1 = im.crop((left, top, right, bottom))
    im1.save('layer 2_'+str(count)+'.'+'jpg')
    count += 1
count = 0
for a in range(h+1):
    left = a
    top = 200  ## Third layer
    right = 100 + a
    bottom = 300  # Third layer
    im1 = im.crop((left, top, right, bottom))
    im1.save('layer 3_'+str(count)+'.'+'jpg')
    count += 1
count = 0
for a in range(h+1):
    left = a
    top = 300  ## Forth layer
    right = 100 + a
    bottom = 400  # Forth layer
    im1 = im.crop((left, top, right, bottom))
    im1.save('layer 4_'+str(count)+'.'+'jpg')
    count += 1
count = 0
for a in range(h+1):
    left = a
    top = 400  # Fifth layer
    right = 100 + a
    bottom = 500  # Fifth layer
    im1 = im.crop((left, top, right, bottom))
    im1.save('layer 5_'+str(count)+'.'+'jpg')
    count += 1
count = 0
for a in range(h+1):
    left = a
    top = 500 # Sixth layer
    right = 100 + a
    bottom = 600 # Sixth layer
    im1 = im.crop((left, top, right, bottom))
    im1.save('layer 6_'+str(count)+'.'+'jpg')
    count += 1
count = 0
for a in range(h+1):
    left = a
    top = 600 ## Seventh layer
    right = 100 + a
    bottom = 700  ## Seventh layer
    im1 = im.crop((left, top, right, bottom))
    im1.save('layer 7_'+str(count)+'.'+'jpg')
    count += 1

Upvotes: 1

Views: 56

Answers (2)

Isaac Ng
Isaac Ng

Reputation: 86

Restructure your layers as a loop within the current loop.

You have seven layers so within the first loop:

for layer in range(7):
    top = layer * 100
    # use str format to name each layer as well
    name = 'layer_{}'.format(layer)
    # etc

Upvotes: 0

LeopardShark
LeopardShark

Reputation: 4416

You can shorten it significantly with a second for loop.

import os

from PIL import Image

# Opens a image in RGB mode
im = Image.open(r"C:/Users/User/Desktop/1.jpg")
out_put_dir = "C:/Users/User/Desktop/cropped image"
os.chdir(out_put_dir)
# Setting the points for cropped image
w, h = im.size
print(w, h)
for b in range(1, 8):
    count = 0
    for a in range(h + 1):
        left = a
        top = (b - 1) * 100
        right = 100 + a
        bottom = b * 100
        im1 = im.crop((left, top, right, bottom))
        im1.save(f"layer {b}_{count}.jpg")
        count += 1

Upvotes: 1

Related Questions