pygame.transform.chop 不工作

Posted

技术标签:

【中文标题】pygame.transform.chop 不工作【英文标题】:pygame.transform.chop not working 【发布时间】:2012-01-21 03:50:47 【问题描述】:

我正在尝试制作一个程序来加载和成像,然后将其拆分为不同的部分,并将这些不同的部分保存在一个文件夹中。但是,它不会保存裁剪后的图像,而是保存整个图像

 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")

而不是保存cropped_image(仅图像的一部分),而是保存整个图像。知道为什么它不起作用吗? 谢谢

【问题讨论】:

【参考方案1】:

您正在切割大小为width x height 的图块。所以i 行和j 列上的瓦片的原点是j*widthi*height - 不是j*columnsi*rows

【讨论】:

我自己不止一次犯了同样的错误;)【参考方案2】:

我建议您尝试将Surface.subsurface 与您指定的矩形一起使用,而不是使用 pygame.transform.chop。在 pygame.transform.chop 的 Pygame 文档中,它指出 -

如果您想要一个“裁剪”返回矩形内的图像部分,您可以使用矩形对新表面进行 blit 或复制次表面。

因此,对于您的示例,它将是:

cropped_image=image.subsurface((j*columns, i*rows, width, height))

【讨论】:

它裁剪了图像,但它没有按应有的方式裁剪它们,它们不按顺序排列,并且在错误的位置裁剪 你需要调整你的矩形代码来给你想要的作物,我没有测试,因为我没有你的源图像。地下方法是执行裁剪的方法

以上是关于pygame.transform.chop 不工作的主要内容,如果未能解决你的问题,请参考以下文章

马上五十岁了,工作得很不开心,非常郁闷,工作重要还是生活重要?

SKSpriteNode .run 不工作或不完全工作

2017除夕夜的感悟:学习工作不分家,工作生活不分家,读书用兵不分家

cakePHP 准备好的语句不工作(获取所有不工作)

无法返回 - popViewController Animated 不工作,向后滑动也不工作

随机 SQL 连接问题。工作,然后不工作,然后工作,然后 BAM!再也不会工作了?