Skip to content

Commit b095a60

Browse files
authored
Merge pull request #2602 from whydoubt/image-tobytes-pitch
Implement pitch argument for image.tobytes()
2 parents c70ce37 + 57c7ede commit b095a60

File tree

5 files changed

+436
-958
lines changed

5 files changed

+436
-958
lines changed

buildconfig/stubs/pygame/image.pyi

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def tostring(
2121
surface: Surface,
2222
format: _to_string_format,
2323
flipped: bool = False,
24+
pitch: int = -1,
2425
) -> bytes: ...
2526
def fromstring(
2627
bytes: bytes,
@@ -32,7 +33,10 @@ def fromstring(
3233

3334
# the use of tobytes/frombytes is preferred over tostring/fromstring
3435
def tobytes(
35-
surface: Surface, format: _to_string_format, flipped: bool = False
36+
surface: Surface,
37+
format: _to_string_format,
38+
flipped: bool = False,
39+
pitch: int = -1,
3640
) -> bytes: ...
3741
def frombytes(
3842
bytes: bytes,

docs/reST/ref/image.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ following formats.
195195
.. function:: tostring
196196

197197
| :sl:`transfer image to byte buffer`
198-
| :sg:`tostring(Surface, format, flipped=False) -> bytes`
198+
| :sg:`tostring(Surface, format, flipped=False, pitch=-1) -> bytes`
199199
200200
DEPRECATED: This function has the same functionality as :func:`tobytes()`, which is preferred and should be used.
201201

@@ -206,7 +206,7 @@ following formats.
206206
.. function:: tobytes
207207

208208
| :sl:`transfer image to byte buffer`
209-
| :sg:`tobytes(Surface, format, flipped=False) -> bytes`
209+
| :sg:`tobytes(Surface, format, flipped=False, pitch=-1) -> bytes`
210210
211211
Creates a string of bytes that can be transferred with the ``fromstring``
212212
or ``frombytes`` methods in other Python imaging packages. Some Python
@@ -235,12 +235,19 @@ following formats.
235235

236236
* ``ARGB_PREMULT``, 32-bit image with colors scaled by alpha channel, alpha channel first
237237

238+
The 'pitch' argument can be used to specify the pitch/stride per horizontal line
239+
of the image in bytes. It must be equal to or greater than how many bytes
240+
the pixel data of each horizontal line in the image bytes occupies without any
241+
extra padding. By default, it is ``-1``, which means that the pitch/stride is
242+
the same size as how many bytes the pure pixel data of each horizontal line takes.
243+
238244
.. note:: The use of this function is recommended over :func:`tostring` as of pygame 2.1.3.
239245
This function was introduced so it matches nicely with other
240246
libraries (PIL, numpy, etc), and with people's expectations.
241247

242248
.. versionadded:: 2.1.3
243249
.. versionchanged:: 2.2.0 Now supports keyword arguments.
250+
.. versionchanged:: 2.5.0 Added a 'pitch' argument.
244251

245252
.. ## pygame.image.tobytes ##
246253

src_c/doc/image_doc.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#define DOC_IMAGE_SAVE "save(Surface, file) -> None\nsave(Surface, file, namehint="") -> None\nsave an image to file (or file-like object)"
66
#define DOC_IMAGE_GETSDLIMAGEVERSION "get_sdl_image_version(linked=True) -> None\nget_sdl_image_version(linked=True) -> (major, minor, patch)\nget version number of the SDL_Image library being used"
77
#define DOC_IMAGE_GETEXTENDED "get_extended() -> bool\ntest if extended image formats can be loaded"
8-
#define DOC_IMAGE_TOSTRING "tostring(Surface, format, flipped=False) -> bytes\ntransfer image to byte buffer"
9-
#define DOC_IMAGE_TOBYTES "tobytes(Surface, format, flipped=False) -> bytes\ntransfer image to byte buffer"
8+
#define DOC_IMAGE_TOSTRING "tostring(Surface, format, flipped=False, pitch=-1) -> bytes\ntransfer image to byte buffer"
9+
#define DOC_IMAGE_TOBYTES "tobytes(Surface, format, flipped=False, pitch=-1) -> bytes\ntransfer image to byte buffer"
1010
#define DOC_IMAGE_FROMSTRING "fromstring(bytes, size, format, flipped=False, pitch=-1) -> Surface\ncreate new Surface from a byte buffer"
1111
#define DOC_IMAGE_FROMBYTES "frombytes(bytes, size, format, flipped=False, pitch=-1) -> Surface\ncreate new Surface from a byte buffer"
1212
#define DOC_IMAGE_FROMBUFFER "frombuffer(buffer, size, format, pitch=-1) -> Surface\ncreate a new Surface that shares data inside a bytes buffer"

0 commit comments

Comments
 (0)