Comments (8)
I think it's not related to compression.
When opening the 3 images in https://www.nayuki.io/page/png-file-chunk-inspector, it shows that gimp only includes IHDR
, IDAT
and IEND
chunks.
while nimPNG also includes PLTE
and tRNS
from nimpng.
I assume your pngs contain transparency and no animation frames, therefore I use loadPNG32/savePNG32
pair.
what we do here basically only preserve the pixel data(+ transparency) and discard everything else.
if you also want to discard transparency, you can use loadPNG24/savePNG24
version.
And then nimPNG will try to find the best compression for your new png.
let lres = loadPNG32(seq[uint8], "input.png")
if lres.isOk:
let png = lres.get()
let sres = savePNG32("output.png", png.data, png.width, png.height)
if sres.isErr:
echo sres.error
else:
echo lres.error
from nimpng.
Hi, Thanks for your help !
That still produces larger output than gimp
Original: 157B
GIMP: 103B
nimPNG: 134B
from nimpng.
then try this. it will take longer time, but produce smaller output.
if it still produce larger size than gimp, then you perhaps should find other png library.
currently nimPNG
doesn't expose it's compression library settings to user, and it requires some work to enable that. and unfortunately I'm still too busy to make such modification.
var conf = makePNGEncoder()
conf.filterStrategy = LFS_BRUTE_FORCE
let sres = savePNG32("output.png", png.data, png.width, png.height, conf)
from nimpng.
I see, then you can try to turn off autoConvert
. If autoConvert
enabled, it will count how many colors in the image and create palette if color number <= 256. This strategy works for bigger image to reduce file size. But looks like your image is small enough and this feature actually add more bytes.
var conf = makePNGEncoder()
conf.autoConvert = false
from nimpng.
alternative, read important chunk only, and then write back without reencoding.
import ../nimPNG, streams
proc main() =
var conf = makePNGDecoder()
conf.colorConvert = false
conf.readTextChunks = false
conf.rememberUnknownChunks = false
var s = newFileStream("input.png", fmRead)
var png = decodePNG(seq[uint8], s, conf)
s.close()
s = newFileStream("output.png", fmWrite)
png.writeChunks s
s.close()
main()
from nimpng.
The first code produces 111B (better), gimp produces 103B.
The second one produces a corrupt file.
from nimpng.
sorry, out of idea.
from nimpng.
Related Issues (20)
- Throw exceptions on errors instead of writing to stdout HOT 12
- Tests fail when using --gc:arc HOT 3
- This hash look up in calculateColorProfile is the slowest part of saving an image. HOT 2
- compiletime decoder HOT 2
- New API βΒ can't savePNG32 with a seq[uint8] HOT 2
- Error: unhandled exception: cannot read from stream [IOError] HOT 4
- Improve chunk parser flexibility when decoding incomplete input data.
- savePNG doesn't work for arrays
- Allow parsing into different data types HOT 1
- nim 1.6.0 savePNG results in too nested template instantiation HOT 4
- Enable user to configure compression library settings
- Need better documentation for encoder settings
- Warning: conversion to enum with holes is unsafe: PNGColorType(typ`gensym14) [HoleEnumConv]
- Doesn't compile on Nim devel due to `shallowCopy` being removed from the language.
- `decodePNG` colorType unexpected HOT 3
- savePNGImpl maybe have bug? HOT 3
- Doesn't work by default on Nim v2.0.0 HOT 2
- nimPNG relies on bugs of `newString` to work
- Move tests to another repo HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nimpng.