【发布时间】: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