图像转mif文件的操作步骤

14 篇文章 8 订阅

图像文件转mif步骤说明

1.使用imaeg2LCD工具将图象文件转成.bin文件。
a)点击打开按钮选择待转换的图像文件。
b)选择输出灰度为256色,不选择包含图象头数据,调整最大宽度和高度。
c)点击保存按钮生成.bin文件。

在这里插入图片描述

2.使用BmpToMif工具将.bin文件转换成.mif文件。
a)切换数据文件标签页下。
b)使用打开文件按钮找到源文件,将字长改为8
c)点击生成Mif文件按钮,将.bin文件转换成.mif文件。

在这里插入图片描述

Bmp To Mif 换器 // (karimov 2005) // This program was originnaly written by one of the ECE241 students to convert an image // supplied in a BMP file into an MIF file format for use with Quartus II. // // This program has recently been modified to work with the new VGA controller used with the DE2 // board. // // What to do: // 1. Create an image in Microsoft paint (or other program). The image must be 160 pixels wide, 120 pixels high and // use 24-bits to represent colour information. // 2. Once you create the image you need, flip it up-side down. Then save the BMP file. (example: foo.bmp) // 3. Run this converter in command prompt using the name of the BMP file you created as a command-line parameter. // For example: // bmp2mif foo.bmp // 4. The program generates two files: // image.colour.mif - an MIF file with 3 bits colour information obtained from the BMP file you supplied // image.mono.mif - an MIF file containing 1 bit of colour for every pixel on the screen. The dot will either be // black or white. // You can change the file names once they are created, but they should still have the .mif extension. // // 5. Copy the proper MIF file to the directory where your design is located and include it in your project. // 6. Change the BACKGROUND_IMAGE parameter of the VgaAdapter to indicate your MIF file. // 7. The COLOR_CHANNEL_DEPTH parameter must be set to 1 to work with the image.colour.mif file. #include #include #define FLIP_INT(c) ((c >> 24) & 0x000000FF) | ((c & 0x00FF0000) >> 8) | ((c & 0x0000FF00) << 8) | ((c & 0x000000FF) <> 8) | ((c & 0x00FF) << 8) typedef struct s_header { unsigned short bfType; unsigned int bfSize; unsigned short reserved1; unsigned short reserved2; unsigned int offset; } t_bmp_header; typedef struct s_bmp_info { unsigned int biSize; unsigned int biWidth; unsigned int biHeight; unsigned short biPlanes; unsigned short biBitCount; unsigned int biCompression; unsigned int biSizeImage; unsigned int biXPelsPerMeter; unsigned int biYPelsPerMeter; unsigned int biClrUsed; unsigned int biClrImportant; } t_bmp_info; int faprint(FILE *fcol, FILE *fm, const char *pattern) { fprintf(fcol, pattern); return fprintf(fm, pattern); } int main(int argc, char* argv[]) { FILE *f, *fcol, *fm; int y; unsigned int x, c, r, g, b; unsigned int width, height; if (argc != 2) { printf("Usage: bmp2mif \n"); return 0; } else { printf("Input file is: %s\n", argv[1]); } printf("This program converts n x m 24-bit .BMP image to MIF file\n"); printf("There are 2 files produced:\n"); printf("\timage.colour.mif - 8-colour channel, n x m x 3\n"); printf("\timage.mono.mif - black and white image, n x m x 1\n\n"); f = fopen(argv[1], "rb"); fcol = fopen("image.colour.mif", "wb"); fm = fopen("image.mono.mif", "wb"); if (f) { t_bmp_header header; t_bmp_info info; fread(&header, 14, 1, f); /* sizeof(t_bmp_header) returns 16 instead of 14. Should be 14. */ fread(&info, sizeof(t_bmp_info), 1, f); #if !defined (WIN32) header.bfSize = FLIP_INT(header.bfSize); header.bfType = FLIP_SHORT(header.bfType); header.offset = FLIP_INT(header.offset); header.reserved1 = FLIP_SHORT(header.reserved1); header.reserved2 = FLIP_SHORT(header.reserved2); info.biSize = FLIP_INT(info.biSize); info.biWidth = FLIP_INT(info.biWidth); info.biHeight = FLIP_INT(info.biHeight); info.biPlanes = FLIP_SHORT(info.biPlanes); info.biBitCount = FLIP_SHORT(info.biBitCount); info.biCompression = FLIP_INT(info.biCompression); info.biSizeImage = FLIP_INT(info.biSizeImage); info.biXPelsPerMeter = FLIP_INT(info.biXPelsPerMeter); info.biYPelsPerMeter = FLIP_INT(info.biYPelsPerMeter); info.biClrUsed = FLIP_INT(info.biClrUsed); info.biClrImportant = FLIP_INT(info.biClrImportant); #endif printf("Input file is %ix%i %i-bit depth\n", info.biWidth, info.biHeight, info.biBitCount); if (info.biBitCount == 24) { char temp[100]; width = info.biWidth; height = info.biHeight; printf("Converting...\n"); sprintf(temp, "Depth = %i;\r\n",width*height); faprint(fcol, fm, temp); fprintf(fcol, "Width = 3;\r\n"); fprintf(fm, "Width = 1;\r\n"); faprint(fcol, fm, "Address_radix=dec;\r\n"); faprint(fcol, fm, "Data_radix=bin;\r\n"); faprint(fcol, fm, "Content\r\n"); faprint(fcol, fm, "BEGIN\r\n"); sprintf(temp, "\t[0..%i] : 000;\r\n", width*height - 1); fprintf(fcol, temp); sprintf(temp, "\t[0..%i] : 0;\r\n", width*height - 1); fprintf(fm, temp); fseek(f, 54, SEEK_SET); for(y=height-1; y >=0; y--) { x = 0; fprintf(fcol, "\t%i :", y*width+x); fprintf(fm, "\t%i :", y*width+x); for(x=0; x 0) && ((x % 40) == 0)) { fprintf(fcol, ";\r\n\t%i :", y*width + x); fprintf(fm, ";\r\n\t%i :", y*width + x); } #if defined (WIN32) c = ((c >> 24) & 0x000000FF) | ((c & 0x00FF0000) >> 8) | ((c & 0x0000FF00) << 8) | ((c & 0x000000FF) <>= 8; b = (c & 0xFF0000) >> 16; g = (c & 0x00FF00) >> 8; r = (c & 0x0000FF); c = r + g + b; c /= 3; r = (r >= 128 ? 1 : 0); g = (g >= 128 ? 1 : 0); b = (b >= 128 ? 1 : 0); c = (c >= 128 ? 1 : 0); fprintf(fcol, " %i%i%i", r, g, b); fprintf(fm, " %i", c); } faprint(fcol, fm, ";\r\n"); if ((x*3) % 4 != 0) { fread(&c, 4-((x*3) % 4), 1, f); } } faprint(fcol, fm, "End;\r\n"); } else printf("Input file image.bmp is not in a 24-bit colour format!\n"); fclose(fm); fclose(fcol); fclose(f); printf("All done.\n"); } else printf("Cannot open input file. Check for input.bmp\n"); }
BMP是一种常见的位图图片格式,而MIF是一种用于描述存储器初始化数据的文件格式。将一个BMP图像换成MIF文件,可以使该图像能够被存储到FPGA等硬件设备中。 Python是一种功能强大的编程语言,它提供了许多库和工具,可以帮助我们完成这个任务。下面是一个大致的步骤来实现这个换过程: 1. 导入必要的库,比如PIL库(Python Imaging Library)。 2. 读取BMP图像文件,可以使用PIL库的`Image.open()`函数。 3. 将图像换成灰度图像,可以使用PIL库的`convert()`函数。 4. 获取图像的宽度和高度,可以使用PIL库的`size`属性。 5. 创建MIF文件,并写入必要的MIF文件头和格式。 6. 遍历图像的每个像素,并将其换为MIF文件中的格式。对于每个像素,可以将其灰度值换为二进制并写入MIF文件。 7. 关闭MIF文件。 下面是一个简单的示例代码: ```python from PIL import Image # 读取BMP图像文件 image = Image.open('input.bmp') # 将图像换成灰度图像 gray_image = image.convert('L') # 获取图像的宽度和高度 width, height = gray_image.size # 创建MIF文件 mif_file = open('output.mif', 'w') # 写入MIF文件头和格式 mif_file.write('DEPTH = %d;\n' % width*height) mif_file.write('WIDTH = 8;\n') mif_file.write('ADDRESS_RADIX = HEX;\n') mif_file.write('DATA_RADIX = BIN;\n') mif_file.write('CONTENT\n') mif_file.write('BEGIN\n') # 遍历图像的每个像素,并将其换为MIF文件中的格式 for y in range(height): for x in range(width): pixel_value = gray_image.getpixel((x, y)) binary_value = bin(pixel_value)[2:].zfill(8) mif_file.write('%04X : %s;\n' % (y*width + x, binary_value)) # 关闭MIF文件 mif_file.write('END;\n') mif_file.close() ``` 这段代码中,我们首先使用PIL库打开BMP图像文件,并将其换成灰度图像。然后,我们获取图像的宽度和高度,并创建一个新的MIF文件。接下来,我们遍历图像的每个像素,将其换为二进制格式,并写入MIF文件中。最后,我们关闭MIF文件。运行这段代码后,将会生成一个名为"output.mif"的MIF文件,其中包含了用于初始化存储器的数据。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pose_Ma

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值