bmp格式图片缩放(位图缩放)

bmp位图格式理逐渐地被广泛应用,其格式的特点是包含的图像信息较丰富,几乎不进行压缩。bmp图片由文件头,位图信息头,24位图片以下还有调色板,和图像数据组成。位图的信息头包含下列变量:
bmp格式图片缩放(位图缩放)_第1张图片
调色板的结构为蓝,绿,红的顺序排列,最后一位为保留项。有了调色板的索引表,在图像数据里就会用索引值表示像素点。图片的实际宽度大小=位图宽度存储每个像素需要的字节数除以4。
在缩放bmp格式的图片时,需要对bmp图片进行读取,设计时,BMP图像用C语言实现图像的缩放。将bmp图片读取后,需要对其格式进行解析,然后对宽度和高度分别放大或缩小一定的尺寸比例,缩放的倍数通过输入设定。代码如下:

#include 
#include 
#include 
#include 

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')

是不是简单很多了呢

你可能感兴趣的:(python,C++,c++,python)