def slow_down_sound(sound, rate):
""" returns a sound which is a slowed down version of the original.
rate - at which the sound should be slowed down. eg. 0.5 would be half speed.
"""
raise NotImplementedError()
grow_rate = 1 / rate
# make it 1/rate times longer.
a1 = sndarray.array(sound)
surf = pygame.surfarray.make_surface(a1)
print (a1.shape[0] * grow_rate)
scaled_surf = pygame.transform.scale(surf, (int(a1.shape[0] * grow_rate), a1.shape[1]))
print (scaled_surf)
print (surf)
a2 = a1 * rate
print (a1.shape)
print (a2.shape)
print (a2)
sound2 = sndarray.make_sound(a2.astype(int16))
return sound2
python类transform()的实例源码
def test_scale__destination( self ):
""" see if the destination surface can be passed in to use.
"""
s = pygame.Surface((32,32))
s2 = pygame.transform.scale(s, (64,64))
s3 = s2.copy()
s3 = pygame.transform.scale(s, (64,64), s3)
pygame.transform.scale(s, (64,64), s2)
# the wrong size surface is past in. Should raise an error.
self.assertRaises(ValueError, pygame.transform.scale, s, (33,64), s3)
if 1:
s = pygame.Surface((32,32))
s2 = pygame.transform.smoothscale(s, (64,64))
s3 = s2.copy()
s3 = pygame.transform.smoothscale(s, (64,64), s3)
pygame.transform.smoothscale(s, (64,64), s2)
# the wrong size surface is past in. Should raise an error.
self.assertRaises(ValueError, pygame.transform.smoothscale, s, (33,64), s3)
def test_average_surfaces__24(self):
SIZE = 32
depth = 24
s1 = pygame.Surface((SIZE, SIZE), 0, depth)
s2 = pygame.Surface((SIZE, SIZE), 0, depth)
s3 = pygame.Surface((SIZE, SIZE), 0, depth)
s1.fill((10,10,70, 255))
s2.fill((10,20,70, 255))
s3.fill((10,130,10, 255))
surfaces = [s1, s2, s3]
sr = pygame.transform.average_surfaces(surfaces)
self.assertEqual( sr.get_masks(), s1.get_masks() )
self.assertEqual( sr.get_flags(), s1.get_flags() )
self.assertEqual( sr.get_losses(), s1.get_losses() )
if 0:
print ( sr, s1 )
print ( sr.get_masks(), s1.get_masks() )
print ( sr.get_flags(), s1.get_flags() )
print ( sr.get_losses(), s1.get_losses() )
print ( sr.get_shifts(), s1.get_shifts() )
self.assertEqual(sr.get_at((0,0)), (10,53,50,255))
def todo_test_rotate(self):
# __doc__ (as of 2008-06-25) for pygame.transform.rotate:
# pygame.transform.rotate(Surface, angle): return Surface
# rotate an image
# color = (128, 128, 128, 255)
# s = pygame.Surface((3, 3))
# s.set_at((2, 0), color)
# self.assert_(s.get_at((0, 0)) != color)
# s = pygame.transform.rotate(s, 90)
# self.assert_(s.get_at((0, 0)) == color)
self.fail()
def todo_test_chop(self):
# __doc__ (as of 2008-08-02) for pygame.transform.chop:
# pygame.transform.chop(Surface, rect): return Surface
# gets a copy of an image with an interior area removed
#
# Extracts a portion of an image. All vertical and horizontal pixels
# surrounding the given rectangle area are removed. The corner areas
# (diagonal to the rect) are then brought together. (The original
# image is not altered by this operation.)
#
# NOTE: If you want a "crop" that returns the part of an image within
# a rect, you can blit with a rect to a new surface or copy a
# subsurface.
self.fail()
def slow_down_sound(sound, rate):
""" returns a sound which is a slowed down version of the original.
rate - at which the sound should be slowed down. eg. 0.5 would be half speed.
"""
raise NotImplementedError()
grow_rate = 1 / rate
# make it 1/rate times longer.
a1 = sndarray.array(sound)
surf = pygame.surfarray.make_surface(a1)
print (a1.shape[0] * grow_rate)
scaled_surf = pygame.transform.scale(surf, (int(a1.shape[0] * grow_rate), a1.shape[1]))
print (scaled_surf)
print (surf)
a2 = a1 * rate
print (a1.shape)
print (a2.shape)
print (a2)
sound2 = sndarray.make_sound(a2.astype(int16))
return sound2
def test_scale__destination( self ):
""" see if the destination surface can be passed in to use.
"""
s = pygame.Surface((32,32))
s2 = pygame.transform.scale(s, (64,64))
s3 = s2.copy()
s3 = pygame.transform.scale(s, (64,64), s3)
pygame.transform.scale(s, (64,64), s2)
# the wrong size surface is past in. Should raise an error.
self.assertRaises(ValueError, pygame.transform.scale, s, (33,64), s3)
if 1:
s = pygame.Surface((32,32))
s2 = pygame.transform.smoothscale(s, (64,64))
s3 = s2.copy()
s3 = pygame.transform.smoothscale(s, (64,64), s3)
pygame.transform.smoothscale(s, (64,64), s2)
# the wrong size surface is past in. Should raise an error.
self.assertRaises(ValueError, pygame.transform.smoothscale, s, (33,64), s3)
def test_average_surfaces__24(self):
SIZE = 32
depth = 24
s1 = pygame.Surface((SIZE, SIZE), 0, depth)
s2 = pygame.Surface((SIZE, SIZE), 0, depth)
s3 = pygame.Surface((SIZE, SIZE), 0, depth)
s1.fill((10,10,70, 255))
s2.fill((10,20,70, 255))
s3.fill((10,130,10, 255))
surfaces = [s1, s2, s3]
sr = pygame.transform.average_surfaces(surfaces)
self.assertEqual( sr.get_masks(), s1.get_masks() )
self.assertEqual( sr.get_flags(), s1.get_flags() )
self.assertEqual( sr.get_losses(), s1.get_losses() )
if 0:
print ( sr, s1 )
print ( sr.get_masks(), s1.get_masks() )
print ( sr.get_flags(), s1.get_flags() )
print ( sr.get_losses(), s1.get_losses() )
print ( sr.get_shifts(), s1.get_shifts() )
self.assertEqual(sr.get_at((0,0)), (10,53,50,255))
def todo_test_rotate(self):
# __doc__ (as of 2008-06-25) for pygame.transform.rotate:
# pygame.transform.rotate(Surface, angle): return Surface
# rotate an image
# color = (128, 128, 128, 255)
# s = pygame.Surface((3, 3))
# s.set_at((2, 0), color)
# self.assert_(s.get_at((0, 0)) != color)
# s = pygame.transform.rotate(s, 90)
# self.assert_(s.get_at((0, 0)) == color)
self.fail()
def todo_test_chop(self):
# __doc__ (as of 2008-08-02) for pygame.transform.chop:
# pygame.transform.chop(Surface, rect): return Surface
# gets a copy of an image with an interior area removed
#
# Extracts a portion of an image. All vertical and horizontal pixels
# surrounding the given rectangle area are removed. The corner areas
# (diagonal to the rect) are then brought together. (The original
# image is not altered by this operation.)
#
# NOTE: If you want a "crop" that returns the part of an image within
# a rect, you can blit with a rect to a new surface or copy a
# subsurface.
self.fail()
def avgColor(self):
self.refresh()
if self._avgColor is None:
self._avgColor = pygame.transform.average_color(self._srf)
return self._avgColor
def image(self):
if self.stale:
self._srf = self.render()
self._rotated = None
self.stale = False
self._avgColor = None
srf = self._srf
a = self.angle
if a:
r = self._rotated
if r and r[0] == a: srf = r[1]
else:
srf = pygame.transform.rotate(srf, -a)
self._rotated = a, srf
return srf
def avgColor(self):
if self._avgColor is None:
self._avgColor = pygame.transform.average_color(self._srf.original)
return self._avgColor
def test_scale__alpha( self ):
""" see if set_alpha information is kept.
"""
s = pygame.Surface((32,32))
s.set_alpha(55)
self.assertEqual(s.get_alpha(),55)
s = pygame.Surface((32,32))
s.set_alpha(55)
s2 = pygame.transform.scale(s, (64,64))
s3 = s.copy()
self.assertEqual(s.get_alpha(),s3.get_alpha())
self.assertEqual(s.get_alpha(),s2.get_alpha())
def test_threshold__uneven_colors(self):
(w,h) = size = (16, 16)
original_surface = pygame.Surface(size, pygame.SRCALPHA, 32)
dest_surface = pygame.Surface(size, pygame.SRCALPHA, 32)
original_surface.fill(0)
threshold_color_template = [5, 5, 5, 5]
threshold_template = [6, 6, 6, 6]
################################################################
for pos in range(len('rgb')):
threshold_color = threshold_color_template[:]
threshold = threshold_template[:]
threshold_color[pos] = 45
threshold[pos] = 50
pixels_within_threshold = pygame.transform.threshold (
dest_surface, original_surface, threshold_color,
threshold,
0, # diff_color
0 # change_return
)
self.assertEqual(w*h, pixels_within_threshold)
################################################################
def test_average_surfaces(self):
"""
"""
SIZE = 32
s1 = pygame.Surface((SIZE, SIZE))
s2 = pygame.Surface((SIZE, SIZE))
s3 = pygame.Surface((SIZE, SIZE))
s1.fill((10,10,70))
s2.fill((10,20,70))
s3.fill((10,130,10))
surfaces = [s1, s2, s3]
surfaces = [s1, s2]
sr = pygame.transform.average_surfaces(surfaces)
self.assertEqual(sr.get_at((0,0)), (10,15,70,255))
self.assertRaises(TypeError, pygame.transform.average_surfaces, 1)
self.assertRaises(TypeError, pygame.transform.average_surfaces, [])
self.assertRaises(TypeError, pygame.transform.average_surfaces, [1])
self.assertRaises(TypeError, pygame.transform.average_surfaces, [s1, 1])
self.assertRaises(TypeError, pygame.transform.average_surfaces, [1, s1])
self.assertRaises(TypeError, pygame.transform.average_surfaces, [s1, s2, 1])
self.assertRaises(TypeError, pygame.transform.average_surfaces, (s for s in [s1, s2,s3] ))
def test_average_color(self):
"""
"""
a = [24, 32]
for i in a:
s = pygame.Surface((32,32), 0, i)
s.fill((0,100,200))
s.fill((10,50,100), (0,0,16,32))
self.assertEqual(pygame.transform.average_color(s),(5,75,150,0))
self.assertEqual(pygame.transform.average_color(s, (16,0,16,32)), (0,100,200,0))
def test_rotate__lossless_at_90_degrees(self):
w, h = 32, 32
s = pygame.Surface((w, h), pygame.SRCALPHA)
gradient = list(test_utils.gradient(w, h))
for pt, color in gradient: s.set_at(pt, color)
for rotation in (90, -90):
s = pygame.transform.rotate(s,rotation)
for pt, color in gradient:
self.assert_(s.get_at(pt) == color)
def test_get_smoothscale_backend(self):
filter_type = pygame.transform.get_smoothscale_backend()
self.failUnless(filter_type in ['GENERIC', 'MMX', 'SSE'])
# It would be nice to test if a non-generic type corresponds to an x86
# processor. But there is no simple test for this. platform.machine()
# returns process version specific information, like 'i686'.
def test_set_smoothscale_backend(self):
# All machines should allow 'GENERIC'.
original_type = pygame.transform.get_smoothscale_backend()
pygame.transform.set_smoothscale_backend('GENERIC')
filter_type = pygame.transform.get_smoothscale_backend()
self.failUnlessEqual(filter_type, 'GENERIC')
# All machines should allow returning to original value.
# Also check that keyword argument works.
pygame.transform.set_smoothscale_backend(type=original_type)
# Something invalid.
def change():
pygame.transform.set_smoothscale_backend('mmx')
self.failUnlessRaises(ValueError, change)
# Invalid argument keyword.
def change():
pygame.transform.set_smoothscale_backend(t='GENERIC')
self.failUnlessRaises(TypeError, change)
# Invalid argument type.
def change():
pygame.transform.set_smoothscale_backend(1)
self.failUnlessRaises(TypeError, change)
# Unsupported type, if possible.
if original_type != 'SSE':
def change():
pygame.transform.set_smoothscale_backend('SSE')
self.failUnlessRaises(ValueError, change)
# Should be back where we started.
filter_type = pygame.transform.get_smoothscale_backend()
self.failUnlessEqual(filter_type, original_type)
def todo_test_flip(self):
# __doc__ (as of 2008-08-02) for pygame.transform.flip:
# pygame.transform.flip(Surface, xbool, ybool): return Surface
# flip vertically and horizontally
#
# This can flip a Surface either vertically, horizontally, or both.
# Flipping a Surface is nondestructive and returns a new Surface with
# the same dimensions.
self.fail()
def todo_test_rotozoom(self):
# __doc__ (as of 2008-08-02) for pygame.transform.rotozoom:
# pygame.transform.rotozoom(Surface, angle, scale): return Surface
# filtered scale and rotation
#
# This is a combined scale and rotation transform. The resulting
# Surface will be a filtered 32-bit Surface. The scale argument is a
# floating point value that will be multiplied by the current
# resolution. The angle argument is a floating point value that
# represents the counterclockwise degrees to rotate. A negative
# rotation angle will rotate clockwise.
self.fail()
start.py 文件源码
项目:Physical-Image-Manipulation-Program
作者: philipptrenz
项目源码
文件源码
阅读 31
收藏 0
点赞 0
评论 0
def ui():
"""
warp the image according to the current tile locations. The whole image processing is done here.
"""
global warped_surface
counter = 0
while capture:
if not screen_is_locked_manually:
start = time.time()
temp = pygame.transform.rotate(screen.copy(), 90)
img = numpy.copy(pygame.surfarray.pixels3d(temp))
circle_coords = cv.detect_colored_circles(img, radius_range, hsv_color_ranges, counter=counter, debug=False)
if circle_coords is not None:
warped_array = cv.warp(overlay, circle_coords)
path = 'doc/3_warped_array_'+str(counter)+'.png'
scipy.misc.imsave(path, warped_array)
warped_surface = pygame.transform.flip(pygame.image.load(path), True, False)
warped_surface.set_alpha(150)
else:
print('put the tiles back, man!')
counter += 1
def test_scale__alpha( self ):
""" see if set_alpha information is kept.
"""
s = pygame.Surface((32,32))
s.set_alpha(55)
self.assertEqual(s.get_alpha(),55)
s = pygame.Surface((32,32))
s.set_alpha(55)
s2 = pygame.transform.scale(s, (64,64))
s3 = s.copy()
self.assertEqual(s.get_alpha(),s3.get_alpha())
self.assertEqual(s.get_alpha(),s2.get_alpha())
def test_threshold__uneven_colors(self):
(w,h) = size = (16, 16)
original_surface = pygame.Surface(size, pygame.SRCALPHA, 32)
dest_surface = pygame.Surface(size, pygame.SRCALPHA, 32)
original_surface.fill(0)
threshold_color_template = [5, 5, 5, 5]
threshold_template = [6, 6, 6, 6]
################################################################
for pos in range(len('rgb')):
threshold_color = threshold_color_template[:]
threshold = threshold_template[:]
threshold_color[pos] = 45
threshold[pos] = 50
pixels_within_threshold = pygame.transform.threshold (
dest_surface, original_surface, threshold_color,
threshold,
0, # diff_color
0 # change_return
)
self.assertEqual(w*h, pixels_within_threshold)
################################################################
def test_average_surfaces(self):
"""
"""
SIZE = 32
s1 = pygame.Surface((SIZE, SIZE))
s2 = pygame.Surface((SIZE, SIZE))
s3 = pygame.Surface((SIZE, SIZE))
s1.fill((10,10,70))
s2.fill((10,20,70))
s3.fill((10,130,10))
surfaces = [s1, s2, s3]
surfaces = [s1, s2]
sr = pygame.transform.average_surfaces(surfaces)
self.assertEqual(sr.get_at((0,0)), (10,15,70,255))
self.assertRaises(TypeError, pygame.transform.average_surfaces, 1)
self.assertRaises(TypeError, pygame.transform.average_surfaces, [])
self.assertRaises(TypeError, pygame.transform.average_surfaces, [1])
self.assertRaises(TypeError, pygame.transform.average_surfaces, [s1, 1])
self.assertRaises(TypeError, pygame.transform.average_surfaces, [1, s1])
self.assertRaises(TypeError, pygame.transform.average_surfaces, [s1, s2, 1])
self.assertRaises(TypeError, pygame.transform.average_surfaces, (s for s in [s1, s2,s3] ))
def test_average_color(self):
"""
"""
a = [24, 32]
for i in a:
s = pygame.Surface((32,32), 0, i)
s.fill((0,100,200))
s.fill((10,50,100), (0,0,16,32))
self.assertEqual(pygame.transform.average_color(s),(5,75,150,0))
self.assertEqual(pygame.transform.average_color(s, (16,0,16,32)), (0,100,200,0))
def test_rotate__lossless_at_90_degrees(self):
w, h = 32, 32
s = pygame.Surface((w, h), pygame.SRCALPHA)
gradient = list(test_utils.gradient(w, h))
for pt, color in gradient: s.set_at(pt, color)
for rotation in (90, -90):
s = pygame.transform.rotate(s,rotation)
for pt, color in gradient:
self.assert_(s.get_at(pt) == color)
def test_get_smoothscale_backend(self):
filter_type = pygame.transform.get_smoothscale_backend()
self.failUnless(filter_type in ['GENERIC', 'MMX', 'SSE'])
# It would be nice to test if a non-generic type corresponds to an x86
# processor. But there is no simple test for this. platform.machine()
# returns process version specific information, like 'i686'.
def test_set_smoothscale_backend(self):
# All machines should allow 'GENERIC'.
original_type = pygame.transform.get_smoothscale_backend()
pygame.transform.set_smoothscale_backend('GENERIC')
filter_type = pygame.transform.get_smoothscale_backend()
self.failUnlessEqual(filter_type, 'GENERIC')
# All machines should allow returning to original value.
# Also check that keyword argument works.
pygame.transform.set_smoothscale_backend(type=original_type)
# Something invalid.
def change():
pygame.transform.set_smoothscale_backend('mmx')
self.failUnlessRaises(ValueError, change)
# Invalid argument keyword.
def change():
pygame.transform.set_smoothscale_backend(t='GENERIC')
self.failUnlessRaises(TypeError, change)
# Invalid argument type.
def change():
pygame.transform.set_smoothscale_backend(1)
self.failUnlessRaises(TypeError, change)
# Unsupported type, if possible.
if original_type != 'SSE':
def change():
pygame.transform.set_smoothscale_backend('SSE')
self.failUnlessRaises(ValueError, change)
# Should be back where we started.
filter_type = pygame.transform.get_smoothscale_backend()
self.failUnlessEqual(filter_type, original_type)