用excel画一个像素画-matlab

clc;
clear;
a=imread('C:\Users\csh_g\Pictures\Saved Pictures\text.jpg');
a1=a(:,:,1);
a2=a(:,:,2);
a3=a(:,:,3);
% a1=round(rand(533,800)*254)+1 ;
% a2=round(rand(533,800)*254)+1 ;
% a3=round(rand(533,800)*254)+1 ;
hExcel = actxserver('excel.application');   % 创建一个excel实例对象
hWorkbooks = hExcel.Workbooks;     % 创建一个活动工作本组对象
hWorkbook = hWorkbooks.invoke('Add');    % 增加一个工作本(簿)对象
hSheets = hExcel.ActiveWorkBook.Sheets;      % 获得当前工作本句柄
set(hExcel,'Visible',1);
hSheet2 = hSheets.Add;
hSheet2.Activate;
hSheet2.Cells.RowHeight = 10;%设置行高
hSheet2.Cells.ColumnWidth = 1.63;%设列宽,excel行宽和列高单位不同,绘制成方格需要换算一下
for i=1:533  %行,
    for j=1:800  %列 ,用列名\
    %-------------序号转算列序字符串
        n=j;
        count=0;
        temp='';
        while floor(n/26)>0   %取整
            yushu=mod(n,26);
            if yushu==0
               temp=strcat(char(26+64),temp); 
               n=floor(n/26)-1;
            else
                temp=strcat(char(yushu+64),temp);
                n=floor(n/26);
            end
        end
        if n==0
        else
            temp=strcat(char(n+64),temp);
        end
        range=strcat(temp,num2str(i));
        %--------------序号换算列序字符串,实际上是十进制转26进制
            R=a1(i,j);
            G=a2(i,j);
            B=a3(i,j);
            color=double(R)+256*double(G)+65536*double(B);%对应颜色
        hSheet2.Range(range).Interior.Color=color;         
    end
end

猜你喜欢

转载自blog.csdn.net/qq_40821274/article/details/89785484