png images反色保留背景透明

将src-png-folder目录下的所有png images反色并保留保留透明不变。


import os
from PIL import Image

def get_files(src_path):
    results = []
    for filepath, dirnames, filenames in os.walk(src_path):
        for filename in filenames:
            path = os.path.join(filepath,filename)
            results.append(path)
            pass
        pass
    return results


files=get_files(r"./src-png-folder")

for file in files:
    result_file = os.path.join("result", file)
    result_dirname = os.path.dirname(result_file)
    print(result_file, "->", result_dirname)
    if not os.path.exists(result_dirname):
        os.makedirs(result_dirname)

    (file_name, ext) = os.path.splitext(file)
    is_png = ext.lower() == ".png"
    print(ext, " is_png:", is_png)
    print("->", file)

    image = Image.open(file).convert('RGBA')
    print("mode:", image.mode)

    newImage = []
    for item in image.getdata():
        (r, g, b, a) = item
        newImage.append(( 255 - r, 255 - g, 255 - b, a))
    image.putdata(newImage)
    image.save(result_file)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
procedure BitmapFileToPNG(const Source, Dest: String); var Bitmap: TBitmap; PNG: TPNGObject; begin Bitmap := TBitmap.Create; PNG := TPNGObject.Create; {In case something goes wrong, free booth Bitmap and PNG} try Bitmap.LoadFromFile(Source); PNG.Assign(Bitmap); //Convert data into png PNG.SaveToFile(Dest); finally Bitmap.Free; PNG.Free; end end; Converting from PNG file to Windows bitmap file The above inverse. Loads a png and saves into a bitmap procedure PNGFileToBitmap(const Source, Dest: String); var Bitmap: TBitmap; PNG: TPNGObject; begin PNG := TPNGObject.Create; Bitmap := TBitmap.Create; {In case something goes wrong, free booth PNG and Bitmap} try PNG.LoadFromFile(Source); Bitmap.Assign(PNG); //Convert data into bitmap Bitmap.SaveToFile(Dest); finally PNG.Free; Bitmap.Free; end end; Converting from TImage to PNG file This method converts from TImage to PNG. It has full exception handling and allows converting from file formats other than TBitmap (since they allow assigning to a TBitmap) procedure TImageToPNG(Source: TImage; const Dest: String); var PNG: TPNGObject; BMP: TBitmap; begin PNG := TPNGObject.Create; {In case something goes wrong, free PNG} try //If the TImage contains a TBitmap, just assign from it if Source.Picture.Graphic is TBitmap then PNG.Assign(TBitmap(Source.Picture.Graphic)) //Convert bitmap data into png else begin //Otherwise try to assign first to a TBimap BMP := TBitmap.Create; try BMP.Assign(Source.Picture.Graphic); PNG.Assign(BMP); finally BMP.Free; end; end; //Save to PNG format PNG.SaveToFile(Dest); finally PNG.Free; end end;
之前上传的版本如果安装了DEV组件,则加载PNG图片时将会出错。感谢下载的网友指出的错误,因为本人上传之后一直未关注过评论,今天偶然发现,在此向各位致歉。 我在本机的XE版本下安装时好像不需要obj文件,但既然有网友指出,我就将d7目录下的obj拷贝了一份放在xe源码目录内。 该修改版本仅在D7和XE中测试。如果是其他版本,请自行修改。 安装说明参见压缩包内的reademe文件。 ---------------------------------------------------------- 针对XE版本增加的属性 如下: Anchors: TAnchors; //按钮相对位置 Action: TActionList; //与 actionlist 连接 CaptionAlign: TAlignment; //按钮标题对齐方式 //是否填充渐变,取决于后两个变量 IsDrawGradientColorFace: Boolean; //是否以渐变填充按钮表面 IsDrawFaceOnMouseOver: boolean; //鼠标悬停时是否填充按钮表面 IsDrawFaceOnPush: Boolean; //鼠标按下时是否填充按钮表面 //边框 IsDrawBorderOnMouseOver: Boolean; IsDrawBorderOnPush: Boolean; //悬停及按下时的渐变设置 DrawOverFaceStartColor: TColor; //鼠标悬停时渐变填充起始 DrawOverFaceEndColor: TColor; //鼠标悬停时渐变填充结束 DrawPushFaceStartColor: TColor; DrawPushFaceEndColor: TColor; //按钮表面单 DrawUniqueColorFaceOnMouseOver: TColor; //鼠标悬停时单填充时的颜 DrawUniqueFaceColorOnPush: TColor; //边框 DrawBorderOnMouseOver: TColor; //鼠标悬停时边框颜 DrawBorderOnPush: TColor; IsShowCaption: boolean; //是否显示按钮文字 注:与颜相关的属性仅在 buttonStyle 设为 pbsFlat 或 pbsNoFrame 时有效

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值