本文主要是介绍使用matlab绘制春联,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
受到了 天元浪子 老师的启发
写了一个类似的绘制春联的程序
已给老师投票,希望老师不要打我
天元老师的博文:
用Python写春联:抒写最真诚的祝福和最美好的祈愿
——————————————————————————
matlab绘制春联
效果:
使用方法:
完整代码:
function SpringScrolls(stringName,type,fontSize)
%stringName is the parameter matrix of string.
%the type can be 'rows' or 'cols'.
warning off
string=stringName;
if nargin<2type='rows';
end
switch 1case ~isempty(regexpi(string,',')),pause_location=regexpi(string,',');case ~isempty(regexpi(string,',')),pause_location=regexpi(string,',');case ~isempty(regexpi(string,' ')),pause_location=regexpi(string,' ');
end
len_string=length(string);
if nargin<3if strcmp(type,'rows'),fontSize=25+0.002*30/max(2/1080,max(pause_location-1,len_string-pause_location)/1920);endif strcmp(type,'cols'),fontSize=20+0.002*30/max(2/1920,max(pause_location-1,len_string-pause_location)/1080);end
end
disp(fontSize);
BG=[];BG_cell=imread('背景图.jpg');
[m,n,~]=size(BG_cell);
for i=1:2for j=1:max(pause_location-1,len_string-pause_location)if strcmp(type,'rows'),BG((i-1)*(m+10)+1:(i-1)*(m+10)+m,(j-1)*n+1:j*n,:)=BG_cell(:,:,:);endif strcmp(type,'cols'),BG((j-1)*n+1:j*n,(i-1)*(m+10)+1:(i-1)*(m+10)+m,:)=BG_cell(:,:,:);endend
end
if strcmp(type,'rows'),BG(m+1:m+10,1:j*n,:)=0.94*255*ones(10,j*n,3);end
if strcmp(type,'cols'),BG(1:j*n,m+1:m+10,:)=0.94*255*ones(j*n,10,3);end
BG=uint8(BG);
imshow(BG)
hold on;
for k=1:pause_location-1if strcmp(type,'rows'),text((k-1)*m+m/2,n/2,string(k),'fontsize',fontSize,'FontName','华文行楷','HorizontalAlignment', 'center');endif strcmp(type,'cols'),text(n/2,(k-1)*m+m/2,string(k),'fontsize',fontSize,'FontName','华文行楷','HorizontalAlignment', 'center');end
end
for k=1:len_string-pause_locationif strcmp(type,'rows'),text((k-1)*m+m/2,n+10+n/2,string(pause_location+k),'fontsize',fontSize,'FontName','华文行楷','HorizontalAlignment', 'center');endif strcmp(type,'cols'),text(n+10+n/2,(k-1)*m+m/2,string(pause_location+k),'fontsize',fontSize,'FontName','华文行楷','HorizontalAlignment', 'center');end
end
get(text)
end
注:该程序的m文件应和下图放在同一文件夹中,并将图片命名为“背景图.jpg”
这篇关于使用matlab绘制春联的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!