numpy "bug" - reproduced when running examples/basic/ptext.py (if versions the same I suppose)
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 611
- Forks
- 211
- PR merge metrics
- No merged PRs in 30d
Description
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'
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Run examples/basic/ptext.py with the reported Python, NumPy, pgzero, and pygame versions, then inspect the getsurf operations in pgzero/ptext.py around the failing lines 229-258. Confirm the example no longer raises the NumPy casting error and that the affected text rendering still works.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, python
- Domain
- game-dev
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100