onlyou13

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
::1.批量转换
gm convert -output-directory output -resize 320x320 "*.gif"

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

::3.单个转换,强制转换
gm convert -resize 600x1200! test.jpg output\t.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 output\c3.jpg

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

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

::9.旋转+背景色
gm convert test.jpg -background blue -rotate 45 output\t10.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

::14.gif分解
gm convert g3.gif +adjoin frame%02d.gif
::15. 合并成gif
gm convert d:\zzz1\*.gif d:\anim.gif

 

        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  

 

posted on 2020-04-20 19:54  onlyou13  阅读(293)  评论(0编辑  收藏  举报