Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

pyxpo-image-cropper

CI PyPI Python License: MIT

A full-featured image cropper for pyxpo apps written in Python: free or fixed aspect ratio, a circle guide for avatars, rotate, flip and pinch zoom. Use it two ways: open a ready full-screen cropper with one call, or put a CropView in your own screen and drive it with your own buttons. iOS uses TOCropViewController, Android the CanHub Android-Image-Cropper. The cropped photo is a new file; the source is never changed.

The inline CropView with a 1:1 box and ratio chips on Android and iOS

from pyxpo_image_cropper import ImageCropper

res = await ImageCropper.open_cropper_async(path, aspectRatio=(1, 1), circle=True)
if not res["canceled"]:
    avatar = res["uri"]

Features

  • Full-screen cropper: open_cropper_async(uri, ...) shows the native cropper and returns the new file.
  • Inline editor: CropView sits in your layout. ref.current gives rotate(), flip(), reset() and crop_async().
  • Aspect ratio: free, or any (width, height) pair such as (16, 9). Lock it, or use it only as the start frame.
  • Circle guide for profile photos (the saved file stays square; round it with borderRadius).
  • Rotate in quarter turns, flip left to right or top to bottom, pinch zoom, thirds grid.
  • Output: JPEG with a quality from 0 to 1, or PNG. Width, height, size and MIME type come back with the path.
  • Typed: ships py.typed. camelCase (openCropperAsync) and snake_case names both work.

Contents

If you are... Start here
Cropping a picked photo in one call Quick start
Building your own crop screen CropView
Making a round avatar Recipe: avatar
Exact signatures API reference · Errors
Stuck Testing · Troubleshooting · FAQ

Platform support

Target What works
Android, API 24+ Everything.
iOS 15+ Everything, except flip in the full-screen cropper (TOCropViewController has no flip). CropView.flip() works.
Web page and desktop window Not supported. Calls raise NotSupportedError.

Requires Python 3.9+ and pyxpo>=0.1.0a4. It needs no permissions and no Info.plist keys. To pick the photo first, use pyxpo-image-picker.

Install

pip install pyxpo-image-cropper
[tool.pyxpo]
modules = ["image-cropper"]

Rebuild the app. The build adds the native croppers (Swift package on iOS, Gradle dependency on Android).

Quick start

from pyxpo import Button, Image, View, component, use_async, use_state
from pyxpo_image_cropper import ImageCropper


@component
def CropButton(path: str = ""):
    taps, set_taps = use_state(0)
    out, set_out = use_state(None)

    async def run():
        if taps == 0 or not path:
            return
        res = await ImageCropper.open_cropper_async(path, aspectRatio=(4, 5), lockAspectRatio=False, title="Crop photo")
        if not res["canceled"]:
            set_out(res)

    use_async(run, [taps])

    return View(
        Image(source=out["uri"], style={"height": 240, "aspectRatio": out["width"] / out["height"]}) if out else None,
        Button("Crop", lambda: set_taps(taps + 1)),
    )

Tip: Start async work from a button with a counter in state and use_async(fn, [counter]). A lambda: coroutine() is never awaited.

open_cropper_async returns {"canceled", "uri", "width", "height", "fileSize", "mimeType"}. When the user cancels, canceled is True and uri is None. Only one full-screen cropper can be open at a time.

CropView

Put the editor in your own screen when you want your own buttons, ratio chips or colors.

from pyxpo import Button, View, component, use_ref, use_state
from pyxpo.apis import _bridge
from pyxpo_image_cropper import CropView

RATIOS = [None, (1, 1), (4, 5), (16, 9)]


@component
def Editor(path: str, on_done=None):
    ratio, set_ratio = use_state(None)
    ref = use_ref(None)

    def crop():
        async def go():
            res = await ref.current.crop_async(format="jpeg", quality=0.85)
            if on_done:
                on_done(res)
        _bridge.spawn(go())

    return View(
        CropView(source=path, aspect_ratio=ratio, ref=ref, style={"flex": 1}),
        View(*[Button(str(r or "Free"), lambda r=r: set_ratio(r)) for r in RATIOS], style={"flexDirection": "row"}),
        View(
            Button("Rotate", lambda: ref.current.rotate()),
            Button("Flip", lambda: ref.current.flip()),
            Button("Reset", lambda: ref.current.reset()),
            style={"flexDirection": "row"},
        ),
        Button("Crop", crop),
        style={"flex": 1},
    )

The circle guide in the CropView, then the cropped photo with its size, on Android and iOS

Prop Default Description
source required A local path or file://; on Android also content://. Pass it by name: a positional argument becomes a child.
aspect_ratio None A (width, height) pair that locks the box, or None for a free box. Changing it reshapes the box.
circle False A round guide; locks 1:1.
grid True The thirds grid.
on_change None on_change({"resettable": bool}) when the crop moves away from, or back to, the whole photo.
on_error None on_error({"message"}) when the photo cannot be opened.
style None Size the view here, for example {"flex": 1}.
ref None ref.current is a CropViewHandle.

Recipe: avatar

Pick a photo, crop it round, show it.

from pyxpo import Button, Image, View, component, use_async, use_state
from pyxpo_image_cropper import ImageCropper
from pyxpo_image_picker import ImagePicker


