簡體   English   中英

如何將 python 循環代碼重寫為更簡單的代碼?

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

我正在嘗試從單個圖像(.jpg)中裁剪方形區域。 我想從左上角和 go 裁剪圖片,然后像這樣將它們保存到下一個“層”,名稱為 1.jpg, 2.jpg....to n.jpg下面的代碼可以工作,但是我想學習如何縮短我使用的代碼。 謝謝!!

以及如何使用相同的過程從圖像中裁剪出圓形?

# 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

您可以使用第二個for循環顯着縮短它。

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

將您的圖層重組為當前循環中的循環。

你有七層,所以在第一個循環中:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM