【问题标题】:Cropping 128x128 image into 64 16x16 images using PIL使用 PIL 将 128x128 图像裁剪为 64 个 16x16 图像
【发布时间】:2019-12-20 12:01:49
【问题描述】:

目的:程序在 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)

【问题讨论】:

    标签: python image python-imaging-library


    【解决方案1】:

    让我们重新设计循环逻辑;您发布的代码高度重复,使其过长且容易出错。目标是让所有 x 间隔与所有 y 间隔相结合,这些间隔为 16 个像素,并且起点可被 16 整除。使用嵌套的 for 循环执行此操作:

    for x1 in range(0, 128, 16):
        for y1 in range(0, 128, 16):
            x2 = x1 + 16
            y2 = y1 + 16
            image_creator(x1, x2, y1, y2)
    

    这应该巧妙地遍历图像上所需窗口的 8x8 排列。这足以让你感动吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-01
      • 2012-04-16
      • 2013-03-06
      • 2012-12-22
      • 2011-02-25
      • 2018-12-11
      • 2019-03-26
      • 2021-10-01
      相关资源
      最近更新 更多