GraphicsMagick 学习

Posted onlyou13

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GraphicsMagick 学习相关的知识,希望对你有一定的参考价值。

::1.批量转换
gm convert -output-directory output -resize 320x320 "*.gif"

::2.单个转换,锁定长宽比
gm convert -resize 600x1200 test.jpg output	.jpg

::3.单个转换,强制转换
gm convert -resize 600x1200! test.jpg output	.jpg

::4.获取图片信息
gm identify test.jpg
test.jpg JPEG 1200x800+0+0 DirectClass 8-bit 172.0Ki 0.000u 0m:0.000006s

::5.图片格式转换
gm convert test.jpg test.png

::6.图片裁剪
gm convert test.jpg -crop 50x50+10+10 outputc3.jpg

::7.图片裁剪
gm convert test.jpg -gravity east -crop 50x50+10+10 outputc3.jpg

::8.按比例裁剪
gm convert test.jpg -crop 50%x50% +repage output	5_d.jpg

::9.旋转+背景色
gm convert test.jpg -background blue -rotate 45 output	10.jpg

::10.生成一张白色图片
gm convert -size 1000x800 xc:"#fff" white.jpg

::11.更改gif的帧率,应该是每张图片的延时
gm convert -delay 3 1.gif d1.gif

::12.更改gif的循环播放次数
gm convert -loop 3 1.gif d2.gif

::13.图片叠加
gm composite -dissolve 10 flag.jpg  eagle.jpg o1.jpg
gm composite -dissolve 30 -resize 800x800 flag.jpg -gravity center -resize 600x600 eagle.jpg o2.jpg

gm convert infile.jpg -thumbnail 120x80 -background red -gravity center -extent 140x100-10-5 outfile.jpg

gm convert background.png -compose over overlay.png -geometry 100x100+0+0 -composite new.png

 

        if self.mode == stretch or srcRateStr == destRateStr or srcRate == destRate:
            # 拉伸模式 或 等比例(长宽比)转换
            cmd = gm convert "{}" -resize {}x{}! {}.format(self.srcFile, self.destWidth, self.destHeight, self.fullOutputPicFile)
        elif self.mode == fill and srcRate < destRate:
            maxW = self.destWidth
            maxH = int(round(self.destWidth / srcRate))
            offsetH = int(round((maxH - self.destHeight) / 2))
            print(maxH = , maxH)
            print(maxW = , maxW)
            print(offsetH = , offsetH)
            cmd = gm convert "{}" -resize {}x{} -crop {}x{}+0+{} "{}".format(self.srcFile, maxW, maxH, self.destWidth, self.destHeight, offsetH, self.fullOutputPicFile)
        elif self.mode == fill and srcRate > destRate:            
            maxH = self.destHeight
            maxW = int(self.destHeight * srcRate)
            offsetW = int(round((maxW - self.destWidth) / 2))
            print(maxH = , maxH)
            print(maxW = , maxW)
            print(offsetW = , offsetW)
            cmd = gm convert "{}" -resize {}x{} -crop {}x{}+{}+0 "{}".format(self.srcFile, maxW, maxH, self.destWidth, self.destHeight, offsetW, self.fullOutputPicFile)
        elif self.mode == fit and srcRate < destRate:
            minH = self.destHeight
            minW = int(self.destHeight * srcRate)
            backPic = self.whitePic
            if self.fillColor == black:
                backPic = self.blackPic
            cmd = gm composite -resize {}x{} -gravity center "{}" -resize {}x{}! "{}" -dissolve 100 "{}".format(minW, minH, self.srcFile, self.destWidth, self.destHeight, backPic, self.fullOutputPicFile)
        elif self.mode == fit and srcRate > destRate:
            minW = self.destWidth
            minH = int(self.destWidth / srcRate)
            backPic = self.whitePic
            if self.fillColor == black:
                backPic = self.blackPic
            cmd = gm composite -resize {}x{} -gravity center "{}" -resize {}x{}! "{}" -dissolve 100 "{}".format(minW, minH, self.srcFile, self.destWidth, self.destHeight, backPic, self.fullOutputPicFile)

 

def runGraphicMagicCmd(cmd):
    output = ‘‘
    error = ‘‘
    try:
        ret = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
        output = ret.communicate()[0].decode().strip()
        error = ret.communicate()[1].decode().strip()
        ret.stderr.close()
        ret.stdout.close()
    except Exception as ex:
        error = str(ex)
    return output, error

def runConvertCmd(cmd):
    _, error = runGraphicMagicCmd(cmd)

    if len(error) == 0:
        error = success
    return error == success, error  

 

以上是关于GraphicsMagick 学习的主要内容,如果未能解决你的问题,请参考以下文章

GraphicsMagick 网络摄像头,带有印章和私人区域

ImageMagick 和 GraphicsMagick 有啥区别?

CentOS7下安装GraphicsMagick1.3.21

linux图片处理工具GraphicsMagick安装使用

GraphicsMagick 的 Convert 命令选项的正确索引位置是啥

GraphicsMagick和ImageMagick有什么区别?