{"id":2228,"library":"pypng","title":"PyPNG","description":"PyPNG is a pure Python library designed for reading, writing, and manipulating PNG (Portable Network Graphics) images. It provides a simple, Pythonic interface and boasts no external dependencies. The current stable version is `0.20220715.0`, with updates released as needed.","status":"active","version":"0.20220715.0","language":"en","source_language":"en","source_url":"https://gitlab.com/drj11/pypng","tags":["image processing","png","pure python","graphics"],"install":[{"cmd":"pip install pypng","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"note":"The PyPI package name is `pypng`, but the module is imported as `png`.","wrong":"import pypng","symbol":"png","correct":"import png"}],"quickstart":{"code":"import png\nimport os\n\n# Create a simple 2x2 grayscale image (Luminance) with a bit depth of 8\nrows = [\n    [255, 0], # White, Black\n    [0, 255]  # Black, White\n]\n\n# Create an image from a 2D array and save it\nimage_file_path = 'simple_image.png'\npng.from_array(rows, 'L').save(image_file_path)\n\nprint(f\"Image saved to {image_file_path}\")\n\n# Clean up the created file\n# os.remove(image_file_path)","lang":"python","description":"This quickstart demonstrates how to create a simple grayscale PNG image from a Python list of lists representing pixel data. It uses the `png.from_array` function for convenience and saves the image to a file."},"warnings":[{"fix":"Remove the `lenient` flag from your `Reader` initialization. Handle checksum warnings via Python's `warnings` module if needed.","message":"The `lenient` flag has been removed. PNG chunk checksum errors are now treated as warnings instead of exceptions and can be managed using Python's standard `warnings` module.","severity":"breaking","affected_versions":"0.20231004.0 and later"},{"fix":"Remove the `interlace` argument from `png.Writer` calls. If interlaced output is strictly required, an older version of `pypng` or an alternative library must be used.","message":"The `interlace` keyword argument has been removed from the `Writer` class, and interlaced output is no longer supported.","severity":"breaking","affected_versions":"0.20250521.0 and later"},{"fix":"Update exception handling to catch `EOFError` for empty file scenarios, while still catching `png.FormatError` for other invalid PNG format issues.","message":"Passing an empty file to PyPNG now raises a built-in `EOFError` instead of `png.FormatError`. This change helps distinguish empty file issues from genuine format errors.","severity":"breaking","affected_versions":"0.20220715.0 and later"},{"fix":"Ensure file opening statements explicitly include the binary mode flag (e.g., `with open('image.png', 'wb') as f:`).","message":"Files for reading or writing PNGs must always be opened in binary mode (e.g., `'wb'` for writing, `'rb'` for reading) to prevent data corruption or unexpected errors.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Carefully align the pixel data structure and values with the `bitdepth`, `greyscale`, and `alpha` parameters when initializing `png.Writer` or using `png.from_array`'s mode argument. For example, 'L;16' for 16-bit grayscale.","message":"Incorrect bit depth specification or pixel data formatting can lead to corrupted images. Pixel data must match the `Writer` configuration (e.g., grayscale, RGB, RGBA) and the declared bit depth.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Replace `/` with `//` for calculations that are expected to result in an integer, such as `width = len(row) // 3` for an RGB image.","message":"When calculating image dimensions, especially for `width` from row data (e.g., `len(row) / channels_per_pixel`), ensure you use integer division (`//`) instead of float division (`/`). Using float division where an integer is expected can lead to `TypeError`.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}