How to make circle thumbnail with GraphicsMagick?

本小妞迷上赌 提交于 2020-01-23 03:39:11

问题


While exploring how to make circular crop with GraphicsMagick i came to that code:

gm convert -thumbnail 200x200^ -extent 200x200 kira.jpg kira_new.jpg && gm convert -size 200x200 xc:none -fill white -draw "circle 100,100 110,0" tmp.png && gm composite -compose CopyOpacity tmp.png kira_new.jpg out.png

Here's what it does:

  1. Creates 200x200 temp file named kira_new.jpg
  2. Creates transparent tmp.png with white circle in the middle
  3. Composition on tmp.png and kira_new.jpg

So the question is: is there any way to make it shorter instead of running 3 commands?


回答1:


Updated Answer

After much experimenting with GraphicsMagick, and disappointment at its lack of features and flexibility compared to ImageMagick (-clone where are you? -swap where are you? aside-processing where are you? -alpha where are you?), the nearest I can get to a single GraphicsMagick command without intermediate files is this:

{
   echo convert -size 400x400 xc:none -fill white -draw \"circle 100,100 200,100\" mpr:mask
   echo convert image.jpg -resize 400x400 mpr:image
   echo composite -compose copyopacity mpr:mask mpr:image result.png
} | gm batch -prompt off

Original Answer

Not sure how you would do that with GraphicsMagick, but as you have tagged with ImageMagick too, I will give a solution based on that:

Let's start with a red square as I don't have a "kira" to hand:

convert -size 400x400 xc:red image.jpg

Now, we clone the square (which is easier than getting its size and adding the dimensions) and then turn on its transparency. Then draw a circle, filled with whatever the default fill colour is - it doesn't matter so long as it is opaque. Then we tell ImageMagick it should copy the opacity channel when we composite in the next step before saving as a PNG:

convert image.jpg  \( +clone -alpha transparent -draw "circle 100,100 200,100" \) -compose copyopacity -composite result.png

If you want to do resizing, do it in the area where I have added dots:

convert image.jpg -resize ... -extent ... \( +clone -alpha ...


来源:https://stackoverflow.com/questions/32274215/how-to-make-circle-thumbnail-with-graphicsmagick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!