Created
October 1, 2022 11:07
-
-
Save AhmedElwerdany/67c3b86407bd2889d2fe27b2a4c42965 to your computer and use it in GitHub Desktop.
round corners (border-radius) with imagemagick
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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