pgzero icon indicating copy to clipboard operation
pgzero copied to clipboard

numpy "bug" - reproduced when running examples/basic/ptext.py (if versions the same I suppose)

Open lordmauve opened this issue 8 years ago • 2 comments

Originally reported by: Steve Clement (Bitbucket: SteveClement, GitHub: SteveClement)


Dear all,

When you run ptext.py example you get this error:

TypeError: Cannot cast ufunc multiply output from dtype('float64') to dtype('uint8') with casting rule 'same_kind'

Not sure how safe my fix is, but it works. Should I do a pull request? Also not sure there are other locations where you need to do unsafe casts…

It can be fixed as follows

#!patch

--- /usr/local/lib/python3.5/site-packages/pgzero/ptext.py.orig	2016-12-02 16:05:29.000000000 +0100
+++ /usr/local/lib/python3.5/site-packages/pgzero/ptext.py	2016-12-02 16:04:50.000000000 +0100
@@ -201,7 +201,8 @@
 			lineheight=lineheight, cache=cache)
 		surf = surf0.copy()
 		array = pygame.surfarray.pixels_alpha(surf)
-		array *= alpha
+		import numpy
+		numpy.multiply(array, alpha, out=array, casting="unsafe")
 	elif spx is not None:
 		surf0 = getsurf(text, fontname, fontsize, width, widthem, color=color,
 			background=(0,0,0,0), antialias=antialias, gcolor=gcolor, align=align,
@@ -255,8 +256,8 @@
 			for lsurf in lsurfs:
 				array = pygame.surfarray.pixels3d(lsurf)
 				for j in (0, 1, 2):
-					array[:,:,j] *= 1.0 - m
-					array[:,:,j] += m * gcolor[j]
+					numpy.multiply(array[:,:,j], 1.0 - m, out=array[:,:,j], casting="unsafe")
+					numpy.multiply(array[:,:,j], m * gcolor[j], out=array[:,:,j], casting="unsafe")
 				del array

 		if len(lsurfs) == 1 and gcolor is None:

My versions

  • numpy==1.11.2
  • pgzero==1.1
  • pygame==1.9.2b8
  • Python 3.5.2
  • Darwin Steves-iMac.local 16.1.0 Darwin Kernel Version 16.1.0: Thu Oct 13 21:26:57 PDT 2016; root:xnu-3789.21.3~60/RELEASE_X86_64 x86_64 (MacOS Sierra)

Run output

#!bash

~/Desktop/code/pgzeroBB/examples/basic  ☿ 179@default  pgzrun ptext.py
Traceback (most recent call last):
  File "/usr/local/bin/pgzrun", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.5/site-packages/pgzero/runner.py", line 89, in main
    PGZeroGame(mod).run()
  File "/usr/local/lib/python3.5/site-packages/pgzero/game.py", line 219, in run
    draw()
  File "ptext.py", line 115, in draw
    gcolor="#442200"
  File "/usr/local/lib/python3.5/site-packages/pgzero/screen.py", line 61, in text
    ptext.draw(*args, surf=self._surf, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/pgzero/ptext.py", line 320, in draw
    ocolor, owidth, scolor, shadow, gcolor, alpha, align, lineheight, angle, cache)
  File "/usr/local/lib/python3.5/site-packages/pgzero/ptext.py", line 229, in getsurf
    lineheight=lineheight, cache=cache)
  File "/usr/local/lib/python3.5/site-packages/pgzero/ptext.py", line 258, in getsurf
    array[:,:,j] *= 1.0 - m
TypeError: Cannot cast ufunc multiply output from dtype('float64') to dtype('uint8') with casting rule 'same_kind'

  • Bitbucket: https://bitbucket.org/lordmauve/pgzero/issue/48

lordmauve avatar Dec 02 '16 15:12 lordmauve

I am reviewing bug

MarkAColes avatar Jul 28 '18 15:07 MarkAColes

As per comments within issue #53 unable to replicate the bug.

I have also tried to revert versions of numpy to 1.11.2 and pygame to 1.9.2b8 but was unable to do so as they are no longer available.

MarkAColes avatar Jul 28 '18 15:07 MarkAColes