Skip to content
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

PNG resize = large file sizes #1399

Closed
timothyallan opened this issue Oct 4, 2018 · 7 comments
Closed

PNG resize = large file sizes #1399

timothyallan opened this issue Oct 4, 2018 · 7 comments
Labels

Comments

@timothyallan
Copy link

timothyallan commented Oct 4, 2018

Hi!

I'm migrating from Intervention image library to Sharp and have run into a snag. My PNG resizes are substantially larger via Sharp, compared to Intervention, which is using Imagemagick under the hood.

I've tried all the PNG settings available in Sharp, but still can't get anywhere close. Hopefully I'm just missing something?

Here are two PNG images, with alpha channel intact, resized from 2k or so down to 1024x1024.

Sharp is about 590KB, and Imagemagick is about 360KB!

imagemagik
sharp

Intervention code I'm using
Image->orientate()->fit(1024, 1024)->encode('png')

and Sharp is:
sharp().resize(1024, 1024).ignoreAspectRatio().png({ adaptiveFiltering: true })

adaptiveFiltering off seems to increase the file size even more on certain files, so I've left it on.

Any suggestions on how to get a comparative file size using Sharp??

@lovell
Copy link
Owner

lovell commented Oct 4, 2018

Hello, please see https://sharp.pixelplumbing.com/en/stable/api-output/#png

"PNG output is always full colour at 8 or 16 bits per pixel. Indexed PNG input at 1, 2 or 4 bits per pixel is converted to 8 bits per pixel."

@lovell lovell added the question label Oct 4, 2018
@timothyallan
Copy link
Author

Thanks, and yup, read that area, but I wasn't aware what the various PNG input types are. So that's the cause then. Hopefully this clears it up for anyone else who isn't so versed on what indexed PNG input is as well!

@lovell
Copy link
Owner

lovell commented Oct 4, 2018

See also #478 for the canonical version of this question.

@sin1rou
Copy link

sin1rou commented Nov 29, 2018

Hello, libvips seems to support PNG8 format now.
8-Bit PNG Quantization Support by felixbuenemann · Pull Request #1009 · libvips/libvips

Can I use this option on sharp?

@lovell
Copy link
Owner

lovell commented Nov 30, 2018

@sin1rou Please see #1484

@victorkurauchi
Copy link

Hi,
I started using this plugin (great work btw) this week and am trying to run a few resizing and optimisations with PNG and JPG files.
So far, no issues with JPG.

Today i'm running the resize for my PNG file and i noticed the final result contain a file that has a bigger size than the original.

Do you happen to know what would be the cause for this behaviour ?

Btw, I had a look in the links provided here :)

Thanks a lot!

my code:

const sharp = require('sharp')
const fs = require('fs')

const img = 'hero';
const inputPath = `./src/public/images/desktop/${img}.png`
const outputPath = `./src/public/images/mobile/${img}.png`

const start = Date.now()
let sharpObj = sharp(inputPath).resize(1024, 500, {
  fit: sharp.fit.inside,
  withoutEnlargement: true
})

// sharpObj = sharpObj.png({ adaptiveFiltering: true, pallete: true, quality: 70, dither: 1.0  })

sharpObj
  .toFile(outputPath)
  .then(() => {
    const end = Date.now()
    console.log((end - start) / 1000)

    fs.stat(outputPath, (err, stat) => {
      console.log(err, stat)
    })
  })
  .catch(err => {
    console.error(err)
  })

Result in /mobile folder:
Screen Shot 2020-08-07 at 14 27 13

My original image in desktop folder:
Screen Shot 2020-08-07 at 14 27 03

original image:
hero

@lovell
Copy link
Owner

lovell commented Aug 7, 2020

@victorkurauchi Please open a new issue, ensuring you've read https://sharp.pixelplumbing.com/api-output#png especially

Some of these options require the use of a globally-installed libvips compiled with support for libimagequant (GPL).

Repository owner locked and limited conversation to collaborators Aug 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants