lokesh / color-thief

Grab the color palette from an image using just Javascript. Works in the browser and in Node.
https://lokeshdhakar.com/projects/color-thief/
MIT License
12.67k stars 1.31k forks source link

if a image just have a color, "getPalette" methods is throw error #242

Open AursorDev opened 1 year ago

AursorDev commented 1 year ago

Hello When I use it to get color,i upload this image,but it is throw error white image

and the code is:

app.post("/colors", upload.single("file"), async (req, res) => {
  const fileExt =
    req.file.originalname.split(".")[
      req.file.originalname.split(".").length - 1
    ];
  try {
    fs.renameSync(
      path.resolve(__dirname, req.file.path),
      `${req.file.path}.${fileExt}`
    );

    const color = await ColorThief.getPalette(
      path.resolve(__dirname, `${req.file.path}.${fileExt}`),
      2,
      500
    );

    res.status(200).send(color);
  } catch (e) {
    console.log("e:", e);
    res.status(500).send("图片处理失败!");
  }
});
john-doherty commented 1 year ago

Why was this closed without a solution/fix?

drzoidberg33 commented 7 months ago

I ran into this yesterday too.

It's because color-thief will fail on any image that only has white/near-white pixels: https://github.com/lokesh/color-thief/blob/4dc3bb0b43250f0d006278998aa91c4156f77cc3/src/core.js#L12-L17

AursorDev commented 7 months ago

wow,you responsed me! ok, i see this,but i don't know why can't support the image in pure color


发件人: Tim Van @.> 发送时间: 2024年4月17日 16:19 收件人: lokesh/color-thief @.> 抄送: 浮云 @.>; Author @.> 主题: Re: [lokesh/color-thief] if a image just have a color, "getPalette" methods is throw error (Issue #242)

I ran into this yesterday too.

It's because color-thief will fail on any image that only has white/near-white pixels: https://github.com/lokesh/color-thief/blob/4dc3bb0b43250f0d006278998aa91c4156f77cc3/src/core.js#L12-L17

― Reply to this email directly, view it on GitHubhttps://github.com/lokesh/color-thief/issues/242#issuecomment-2060677670, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AXGW4TCLLYGQUNREP7YRUILY5YV7LAVCNFSM6AAAAAAZLRXDD2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANRQGY3TONRXGA. You are receiving this because you authored the thread.Message ID: @.***>