[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] transform.chop and image cropping



Hey all,
  I'm emailing cause I want to do 2 patches for the next pygame
release - first is add some pygame function that will determine what
the minimum rect in a surface that has meaningful content is, seconds
is I want to fix transform.chop. I want to get advice from the mailing
list on how best to do those things.

---
  With regards to getting the min rect - basically various art
production tools tend to produce output where the meaningful content
(places where non-zero color and/or alpha values) are present is a
subset of the entire image.  Overdrawing the empty area wastes time,
and wastes video memory with OpenGL. People have fast numeric code to
find the meaningful area (but I don't want numeric) and I have pure
python code to do it (but it's slow), so I want to add a neat fast
pygame C routine for it.

So my questions are what should it be called, where should it exist
and should it take any arguments?

my first thoughts are "get_min_rect_with_content" on a surface object,
and it just takes any pixel with non-zero alpha to be meaningful - but
I suppose it could optionally take an alpha and/or color threshold.

---
  With regards to the chop function, It's come up on this mailing list
a few times - from the docs it would seem absolutely clear that it
returns a surface for the area bounded by the rect from the original
surface. http://www.pygame.org/docs/ref/transform.html#pygame.transform.chop
But the code for chop most definitely returns a surface built from the
four corner areas outside of the specified rect (attached is a script
that shows this), as opposed to returning just the chopped area, like
the docs and people's initial assumptions would dictate.

So basically I want to either fix the docs or fix the function. I
perfer to fix the function to do what is expected(it would complement
a min-rect finding function), but if anybody actually ever uses the
chop function as it is for anything at all, then I could see a reason
to just fix the docs (so please, if you use it as is, I'm super
curious as to what you use it for)

... also, one last thing that strikes me about chop, is that it
doesn't see like it doesn't do anything you couldn't do fairly easily
with blits with area-rects and blending disabled. I assume it's just
there to be a convenience function... am I missing out on some part of
it's functionality that you couldn't do with blits?
import pygame
import array

pygame.display.init()
screen = pygame.display.set_mode((320,256))

pygame.font.init()
source_surface = pygame.Surface((60,60), 0, 24)
for x in xrange(3):
    for y in xrange(3):
        pygame.draw.rect(source_surface, (x*127,y*127,255), (x*20,y*20,20,20))

chop_rect = (20,20,20,20)
chop_surface = pygame.transform.chop(source_surface, chop_rect)

expected = pygame.Surface((20,20), 0, 24)
expected.blit(source_surface, (0,0), chop_rect)

screen.blit(source_surface,(50,100))
screen.blit(chop_surface,(150,100))
screen.blit(expected,(250,100))
pygame.display.update()

while 1:
    for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == 27:
                    raise SystemExit
    pygame.display.update()