stb (c++) 操作png图片

这篇文章详细介绍了如何使用C++和STB库进行图像处理,包括读取世界.png文件的尺寸和通道数,获取并修改特定像素值,以及将修改后的图片保存为jpg和png格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

测试图

读图

取像素值

保存图片

看下world-jpg.jpg属性

修改像素值

中间-1列

红色

绿色

蓝色

中间-1行

红色

绿色

蓝色


测试图

world.png

读图

#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"

using namespace std;

int main()
{
    int w,h,n;
    unsigned char *data = stbi_load("./world.png", &w, &h, &n, 0);
    cout << "宽: " << w << endl;
    cout << "高: " << h << endl;
    cout << "channel: " << n << endl;
}
宽: 1950
高: 1092
channel: 4

取像素值

#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"

using namespace std;

int main()
{
    int w,h,n;
    unsigned char *data = stbi_load("./world.png", &w, &h, &n, 0);
    cout << "宽: " << w << endl;
    cout << "高: " << h << endl;
    cout << "channel: " << n << endl;
    
    cout << "---------\n";
    int x=200,y=100;
    cout << data[y*w*n + x*n + 0] << endl;
    cout << data[y*w*n + x*n + 1] << endl;
    cout << data[y*w*n + x*n + 2] << endl;
    cout << "---------\n";
    cout << int(data[y*w*n + x*n + 0]) << endl;
    cout << int(data[y*w*n + x*n + 1]) << endl;
    cout << int(data[y*w*n + x*n + 2]) << endl;
}
宽: 1950
高: 1092
channel: 4
---------
0
A
%
---------
48
65
37

保存图片

#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image_write.h"
using namespace std;

int main()
{
    int w,h,n;
    unsigned char *data = stbi_load("./world.png", &w, &h, &n, 0);

    string name = "world-jpg.jpg"; 
    stbi_write_jpg(name.c_str(), w,h,n,data,95);
    string name2 = "world-png.png";
    stbi_write_png(name2.c_str(),w,h,n,data,0);
}
看下world-jpg.jpg属性
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"

using namespace std;

int main()
{
    int w,h,n;
    unsigned char *data = stbi_load("./world-jpg.jpg", &w, &h, &n, 0);
    cout << "宽: " << w << endl;
    cout << "高: " << h << endl;
    cout << "channel: " << n << endl;
}
宽: 1950
高: 1092
channel: 3

修改像素值

中间-1列
红色
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image_write.h"
using namespace std;

int main()
{
    int w,h,n;
    unsigned char *data = stbi_load("./world.png", &w, &h, &n, 0);

    int red_col = w/2;
    for(int i=0; i<h; i++)
    {
        data[w*i*n+red_col*n+0] = 255;
        data[w*i*n+red_col*n+1] = 0;
        data[w*i*n+red_col*n+2] = 0;
    }
    string name = "world-plot.jpg"; 
    stbi_write_jpg(name.c_str(), w,h,n,data,95);
    string name2 = "world-plot.png";
    stbi_write_png(name2.c_str(),w,h,n,data,0);
}

绿色
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image_write.h"
using namespace std;

int main()
{
    int w,h,n;
    unsigned char *data = stbi_load("./world.png", &w, &h, &n, 0);

    int red_col = w/2;
    for(int i=0; i<h; i++)
    {
        data[w*i*n+red_col*n+0] = 0;
        data[w*i*n+red_col*n+1] = 255;
        data[w*i*n+red_col*n+2] = 0;
    }
    string name = "world-plot.jpg"; 
    stbi_write_jpg(name.c_str(), w,h,n,data,95);
    string name2 = "world-plot.png";
    stbi_write_png(name2.c_str(),w,h,n,data,0);
}

蓝色
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image_write.h"
using namespace std;

int main()
{
    int w,h,n;
    unsigned char *data = stbi_load("./world.png", &w, &h, &n, 0);

    int red_col = w/2;
    for(int i=0; i<h; i++)
    {
        data[w*i*n+red_col*n+0] = 0;
        data[w*i*n+red_col*n+1] = 0;
        data[w*i*n+red_col*n+2] = 255;
    }
    string name = "world-plot.jpg"; 
    stbi_write_jpg(name.c_str(), w,h,n,data,95);
    string name2 = "world-plot.png";
    stbi_write_png(name2.c_str(),w,h,n,data,0);
}

中间-1行
红色
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image_write.h"
using namespace std;

int main()
{
    int w,h,n;
    unsigned char *data = stbi_load("./world.png", &w, &h, &n, 0);

    int red_col = h/2;
    for(int i=0; i<w; i++)
    {
        data[w*red_col*n+i*n+0] = 255;
        data[w*red_col*n+i*n+1] = 0;
        data[w*red_col*n+i*n+2] = 0;
    }
    string name = "world-plot.jpg"; 
    stbi_write_jpg(name.c_str(), w,h,n,data,95);
    string name2 = "world-plot.png";
    stbi_write_png(name2.c_str(),w,h,n,data,0);
}

绿色
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image_write.h"
using namespace std;

int main()
{
    int w,h,n;
    unsigned char *data = stbi_load("./world.png", &w, &h, &n, 0);

    int red_col = h/2;
    for(int i=0; i<w; i++)
    {
        data[w*red_col*n+i*n+0] = 0;
        data[w*red_col*n+i*n+1] = 255;
        data[w*red_col*n+i*n+2] = 0;
    }
    string name = "world-plot.jpg"; 
    stbi_write_jpg(name.c_str(), w,h,n,data,95);
    string name2 = "world-plot.png";
    stbi_write_png(name2.c_str(),w,h,n,data,0);
}

蓝色
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image_write.h"
using namespace std;

int main()
{
    int w,h,n;
    unsigned char *data = stbi_load("./world.png", &w, &h, &n, 0);

    int red_col = h/2;
    for(int i=0; i<w; i++)
    {
        data[w*red_col*n+i*n+0] = 0;
        data[w*red_col*n+i*n+1] = 0;
        data[w*red_col*n+i*n+2] = 255;
    }
    string name = "world-plot.jpg"; 
    stbi_write_jpg(name.c_str(), w,h,n,data,95);
    string name2 = "world-plot.png";
    stbi_write_png(name2.c_str(),w,h,n,data,0);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值