pygame.transform.chop 无法工作
我正在尝试制作一个程序,加载一张图片,然后把它分成不同的部分,并把这些不同的部分保存在一个文件夹里。不过,程序并没有保存我裁剪后的图片,而是把整张图片都保存下来了。
import sys, pygame
from pygame import *
pygame.init()
while True:
image=pygame.image.load(raw_input("Enter the file: "))
rows=int(input("Enter the number of rows: "))
columns=int(input("Enter the number of columns: "))
output=raw_input("Enter the output folder: ")
width=image.get_width()/columns
height=image.get_height()/rows
print ("In progress...")
for i in range(0, rows):
for j in range(0, columns):
cropped_image=pygame.transform.chop(image, (j*columns, i*rows, width, height))
cropped_output=output+"/" + str(i)+"_"+str(j)+".png"
pygame.image.save(cropped_image, cropped_output)
print ("completed")
它不是保存裁剪后的图片(只有图片的一部分),而是保存了整张图片。你知道为什么会这样吗?谢谢!
2 个回答
2
与其使用 pygame.transform.chop,我建议你试试 Surface.subsurface,并使用你指定的矩形区域。在 Pygame 的文档中,关于 pygame.transform.chop 这样写到 -
如果你想要一个“裁剪”,也就是返回图像中某个矩形区域的部分,你可以用一个矩形在新的表面上进行绘制,或者复制一个子表面。
所以在你的例子中,可以这样做:
cropped_image=image.subsurface((j*columns, i*rows, width, height))
2
你正在把东西切成大小为 width
x height
的小块。也就是说,在第 i
行和第 j
列的小块的起始位置是 j*width
和 i*height
,而不是 j*columns
和 i*rows
。