使用PIL

时间:2019-08-13 22:30:10

标签: python image python-imaging-library

目的:程序在x1,y1,x2,y2上迭代,然后使用PIL从128x128图像中裁剪出16 x 16图像。我从0,0,16,16开始播种。要参考如何在PIL中裁剪图像,请查看here-使用坐标裁剪图像。程序

所需的输出:每128x128张图像64张16x16张图像,但我却获得了8张图像作为输出。任何帮助将不胜感激/有关如何改进我的代码的提示。

import os
count2 = 0
from PIL import Image, ImageFont, ImageDraw
count2 = 0
count3 = 0
count4 = 0
count5 = 0
count6 = 0
count7 = 0
fil = 0
count = 0
count8 = 0

def image_creator(x1,y1,x2,y2):
    global img
    global fil
    global filename
    coo = (x1,y1,x2,y2)
    imgsmall = img.crop(coo)
    fil = int(fil)
    fil += 1
    fil = str(fil)
    new_filename = filename + str(fil)+ "ver2.jpg"
    return(imgsmall.save(new_filename))

def new_row(x1, y1, x2, y2):
    x1 = 0
    y1 = y1 + 16
    x2 = 16
    y2 = y2 + 16
    return (x1, y1, x2, y2)
count100 = 0
def move1(x1, y1, x2, y2):
     global count
     global count100
     global count2
     global count3
     global count4
     global count5
     global count6
     global count7
     global count8
     image_creator(x1,y1,x2,y2) #firstL (0,0,16,16) ------ 1
     while count <= 6:
         count += 1
         x1 += 16
         x2 += 16
         image_creator(x1,y1,x2,y2) # ---------- 6 per image
         print(x1,y1,x2,y2) #C
         x1, y1, x2, y2 = new_row(x1, y1, x2, y2)
         print(x1, y1, x2, y2)
         image_creator(x1, y1, x2, y2) # ---------- 1
     while count2 <= 6:
         count2 += 1
         x1 += 16
         x2 += 16
         image_creator(x1, y1, x2, y2) # ----- 6
         print(x1,y1,x2,y2)
     x1, y1, x2, y2 = new_row(x1, y1, x2, y2) # ---- 1
     image_creator(x1, y1, x2, y2)
     print(x1, y1, x2, y2)
     while count3 <= 6:
         count3 += 1
         x1 += 16
         x2 += 16
         image_creator(x1, y1, x2, y2)
         print(x1,y1,x2,y2)
     x1, y1, x2, y2 = new_row(x1, y1, x2, y2)
     image_creator(x1, y1, x2, y2)
     print(x1, y1, x2, y2)
     while count4 <= 6:
         count4 += 1
         x1 += 16
         x2 += 16
         image_creator(x1, y1, x2, y2)
         print(x1,y1,x2,y2)
     x1, y1, x2, y2 = new_row(x1, y1, x2, y2)
     image_creator(x1, y1, x2, y2)
     print(x1, y1, x2, y2)
     while count5 <= 6:
         count5 += 1
         x1 += 16
         x2 += 16
         image_creator(x1, y1, x2, y2)
         print(x1,y1,x2,y2)
     x1, y1, x2, y2 = new_row(x1, y1, x2, y2)
     image_creator(x1, y1, x2, y2)
     print(x1, y1, x2, y2)
     while count6 <= 6:
         count6 += 1
         x1 += 16
         x2 += 16
         image_creator(x1, y1, x2, y2)
         print(x1,y1,x2,y2)
     x1, y1, x2, y2 = new_row(x1, y1, x2, y2)
     print(x1,y1,x2,y2)
     image_creator(x1, y1, x2, y2)
     print(x1, y1, x2, y2)
     while count7 <= 6:
         count7 += 1
         x1 += 16
         x2 += 16
         image_creator(x1, y1, x2, y2)
         print(x1,y1,x2,y2)
     x1, y1, x2, y2 = new_row(x1, y1, x2, y2)
     print(x1,y1,x2,y2)
     image_creator(x1, y1, x2, y2)
     while count8 <= 6:
         count8 += 1
         x1 += 16
         x2 += 16
         image_creator(x1, y1, x2, y2)
         print(x1,y1,x2,y2)
     x1, y1, x2, y2 = new_row(x1, y1, x2, y2)
     print(x1,y1,x2,y2)
     image_creator(x1, y1, x2, y2)
     return (x1, y1, x2, y2)

directory_in_str = "thepath"
directory = os.fsencode(directory_in_str)
for file in os.listdir(directory):
    filename = directory_in_str + "/" + os.fsdecode(file)
    if filename.endswith(".jpg"):
        img = Image.open(filename)
        move1(0,0,16,16)

1 个答案:

答案 0 :(得分:2)

让我们重新设计循环逻辑;您发布的代码是高度重复的,因此代码过长且容易出错。目的是获得所有x个间隔和所有y个间隔的组合,这些间隔明智地为16像素,起始点可被16整除。请使用嵌套的Module1循环:

for

这应该整齐地遍历图像上所需窗口的8x8排列。这样足以让您动起来吗?