Skip to content

Implement pitch argument for image.tobytes() #2602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion buildconfig/stubs/pygame/image.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def tostring(
surface: Surface,
format: _to_string_format,
flipped: bool = False,
pitch: int = -1,
) -> bytes: ...
def fromstring(
bytes: bytes,
Expand All @@ -31,7 +32,10 @@ def fromstring(

# the use of tobytes/frombytes is preferred over tostring/fromstring
def tobytes(
surface: Surface, format: _to_string_format, flipped: bool = False
surface: Surface,
format: _to_string_format,
flipped: bool = False,
pitch: int = -1,
) -> bytes: ...
def frombytes(
bytes: bytes,
Expand Down
11 changes: 9 additions & 2 deletions docs/reST/ref/image.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ following formats.
.. function:: tostring

| :sl:`transfer image to byte buffer`
| :sg:`tostring(Surface, format, flipped=False) -> bytes`
| :sg:`tostring(Surface, format, flipped=False, pitch=-1) -> bytes`

DEPRECATED: This function has the same functionality as :func:`tobytes()`, which is preferred and should be used.

Expand All @@ -180,7 +180,7 @@ following formats.
.. function:: tobytes

| :sl:`transfer image to byte buffer`
| :sg:`tobytes(Surface, format, flipped=False) -> bytes`
| :sg:`tobytes(Surface, format, flipped=False, pitch=-1) -> bytes`

Creates a string of bytes that can be transferred with the ``fromstring``
or ``frombytes`` methods in other Python imaging packages. Some Python
Expand Down Expand Up @@ -209,12 +209,19 @@ following formats.

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

The 'pitch' argument can be used to specify the pitch/stride per horizontal line
of the image in bytes. It must be equal to or greater than how many bytes
the pixel data of each horizontal line in the image bytes occupies without any
extra padding. By default, it is ``-1``, which means that the pitch/stride is
the same size as how many bytes the pure pixel data of each horizontal line takes.

.. note:: The use of this function is recommended over :func:`tostring` as of pygame 2.1.3.
This function was introduced so it matches nicely with other
libraries (PIL, numpy, etc), and with people's expectations.

.. versionadded:: 2.1.3
.. versionchanged:: 2.2.0 Now supports keyword arguments.
.. versionchanged:: 2.4.0 Added a 'pitch' argument.

.. ## pygame.image.tobytes ##

Expand Down
4 changes: 2 additions & 2 deletions src_c/doc/image_doc.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#define DOC_IMAGE_SAVE "save(Surface, file) -> None\nsave(Surface, file, namehint="") -> None\nsave an image to file (or file-like object)"
#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"
#define DOC_IMAGE_GETEXTENDED "get_extended() -> bool\ntest if extended image formats can be loaded"
#define DOC_IMAGE_TOSTRING "tostring(Surface, format, flipped=False) -> bytes\ntransfer image to byte buffer"
#define DOC_IMAGE_TOBYTES "tobytes(Surface, format, flipped=False) -> bytes\ntransfer image to byte buffer"
#define DOC_IMAGE_TOSTRING "tostring(Surface, format, flipped=False, pitch=-1) -> bytes\ntransfer image to byte buffer"
#define DOC_IMAGE_TOBYTES "tobytes(Surface, format, flipped=False, pitch=-1) -> bytes\ntransfer image to byte buffer"
#define DOC_IMAGE_FROMSTRING "fromstring(bytes, size, format, flipped=False, pitch=-1) -> Surface\ncreate new Surface from a byte buffer"
#define DOC_IMAGE_FROMBYTES "frombytes(bytes, size, format, flipped=False, pitch=-1) -> Surface\ncreate new Surface from a byte buffer"
#define DOC_IMAGE_FROMBUFFER "frombuffer(buffer, size, format, pitch=-1) -> Surface\ncreate a new Surface that shares data inside a bytes buffer"
Expand Down
Loading