Skip to content

Instantly share code, notes, and snippets.

@AhmedElwerdany
Created October 1, 2022 11:07
Show Gist options
  • Save AhmedElwerdany/67c3b86407bd2889d2fe27b2a4c42965 to your computer and use it in GitHub Desktop.
Save AhmedElwerdany/67c3b86407bd2889d2fe27b2a4c42965 to your computer and use it in GitHub Desktop.
round corners (border-radius) with imagemagick
import { execSync } from 'child_process';
import path from 'path';
function roundImage(src_path, dest_path,radius = 15){
const {w,h} = getSize(src_path)
const maskName = `mask.png`
execSync(`convert -size ${w}x${h} xc:none -draw "roundrectangle 0,0,${w},${h},${radius},${radius}" ${maskName}`)
execSync(`convert ${src_path} -matte ${maskName} -compose DstIn -composite ${dest_path}`)
}
/**
*
* @param string imagePath
* @return {w:number, h:number}
*/
function getSize(imagePath) {
// D:\data\smile.jpg JPEG 612x612 612x612+0+0 8-bit sRGB 20238B 0.000u 0:00.000
const data = execSync(`magick identify ${path.resolve(imagePath)}`,{encoding: 'utf-8'})
//extracting the size
const [w,h] = data.split(' ')[2].split('x').map(n => +n)
return ({w,h})
}
roundImage('../images/main_image.jpg', '../images/rounded.png', 100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment