How to use jimp - 10 common examples

To help you get started, we’ve selected a few jimp examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github NiGhTTraX / mugshot / tests / node / suite.ts View on Github external
// eslint-disable-next-line no-restricted-syntax
  for (const row of rows) {
    let x = 0;

    // eslint-disable-next-line no-restricted-syntax
    for (const pixel of row) {
      // eslint-disable-next-line no-await-in-loop
      await j.setPixelColor(getHexFromChar(pixel), x, y);

      x++;
    }

    y++;
  }

  return j.getBufferAsync(Jimp.MIME_PNG);
}
github dadi / cdn / test / acceptance / visual.js View on Github external
return Jimp.read(cdnUrl + requestPath).then(testImage => {
      const diff = Jimp.diff(baselineImage, testImage, 0.1) // threshold ranges 0-1 (default: 0.1)
      const distance = Jimp.distance(baselineImage, testImage) // perceived distance

      if (distance < 0.15 || diff.percent < 0.15) {
        return
      }

      const error = new Error(
        `Image mismatch percentage: ${diff.percent *
          100}. Saving diff image to ${outputPath}.`
      )

      diff.image.write(outputPath)

      return Promise.reject(error)
    })
  })
github dadi / cdn / test / acceptance / visual.js View on Github external
return Jimp.read(cdnUrl + requestPath).then(testImage => {
      const diff = Jimp.diff(baselineImage, testImage, 0.1) // threshold ranges 0-1 (default: 0.1)
      const distance = Jimp.distance(baselineImage, testImage) // perceived distance

      if (distance < 0.15 || diff.percent < 0.15) {
        return
      }

      const error = new Error(
        `Image mismatch percentage: ${diff.percent *
          100}. Saving diff image to ${outputPath}.`
      )

      diff.image.write(outputPath)

      return Promise.reject(error)
    })
  })
github interledger-deprecated / ilp-kit / api / src / controllers / auth.js View on Github external
const file = ctx.request.body.files && ctx.request.body.files.file

      let user = ctx.req.user

      if (!user) throw new NotFoundError('No active user session')
      if (!file) throw new InvalidBodyError('Request doesn\'t include an image file')

      user = await User.findOne({where: {id: user.id}})

      const newFilePath = file.path + '_square.' + file.type.split('/')[1]

      // Resize
      try {
        const image = await jimp.read(file.path)

        image.cover(200, 200, jimp.HORIZONTAL_ALIGN_CENTER, jimp.VERTICAL_ALIGN_TOP)
          .write(newFilePath, err => {
            if (err) {
              console.log('auth:197', newFilePath, err)
            }
          })
      } catch (e) {
        throw new InvalidBodyError('Unsopported image format')
      }

      user.profile_picture = path.basename(newFilePath)

      try {
        await user.save()
      } catch (e) {
        console.log('auth.js:191', e)
      }
