Skip to content

fix(extractImages): calculate channels correctly and return image metadata #22

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

Merged
merged 3 commits into from
Apr 14, 2025

Conversation

Yiin
Copy link
Contributor

@Yiin Yiin commented Apr 12, 2025

Calculate image channels based on data length, width, and height.

extractImages now returns an array of objects containing data, width, height, channels, and key instead of just the raw data array. This fixes issues with distorted images caused by incorrect channel assumptions and provides necessary metadata for downstream processing.

resolves #21

Example of extracting images to png format:

const imagesData = await extractImages(pdf, pageNum);

for (const imgData of imagesData) {
  const imageIndex = totalImagesProcessed + 1;

  try {
    await sharp(imgData.data, {
      raw: { width: imgData.width, height: imgData.height, channels: imgData.channels }
    })
    .png()
    .toFile(`${imageIndex}.png`);
  } catch (e) {
     // handle error
     continue;
  }
}

It is a breaking change because return value was changed, but given that this function was unusable before I have doubts it was in use anywhere

Yiin and others added 2 commits April 12, 2025 04:35
…adata

Calculate image channels based on data length, width, and height.

`extractImages` now returns an array of objects containing `data`, `width`, `height`, `channels`, and `key` instead of just the raw data array. This fixes issues with distorted images caused by incorrect channel assumptions and provides necessary metadata for downstream processing.
@johannschopplich johannschopplich changed the title fix(extractImages): Calculate channels correctly and return image metadata fix(extractImages): calculate channels correctly and return image metadata Apr 14, 2025
@johannschopplich
Copy link
Collaborator

Thanks a lot!

@johannschopplich johannschopplich merged commit d69325f into unjs:main Apr 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

extractImages does not work for non-.jpg images embedded in the PDF
2 participants