批处理抠图(透明图)

有张大的图片(png)格式,是张比较大的图,需要把内容,提取出来(周围是透明,即alpha值为0)

可以达到下图的效果,且自动输出所有扣除图片坐上角的坐标到文本中(./out/results.txt')

附上python脚本源码:

import sys
import os
import Image

def parsepng_2(filename):
    print 'filename is :' , filename
    R, G, B ,A = 0, 1, 2,3
    im =Image.open(filename)
    imageW = im.size[0]
    imageH = im.size[1]
    print im.format , im.size , im.mode

    myleft=10000
    myright=0
    mytop=10000
    mybottom=0
    mywidth =0 
    myheigth = 0
    i = 0
    for y in range(0, imageH):
        for x in range(0, imageW):
            offset = y*imageW + x
            xy = (x, y)
            rgba = im.getpixel(xy)
            #print rgba
            # (255, 240, 0, 255)
            if rgba[R] == 255  and rgba[G] == 255 \
                and rgba[B] == 255  and  rgba[A] == 0: 
                continue
            else:
                if x <= myleft :
                    myleft =x
                if x >= myright :
                    myright =x
                if y <= mytop :
                    mytop = y
                if y >= mybottom :
                    mybottom = y
    print  myleft , myright , mytop  ,  mybottom
    mywidth = myright - myleft +1
    myheigth =  mybottom - mytop+1
    print mywidth , myheigth
    outim=Image.new("RGBA", (mywidth,myheigth))
    for y in range(0, myheigth):
        for x in range(0, mywidth):
            xy = (x, y)
            xyorigin = (x+myleft, y+mytop)
            rgbaorigin = im.getpixel(xyorigin)
            outim.putpixel(xy,rgbaorigin)
    
    outim.save('./out/%s'%(filename),)
    os.system('echo %d , %d , // %s >> ./out/results.txt'%(myleft,mytop,filename))



os.system('mkdir  ./out')
os.system('rm -rf  ./out/*')

pngfilelist = []
files = os.walk('./')
for pngfile in files:
    for item in pngfile:   
        for i in item:
            if 'png' in i:         
                pngfilelist.append(i)

os.system('echo "int imgIndex[ %d ] = {" >> ./out/results.txt'%(len(pngfilelist)))
for pngfile in pngfilelist:
    parsepng_2('%s'%(pngfile))  

os.system('echo "}; " >> ./out/results.txt')

 

简单的使用说明:

 

0
   先确保python已经安装
1
   安装python 图形处理库
        $ tar xvfz Imaging-1.1.7.tar.gz
        $ cd Imaging-1.1.7
        $ python setup.py install

2  *.png 图片放在此目录下

3   ./suguang.py
   或者  python ./suguang.py

   不能执行的话先:
   chmod 777 ./suguang.py

4
   图片生成在 ./out 目录下
   ./out/results.txt 保存的是坐标的信息

 

完整工程放在:

http://git.oschina.net/xmeter/pick-picture

good luck !


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
改变透明度 很好的学习用工码 attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); Bitmap resultImage = new Bitmap(srcImage.Width, srcImage.Height); Graphics g = Graphics.FromImage(resultImage); g.DrawImage(srcImage, new Rectangle(0, 0, srcImage.Width, srcImage.Height), 0, 0, srcImage.Width, srcImage.Height, GraphicsUnit.Pixel, attributes); return resultImage; } #region 不安全代码 需要设置项目允许不安全代码 /// /// 方法二 设置透明度,若原不为32位ARGB格式,则自动转换为32位ARGB输出 /// /// 原 /// 透明度(0~1之间双精度浮点数) //public static unsafe void SetTransparent(ref Bitmap src, double transparency) //{ // if (transparency 1.0) // throw new ArgumentOutOfRangeException("透明度必须为0~1之间的双精度浮点数"); // BitmapData srcData; // Rectangle rect; // byte* p; // rect = new Rectangle(0, 0, src.Width, src.Height); // src = src.Clone(rect, PixelFormat.Format32bppArgb); // //转换到32位,否则丢失透明度 // srcData = src.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); // //锁定字节数组到内存 // p = (byte*)srcData.Scan0.ToPointer(); // //获取字节数组在内存中地址 // if (src.PixelFormat == PixelFormat.Format32bppArgb) // { // //如果是32位片,有Alpha通道,以原Alpha通道的值为基准 // p += 3; // //默认修改Alpha通道的值 // for (int i = 0; i < srcData.Stride * srcData.Height; i += 4) // { // *p = (byte)(*p * transparency); // //原Alpha通道值*透明度 // p += 4; // }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值