批量更改图片大小程序

假设我们现在有目标文件夹A,A中包含40个文件夹s1,s2...s40,每个文件夹下包含10张图1,2,3...10。图片大小任意。

我们的目标是在不改变整个图片数据格式的前提下,批量更改图片大小,最后得到的结果是,输出B文件夹,B中包含40个文件夹s1,s2...s40,每个文件夹下包含10张图1,2,3...10,图片大小为我们所需要的大小(这里我使用256X256)。

具体实现借助opencv中的resize函数,具体用法自行百度,参数调用CV_INTER_LINEAR(双线性插值),放缩效果还不错。

贴代码=。=

// Resize_32x32.cpp : 定义控制台应用程序的入口点。
//**********************************************
*           批量更改图片大小
*           Win8.1+VS2015+Opencv 2.4.8
*           King_HAW
*           2016/12/10
***********************************************/

#include "stdafx.h"
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <string>
#include <direct.h>
using namespace cv;
using namespace std;


int main()
{
	int j = 1;
	int k;
	int i;
	for (k = 1; k <= 40; k++)
	{
		for (i = 1; i <= 10; i++)
		{
			
			char imageName[100];
			char imageSave[100];
			char FileName[100];
			sprintf_s(imageName, "E:\\深度学习\\face_data\\orl_faces_png\\s%d\\%d.png", k, i);
			sprintf_s(imageSave, "E:\\ORL_FACE\\s%d\\%d.png", k, j);
			sprintf_s(FileName,  "E:\\ORL_FACE\\s%d", k);
			_mkdir(FileName);
			Mat src_img = imread(imageName);//读入图片数据
			Mat dst_img1;
			resize(src_img, dst_img1, Size(256, 256), 0, 0, CV_INTER_LINEAR);	// 双线性插值
			imwrite(imageSave, dst_img1);
			waitKey(0);
			++j;
		}
		j = 1;
	}
}

路径为字符数组,不要直接用字符串。

如果你的图片数据大于两层的话请按照数据层数套for循环。

有时间也可以试试CV_INTER_CUBIC(立方插值),应该会有所差别,选择你认为较好的那个。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
代码请参考笔者的另一个资源。 我们用千万像素级相机拍摄的照片,通常达到好几兆,时间长了,相片多了,就很占空间,但是又舍不得删除。怎么办?减少图片的精度,也就减小了所占空间。事实上,用Windows XP自带的画图程序(Win7自带的画图程序没试过),打开图片,然后另存,即可发现图片体积大大减小,但是精度的损失用人眼是判断不出来的。可是如果每个图片都用画图程序打开再另存,势必很费时间。于是,小弟就开发了一个批量减少图片体积的小程序,可以递归执行。需要注意的是,要先选中递归复选框,然后再选择文件夹,否则不会递归。这也算是一个小bug吧。 We do pixel-level camera photographs, usually reaches several trillion, a long time, photos and more, it is the space, but they could not bear to delete. How to do? Reduce image precision, it reduces the footprint. In fact, with the Paint program that comes with Windows XP (Win7 Paint program that comes with not tried), open the image, then save it, you can find pictures volume greatly reduced, but the loss of accuracy of the human eye is not out of judgment. But if each picture are turned on and then save it with a drawing program, it is bound to be time-consuming. Thus, the younger brother developed a small batch volume reduction picture program can recursively. Note, first check the Recursive checkbox, and then select the folder, otherwise not recursively. It would be a little bug it.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值