Skip to content

Commit f3b53b6

Browse files
authored
Merge pull request #939 from coadkins/coadkins-patch-1
Add PNG File subsection to Pico Graphics documentation
2 parents 616b1cc + 37c4d22 commit f3b53b6

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

micropython/modules/picographics/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,3 +584,37 @@ The arguments for `decode` are as follows:
584584
2. Decode Y
585585
3. Flags - one of `JPEG_SCALE_FULL`, `JPEG_SCALE_HALF`, `JPEG_SCALE_QUARTER` or `JPEG_SCALE_EIGHTH`
586586
4. If you want to turn off dither altogether, try `dither=False`. This is useful if you want to [pre-dither your images](https://ditherit.com/) or for artsy posterization effects.
587+
588+
### PNG Files
589+
590+
We've also included Bitbank's PNGdec - https://github.com/bitbank2/PNGdec - for PNG file support with Pico Graphics.
591+
592+
Like JPEG decoding, PNG decoding supports loading files from microSD, flash and RAM, but unlike JPEG decoding there are some new options for cropping, scaling and rotating you PNG images. (Note: the order is always crop, scale and rotate.)
593+
594+
A basic example looks something like this:
595+
596+
```python
597+
from pngdec import PNG
598+
png = PNG(display)
599+
png.open_file("fire.png")
600+
png.decode(0, 0)
601+
```
602+
The arguments for `decode` are as follows:
603+
1. Decode X - where to place the decoded PNG on screen
604+
2. Decode Y
605+
3. Source - The region, in pixels, that you want to show from the PNG. The argument is given as a tuple of four values which give the offset from the left and top of the images, plus the width and height of the selected region. The whole PNG is loaded and decoded no matter what you put here, but this it makes it easier to manage multiple images for things like icons.
606+
4. Scale - Lets you scale images up by a fixed multiplier along the X and Y axis. If you want to make an image 4x wider and 2x taller you'd use `scale=(4,2)'.
607+
5. Rotate - Lets you rotate your PNG graphic in 90 degree intervals.
608+
6. Mode - For indexed PNGs, you can supply a mode argument with one of `PNG COPY`, `PNG DITHER`, and `PNG_POSTERISE`. `PNG_COPY` will copy the palette indexes into a P4 or P8 graphics buffer rather than dithering or posterising (snapping to the nearest available colour).
609+
`PNG_DITHER` will use a simple ordered dither matrix to dither the image colours to the available display colours.
610+
`PNG_POSTERISE` will snap the colours in the PNG to their nearest display counterpart. Posterise is the default in all cases.
611+
612+
Lets say you have a spritesheet with 8x8 sprites and you want to display a 3x2 character from it at 4x scale, you might do something like this:
613+
614+
```python
615+
from pngdec import PNG
616+
png = PNG(display)
617+
png.open_file("/s4m_ur4i-pirate-characters.png")
618+
619+
png.decode(0, 0, source=(32, 48, 24, 16), scale=(4, 4), rotate=0)
620+
```

0 commit comments

Comments
 (0)