stb (c++) 操作 jpg图片

目录

测试图

读图

取像素值 

保存图片

 修改像素值

中间-1列

红色

绿色

蓝色

中间-1行

红色

绿色 

蓝色 


测试图

s_l.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("./s_l.jpg", &w, &h, &n, 0);
    cout << "宽: " << w << endl;
    cout << "高: " << h << endl;
    cout << "channel: " << n << endl;
}
宽: 400
高: 328
channel: 3

取像素值 

#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("./s_l.jpg", &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;
}
宽: 400
高: 328
channel: 3
---------
^
7
(
---------
94
55
40

保存图片

#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("./s_l.jpg", &w, &h, &n, 0);

    string name = "s_l_new.jpg"; 
    stbi_write_jpg(name.c_str(), w,h,n,data,95);
}

 修改像素值

中间-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("./s_l.jpg", &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 = "s_l_new.jpg"; 
    stbi_write_jpg(name.c_str(), w,h,n,data,95);
}

绿色
#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("./s_l.jpg", &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 = "s_l_new.jpg"; 
    stbi_write_jpg(name.c_str(), w,h,n,data,95);
}

 

蓝色
#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("./s_l.jpg", &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 = "s_l_new.jpg"; 
    stbi_write_jpg(name.c_str(), w,h,n,data,95);
}

 

中间-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("./s_l.jpg", &w, &h, &n, 0);

    int red_row = h/2;
    for(int i=0; i<w; i++)
    {
        data[red_row*w*n+i*n+0] = 255;
        data[red_row*w*n+i*n+1] = 0;
        data[red_row*w*n+i*n+2] = 0;
    }
    string name = "s_l_new.jpg"; 
    stbi_write_jpg(name.c_str(), w,h,n,data,95);
}

 

绿色 
#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("./s_l.jpg", &w, &h, &n, 0);

    int red_row = h/2;
    for(int i=0; i<w; i++)
    {
        data[red_row*w*n+i*n+0] = 0;
        data[red_row*w*n+i*n+1] = 255;
        data[red_row*w*n+i*n+2] = 0;
    }
    string name = "s_l_new.jpg"; 
    stbi_write_jpg(name.c_str(), w,h,n,data,95);
}

 

蓝色 
#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("./s_l.jpg", &w, &h, &n, 0);

    int red_row = h/2;
    for(int i=0; i<w; i++)
    {
        data[red_row*w*n+i*n+0] = 0;
        data[red_row*w*n+i*n+1] = 0;
        data[red_row*w*n+i*n+2] = 255;
    }
    string name = "s_l_new.jpg"; 
    stbi_write_jpg(name.c_str(), w,h,n,data,95);
}

 

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值