@component
def Avatar():
    taps, set_taps = use_state(0)
    uri, set_uri = use_state(None)

    async def run():
        if taps == 0:
            return
        picked = await ImagePicker.launch_image_library_async()
        if picked["canceled"]:
            return
        res = await ImageCropper.open_cropper_async(picked["assets"][0]["uri"], circle=True, quality=0.8)
        if not res["canceled"]:
            set_uri(res["uri"])

    use_async(run, [taps])
    return View(
        Image(source=uri, style={"width": 120, "height": 120, "borderRadius": 60}) if uri else None,
        Button("Choose photo", lambda: set_taps(taps + 1)),
    )

To make the file smaller before upload, pass the result to pyxpo-image-manipulator.

Errors

Error When What to do
ValueError Empty uri or source, a bad aspectRatio, a format other than "jpeg" or "png", quality outside 0 to 1. Fix the call.
NotSupportedError Called on the web page or desktop window. Crop on iOS or Android only.
ModuleNotLinkedError (from pyxpo.module_api) Built without "image-cropper" in modules. Add it and rebuild.
RuntimeError: the CropView is not mounted ref.current used before the view is on screen. Call it from a button.
A native error (file missing, not an image) Bad path. Check the path.

Production checklist

  • You handle canceled (the user can always back out).
  • You size the CropView with flex or a height. It has no size of its own.
  • You delete cropped files when finished (they are in the app cache).
  • For avatars you round the image with borderRadius (the file is square).
  • You tested a large photo (12 MP and up) on a real phone.

Testing

The plain Python parts need no device:

import asyncio

import pyxpo_image_cropper as mod


def test_ratio_is_reduced():
    assert mod._ratio((1.5, 1)) == (3, 2)
    assert mod._ratio(None) == (0, 0)


def test_cropper_with_fake(monkeypatch):
    async def fake_call(native, method, uri, opts, reply=True):
        assert opts["ratioX"] == 1 and opts["circle"]
        return {"canceled": False, "uri": "/c.jpg", "width": 500, "height": 500, "fileSize": 9, "mimeType": "image/jpeg"}

    monkeypatch.setattr(mod, "platform", lambda: "android")
    monkeypatch.setattr(mod, "require_linked", lambda *a, **k: None)
    monkeypatch.setattr(mod, "call", fake_call)
    out = asyncio.run(mod.ImageCropper.open_cropper_async("/a.jpg", circle=True))
    assert out["width"] == 500 and not out["canceled"]

Troubleshooting

Symptom Cause and fix
NotSupportedError: the image cropper needs iOS or Android You are in the web or desktop target.
ModuleNotLinkedError Add "image-cropper" to modules and rebuild.
The CropView is empty Give it a size ({"flex": 1}), and check on_error.
No flip button in the iOS full-screen cropper TOCropViewController has no flip. Use CropView if you need it.
Android build fails on minSdk The cropper needs API 24 or higher.

FAQ

Is the original changed? No. A new file is written to the app cache.

Is the circle crop a round PNG? No, the file is square. Round it on screen with borderRadius.

Can I crop a remote URL? Download it first, for example with pyxpo-file-system, then pass the local path.

API reference

ImageCropper

Method (camelCase name) Returns
open_cropper_async(uri, aspectRatio=None, lockAspectRatio=True, circle=False, rotate=True, flip=True, format="jpeg", quality=0.9, title="") (openCropperAsync) {canceled, uri, width, height, fileSize, mimeType}

CropView

CropView(source=..., aspect_ratio=None, circle=False, grid=True, on_change=None, on_error=None, style=None, ref=None). See CropView.

CropViewHandle

Method Description
rotate(clockwise=True) A quarter turn.
flip(horizontal=True) Mirror left to right, or top to bottom with horizontal=False.
reset() Back to the whole photo: no rotation, flip or zoom.
crop_async(format="jpeg", quality=0.9) (cropAsync) Saves the box as a new file. Same dict as open_cropper_async, canceled always False.

License

MIT

Metadata

Release files for pyxpo-image-cropper 0.1.0a1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyxpo-image-cropper 0.1.0a1
File Size Uploaded Provenance Actions
pyxpo_image_cropper-0.1.0a1.tar.gz 22.9 kB With provenance Details for pyxpo_image_cropper-0.1.0a1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pyxpo-image-cropper 0.1.0a1
File Interpreter ABI Platform Actions
pyxpo_image_cropper-0.1.0a1-py3-none-any.whl Python 3 none any Details for pyxpo_image_cropper-0.1.0a1-py3-none-any.whl

Total release size: 42.7 kB

Release files / pyxpo_image_cropper-0.1.0a1.tar.gz

Download URL pyxpo_image_cropper-0.1.0a1.tar.gz
Size 22.9 kB
Tags Source
SHA-256 checksum
How to use checksums
afc35dae7e508aa79bc012db66d10ff63b5b566706f5dd1835bcfd11fe707a34
BLAKE2b-256 checksum
How to use checksums
95dbae6321fe736773ef131e63646922976d65c7d35b8bb129f4675428dfbb9e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 29, 2026.

Transparency log

Release files / pyxpo_image_cropper-0.1.0a1-py3-none-any.whl

Download URL pyxpo_image_cropper-0.1.0a1-py3-none-any.whl
Size 19.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
70a355c0fec44f2be97f31dc70266c96047af16c8c0dfd40b86c5619ff215f94
BLAKE2b-256 checksum
How to use checksums
51828b6176bd498ffc84e26960e41b8ee94218511fdd029e0ebb64dceabd2cd9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 29, 2026.

Transparency log

Security for pyxpo-image-cropper 0.1.0a1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pyxpo_image_cropper-0.1.0a1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 29, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page