0

Purpose:The program iterates on x1,y1,x2,y2 to then crop 16 x 16 images from a 128x128 image using PIL. I start the crop at 0,0,16,16. To reference how images are cropped in PIL, look here - images are cropped using coordinates. The program

Desired Output: 64 16x16 images per 128x128 image but instead I'm getting 8 images as output. Any help would be appreciated/tips on how I can improve my code.

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)
iiooii
  • 551
  • 2
  • 9
  • 15

1 Answers1

2

Let's redesign the looping logic; your posted code is highly repetitive, making it unduly long and prone to error. The object is to get all x intervals combined with all y intervals, those intervals being 16 pixels wise and having starting points divisible by 16. Do this with nested for loops:

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)

That should neatly iterate through the 8x8 arrangement of desired windows on your image. Is that enough to get you moving?

Prune
  • 76,765
  • 14
  • 60
  • 81