bmp格式图片缩放(位图缩放)
- bmp格式图片缩放(位图缩放) 推荐度:
- 相关推荐
bmp格式图片缩放(位图缩放)
bmp位图格式理逐渐地被广泛应用,其格式的特点是包含的图像信息较丰富,几乎不进行压缩。bmp图片由文件头,位图信息头,24位图片以下还有调色板,和图像数据组成。位图的信息头包含下列变量:
调色板的结构为蓝,绿,红的顺序排列,最后一位为保留项。有了调色板的索引表,在图像数据里就会用索引值表示像素点。图片的实际宽度大小=位图宽度存储每个像素需要的字节数除以4。
在缩放bmp格式的图片时,需要对bmp图片进行读取,设计时,BMP图像用C语言实现图像的缩放。将bmp图片读取后,需要对其格式进行解析,然后对宽度和高度分别放大或缩小一定的尺寸比例,缩放的倍数通过输入设定。代码如下:
#include <Windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>int main(int argc, char ** argv)
{FILE *fp = fopen("C:/Users/23011/Desktop/ttt/input.bmp","rb");if (fp == 0)return 0;BITMAPFILEHEADER fileHead;fread(&fileHead, sizeof(BITMAPFILEHEADER), 1, fp);BITMAPINFOHEADER infoHead;fread(&infoHead, sizeof(BITMAPINFOHEADER), 1, fp);int width = infoHead.biWidth;int height = infoHead.biHeight;int biCount = infoHead.biBitCount;RGBQUAD *pColorTable;int pColorTableSize = 0;pColorTable = new RGBQUAD[256];fread(pColorTable, sizeof(RGBQUAD), 256, fp);pColorTableSize = 1024;unsigned char *pBmpBuf;int lineByte = (width*biCount / 8 + 3) / 4 * 4;pBmpBuf = new unsigned char[lineByte*height];fread(pBmpBuf, lineByte*height, 1, fp);fclose(fp);printf("input the scale");float lx, ly;scanf("%f%f", &lx, &ly);int dstWidth = round(double(lx*width));int dstHeight = round(double(ly*height));int lineByte2 = (dstWidth*biCount / 8 + 3) / 4 * 4;unsigned char*pBmpBuf2;pBmpBuf2 = new unsigned char[lineByte2*dstHeight];for (int i = 0; i < dstHeight; ++i){for (int j = 0; j < dstWidth; ++j){unsigned char *p;p = (unsigned char *)(pBmpBuf2 + lineByte2*i + j);(*p) = 255;}}int x = 0;int y = 0;for (int i = 0; i < height; ++i){for (int j = 0; j < width; ++j){unsigned char *p1, *p2;x = round(double(lx*j));y = round(double(ly*i));p1 = (unsigned char *)(pBmpBuf + i*lineByte + j);p2 = (unsigned char *)(pBmpBuf2 + y*lineByte2 + x);(*p2) = (*p1);}}FILE *fpo = fopen("C:/Users/23011/Desktop/ttt/output.bmp", "wb");if (fpo == 0)return 0;BITMAPFILEHEADER dstFileHead;dstFileHead.bfOffBits = 14 + 40 + pColorTableSize;dstFileHead.bfReserved1 = 0;dstFileHead.bfReserved2 = 0;dstFileHead.bfSize = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+pColorTableSize + lineByte2*dstHeight;dstFileHead.bfType = 0x4D42;fwrite(&dstFileHead, sizeof(dstFileHead), 1, fpo);BITMAPINFOHEADER dstInfoHead;dstInfoHead.biBitCount = biCount;dstInfoHead.biClrImportant = 0;dstInfoHead.biClrUsed = 0;dstInfoHead.biCompression = 0;dstInfoHead.biHeight = dstHeight;dstInfoHead.biPlanes = 1;dstInfoHead.biSize = 40;dstInfoHead.biSizeImage = lineByte2*dstHeight;dstInfoHead.biWidth = dstWidth;dstInfoHead.biXPelsPerMeter = 0;dstInfoHead.biYPelsPerMeter = 0;fwrite(&dstInfoHead, sizeof(BITMAPINFOHEADER), 1, fpo);fwrite(pColorTable, sizeof(RGBQUAD), 256, fpo);fwrite(pBmpBuf2, lineByte2*dstHeight, 1, fp);fclose(fpo);system("pause");return 0;
}
当然,上面的C++代码相当于手撕代码了,纯纯地手动对BMP位图进行缩放,如果没有特殊要求的话,我们还可以直接使用python库进行resize(CV库,Image库都可以~),以Image库为例,代码如下:
from PIL import Image
img = Image.open('DIR')imgSize = img.size
w = img.width
h = img.height
print('Before Resize:\n')
print(imgSize)
print(w, h)out = img.resize((400, 300))
imgSize = out.size
w = out.width
h = out.height
print('======================')
print('After Resize:\n')
print(imgSize)
print(w, h)
out.show()
out.save('DIR')
是不是简单很多了呢😄
最新文章
- 盘点2012:软件定义网络风生水起
- jkd8新特性 StreamAPi流
- 亲测Linux服务器安装JKD操作流程
- JKD+Tomcat+Eclipse基础配置
- #今日论文推荐# 陈天奇、王威廉等人推荐:ACL最佳论文奖得主给新入行研究者的一点建议
- 破解SQLyog 30天试用
- Windows上使用ionic开发IOS程序
- Emgucv使用中常用函数总结
- JS exports的用法
- NLP
- Python四行代码实现的猜数字小游戏,基于thinker,带GUI界面
- web中gzip,deflate的压缩与解压
- Janus源码分析(1)——源码结构
- tensorflow高阶教程:tf.dynamic
- 视频编解码——编码流程介绍
- 推荐给初学者:如何学好C语言?
- ThreadLock