github Googer / Professor-Pine / app / process-image.js View on Github external
}

    // if not in a proper raid channel, cancel out immediately
    const regionId = await RegionHelper.getRegionId(message.channel.id)
      .catch(error => log.error(error));

    if (!regionId) {
      log.info('Not in a region channel, won\'t attempt to process');
      return;
    }

    // show users the bot is starting to process their image
    message.react('🤔')
      .catch(err => log.error(err));

    Jimp.read(url)
      .then(image => {
        if (!image) {
          return;
        }
        id = uuidv1();

        // resize to some standard size to help tesseract
        log.debug("Scaling image to standard size...");
        newImage = image.scaleToFit(1440, 2560, Jimp.RESIZE_HERMITE);
        log.debug("...done");

        // determine if image is a raid image or not
        let screenshotType = ImageProcessing.SCREENSHOT_TYPE_NONE;

        // check for pink "time remaining" pixels
        newImage.scan(newImage.bitmap.width / 2, (newImage.bitmap.height / 4.34) - 80, 1, 160, function (x, y, idx) {
github wesbos / Learn-Node / stepped-solutions / 36 / controllers / storeController.js View on Github external
exports.resize = async (req, res, next) => {
  // check if there is no new file to resize
  if (!req.file) {
    next(); // skip to the next middleware
    return;
  }
  const extension = req.file.mimetype.split('/')[1];
  req.body.photo = `${uuid.v4()}.${extension}`;
  // now we resize
  const photo = await jimp.read(req.file.buffer);
  await photo.resize(800, jimp.AUTO);
  await photo.write(`./public/uploads/${req.body.photo}`);
  // once we have written the photo to our filesystem, keep going!
  next();
};
github afuh / pinstagram / controllers / imageControllers.js View on Github external
exports.resize = async (req, res, next) => {
  if (req.body.caption && req.body.caption.length > 140) {
    req.flash('error', 'Upload failed, apparently you have written too much')
    return res.redirect('back')
  }
  //rename
  const extension = req.file.mimetype.split('/')[1];
  req.body.url = crypto.randomBytes(10).toString('hex');
  req.body.photo = `${req.body.url}.${extension}`;

  // resize
  const photo = await jimp.read(req.file.buffer);
  await photo.resize(600, jimp.AUTO).quality(70);
  await photo.write(`./public/uploads/${req.body.photo}`);

  await photo.cover(290, 290, jimp.HORIZONTAL_ALIGN_CENTER | jimp.VERTICAL_ALIGN_MIDDLE);
  await photo.write(`./public/uploads/gallery/${req.body.photo}`);

  next();
}
github xtremespb / zoia2 / modules_unused / boats / backend / apiBoatsSave.js View on Github external
await formData.default.plans.reduce(async (prev, p) => {
                        await prev;
                        const {
                            name
                        } = p;
                        if (req.body[name]) {
                            try {
                                const filename = uuid();
                                const img = await Jimp.read(req.body[name][0].data);
                                const thumb = await Jimp.read(req.body[name][0].data);
                                await fs.ensureDir(`${__dirname}/../static/cache/${id}/plans`);
                                img.scaleToFit(800, Jimp.AUTO);
                                img.quality(config.qualityThumbPic);
                                thumb.quality(config.qualityFullPic);
                                img.composite(watermark, img.bitmap.width - config.watermarkWidth, img.bitmap.height - config.watermarkHeight);
                                const imgBuffer = await img.getBufferAsync(Jimp.MIME_JPEG);
                                await fs.writeFile(`${__dirname}/../static/cache/${id}/plans/${filename}.jpg`, imgBuffer);
                                thumb.cover(config.thumbnailWidth, config.thumbnailHeight, Jimp.HORIZONTAL_ALIGN_CENTER | Jimp.VERTICAL_ALIGN_MIDDLE); // eslint-disable-line no-bitwise
                                const thumbBuffer = await thumb.getBufferAsync(Jimp.MIME_JPEG);
                                await fs.writeFile(`${__dirname}/../static/cache/${id}/plans/tn_${filename}.jpg`, thumbBuffer);
                                plansHash[name] = filename;
                            } catch (e) {
                                req.log.error({
                                    ip: req.ip,
                                    path: req.urlData().path,
                                    query: req.urlData().query,
                                    error: e.message
                                });
                                rep.code(400)
                                    .send(JSON.stringify({
                                        statusCode: 400,
                                        errors: {
github zinserjan / wdio-screenshot / src / utils / image / jimp.js View on Github external
let x = 0;
    for (const colImage of colImages) {
      image.blit(colImage, x, 0);
      x += colImage.bitmap.width
    }

    imageWidth = image.bitmap.width;
    imageHeight += image.bitmap.height;

    return image;
  });

  const rowImages = await Promise.all(rowImagePromises);

  // merge vertical
  const image = await new Jimp(imageWidth, imageHeight);

  let y = 0;
  for (const rowImage of rowImages) {
    image.blit(rowImage, 0, y);
    y += rowImage.bitmap.height;
  }

  // finally get screenshot
  const base64Screenshot = await new Promise((resolve, reject) => {
    image.getBuffer(Jimp.MIME_PNG,function (err, buffer) {
      if (err) {
        return reject(err);
      }
      return resolve(buffer.toString('base64'));
    })
  });
github xtremespb / zoia2 / modules_unused / boats / backend / apiBoatsSave.js View on Github external
const {
                            name
                        } = p;
                        if (req.body[name]) {
                            try {
                                const filename = uuid();
                                const img = await Jimp.read(req.body[name][0].data);
                                const thumb = await Jimp.read(req.body[name][0].data);
                                await fs.ensureDir(`${__dirname}/../static/cache/${id}/photos`);
                                img.scaleToFit(800, Jimp.AUTO);
                                img.quality(config.qualityThumbPic);
                                thumb.quality(config.qualityFullPic);
                                img.composite(watermark, img.bitmap.width - config.watermarkWidth, img.bitmap.height - config.watermarkHeight);
                                const imgBuffer = await img.getBufferAsync(Jimp.MIME_JPEG);
                                await fs.writeFile(`${__dirname}/../static/cache/${id}/photos/${filename}.jpg`, imgBuffer);
                                thumb.cover(config.thumbnailWidth, config.thumbnailHeight, Jimp.HORIZONTAL_ALIGN_CENTER | Jimp.VERTICAL_ALIGN_MIDDLE); // eslint-disable-line no-bitwise
                                const thumbBuffer = await thumb.getBufferAsync(Jimp.MIME_JPEG);
                                await fs.writeFile(`${__dirname}/../static/cache/${id}/photos/tn_${filename}.jpg`, thumbBuffer);
                                picsHash[name] = filename;
                            } catch (e) {
                                req.log.error({
                                    ip: req.ip,
                                    path: req.urlData().path,
                                    query: req.urlData().query,
                                    error: e.message
                                });
                                rep.code(400)
                                    .send(JSON.stringify({
                                        statusCode: 400,
                                        errors: {
                                            default: {
                                                name: 'Error while processing images'