Python&Matlab实现炫酷的3D旋转图
目录
前言1.Python爬取美女照片1.1 留恋忘返的网址1.2 Python代码1.3 结果2.Python实现2.1 条件准备2.2 运行展示2.3 Python实现3.Matlab实现3.1 运行展示3.2 Matlab代码前言
我们今天的任务很明确,我先系统梳理一下:
1.先用Python爬取一波漂亮的美女照片;
2.然后Python中炫酷的代码实现;
3.最后用matlab伺候,得到相同的结果。
1.Python爬取美女照片
1.1 留恋忘返的网址
站长素材-分享综合设计素材的平台 (chinaz.com)
1.2 Python代码
#~~~~欢迎关注公众号:电力系统与算法之美~~~~~~~~~~~~· #~~~~~~~~~导入相关库~~~~~~~~~~~~~~~~~~~~· import urllib.request from lxml import etree #~~~~~~~~~1.请求对象的定制~~~~~~~~~~~~~~~~~ def create_request(page): if (page == 1): url = "https://sc.chinaz.com/tag_tupian/YaZhouMeiNv.html" else: url = "https://sc.chinaz.com/tag_tupian/yazhoumeinv_" + str(page) + ".html" # print(url) headers = { "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36" } request = urllib.request.Request(url=url, headers=headers) return request #~~~~~~~~~~~2.获取网页的源码~~~~~~~~~~~~~~~~~~~~~ def get_content(request): response = urllib.request.urlopen(request) content = response.read().decode("utf-8") return content #~~~~~~~~~~~~~~~~~~~3.下载图片~~~~~~~~~~~~~~~~~~~~~~~~~~ def down_img(content): # 下载文件格式:urllib.request.urlretrieve("图片地址","文件的名字") tree = etree.HTML(content) name_list = tree.xpath("//div[@id = "container"]//a/img/@alt") # 一般涉及到图片的网站,都会进行懒加载,要把src换成src2(懒加载时,src会以src2出现) src_list = tree.xpath("//div[@id = "container"]//a/img/@src2") # print(len(name_list)) # print(len(src_list)) for i in range(len(name_list)): name = name_list[i] src = src_list[i] url = "https:" + src url = url.replace("_s", "") urllib.request.urlretrieve(url=url, filename="./meinv/" + name + ".jpg") #~~~~~~~~~运行~~~~~~~~~~~~~~~ if __name__ == "__main__": start_page = int(input("请输入起始页码:")) end_page = int(input("请输入终止页码:")) for page in range(start_page, end_page + 1): #~~~~1.定制请求对象~~~~~ request = create_request(page) #~~~~2.获取网页源码~~~~~ content = get_content(request) #~~~~~3.解析源码并下载图片~~~ down_img(content)
1.3 结果
温馨提示:meinv这个文件夹是提前建立的。
2.Python实现
2.1 条件准备
由1中爬取的照片,我们就可以为接下来的事做准备啦。选取十二张照片,如图:
2.2 运行展示
运行出来比下面这个还炫酷:
2.3 Python实现
CSS3制作3D图片立方体旋转特效
3.Matlab实现
3.1 运行展示
动态视频:
3.2 Matlab代码
%====欢迎关注关注号:电力系统与算法之美 function wlz3d path=".\meinv\";%文件夹名称 files=dir(fullfile(path,"*.jpg")); picNum=size(files,1); %% 遍历路径下每一幅图像 for i=1:picNum fileName=strcat(path,files(i).name); img=imread(fileName); img=imresize(img,[120,120]); imgSet{i}=img; end %% fig axes设置 fig=figure("units","pixels","position",[50 50 600 600],... "Numbertitle","off","resize","off",... "name","album3d","menubar","none"); ax=axes("parent",fig,"position",[-0.5 -0.5 2 2],... "XLim", [-6 6],... "YLim", [-6 6],... "ZLim", [-6 6],... "Visible","on",... "XTick",[], ... "YTick",[],... "Color",[0 0 0],... "DataAspectRatioMode","manual",... "CameraPositionMode","manual"); hold(ax,"on") ax.CameraPosition=[5 5 5]; %% 用于绘制图片的网格 [XMesh,YMesh]=meshgrid(linspace(-1,1,120),linspace(-1,1,120)); ZMesh=ones(120,120); %% 绘制图片立方体 surfPic(1)=surf(XMesh,YMesh,ZMesh,"CData",imgSet{mod(0,picNum)+1},"EdgeColor","none","FaceColor","interp"); surfPic(2)=surf(XMesh,YMesh(end:-1:1,:),-ZMesh,"CData",imgSet{mod(1,picNum)+1},"EdgeColor","none","FaceColor","interp"); surfPic(3)=surf(ZMesh,XMesh,YMesh(end:-1:1,:),"CData",imgSet{mod(2,picNum)+1},"EdgeColor","none","FaceColor","interp"); surfPic(4)=surf(XMesh,ZMesh,YMesh(end:-1:1,:),"CData",imgSet{mod(3,picNum)+1},"EdgeColor","none","FaceColor","interp"); surfPic(5)=surf(-ZMesh,XMesh,YMesh(end:-1:1,:),"CData",imgSet{mod(4,picNum)+1},"EdgeColor","none","FaceColor","interp"); surfPic(6)=surf(XMesh,-ZMesh,YMesh(end:-1:1,:),"CData",imgSet{mod(5,picNum)+1},"EdgeColor","none","FaceColor","interp"); %% 依靠小立方体数据绘制中等立方体 for i=1:6 surfPicA(i)=surf(surfPic(i).XData.*1.5,surfPic(i).YData.*1.5,surfPic(i).ZData.*1.5,... "CData",surfPic(i).CData,"EdgeColor","none","FaceColor","interp","FaceAlpha",0.7); end %% 用来调整放大比例的矩阵 resizeMat=[2 2 2.5;2 2 2.5;2.5 2 2; 2 2.5 2;2.5 2 2;2 2.5 2]; %% 最大图片绘制 % for i=1:6 % surfPicB(i)=surf(surfPic(i).XData.*resizeMat(i,1),... % surfPic(i).YData.*resizeMat(i,2),... % surfPic(i).ZData.*resizeMat(i,3),... % "CData",surfPic(i).CData,"EdgeColor",... % "none","FaceColor","interp","FaceAlpha",0.7); % end lastDis=300; preDis=300; set(fig,"WindowButtonMotionFcn",@move2center) function move2center(~,~) xy=get(fig,"CurrentPoint"); preDis=sqrt(sum((xy-[300,300]).^2)); end fps=40;theta=0; rotateTimer=timer("ExecutionMode", "FixedRate", "Period",1/fps, "TimerFcn", @rotateCube); start(rotateTimer) function rotateCube(~,~) theta=theta+0.02; ax.CameraPosition=[cos(theta)*5*sqrt(2),sin(theta)*5*sqrt(2),5]; if (~all([preDis lastDis]<150))&&any([preDis lastDis]<150) for ii=1:6 if preDis<150 surfPicA(ii).XData=surfPic(ii).XData.*resizeMat(ii,1); surfPicA(ii).YData=surfPic(ii).YData.*resizeMat(ii,2); surfPicA(ii).ZData=surfPic(ii).ZData.*resizeMat(ii,3); else surfPicA(ii).XData=surfPic(ii).XData.*1.5; surfPicA(ii).YData=surfPic(ii).YData.*1.5; surfPicA(ii).ZData=surfPic(ii).ZData.*1.5; end end end lastDis=preDis; end % 弃用方案:太卡 % set(fig,"WindowButtonMotionFcn",@move2center) % function move2center(~,~) % xy=get(fig,"CurrentPoint"); % dis=sum((xy-[300,300]).^2); % for ii=1:6 % if dis<200 % surfPicA(ii).XData=surfPic(ii).XData.*resizeMat(ii,1); % surfPicA(ii).YData=surfPic(ii).YData.*resizeMat(ii,2); % surfPicA(ii).ZData=surfPic(ii).ZData.*resizeMat(ii,3); % else % surfPicA(ii).XData=surfPic(ii).XData; % surfPicA(ii).YData=surfPic(ii).YData; % surfPicA(ii).ZData=surfPic(ii).ZData; % end % end % % % % end end
到此这篇关于Python&Matlab实现炫酷的3D旋转图的文章就介绍到这了,更多相关Python Matlab3D旋转图内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
X 关闭
X 关闭
- 15G资费不大降!三大运营商谁提供的5G网速最快?中国信通院给出答案
- 2联想拯救者Y70发布最新预告:售价2970元起 迄今最便宜的骁龙8+旗舰
- 3亚马逊开始大规模推广掌纹支付技术 顾客可使用“挥手付”结账
- 4现代和起亚上半年出口20万辆新能源汽车同比增长30.6%
- 5如何让居民5分钟使用到各种设施?沙特“线性城市”来了
- 6AMD实现连续8个季度的增长 季度营收首次突破60亿美元利润更是翻倍
- 7转转集团发布2022年二季度手机行情报告:二手市场“飘香”
- 8充电宝100Wh等于多少毫安?铁路旅客禁止、限制携带和托运物品目录
- 9好消息!京东与腾讯续签三年战略合作协议 加强技术创新与供应链服务
- 10名创优品拟通过香港IPO全球发售4100万股 全球发售所得款项有什么用处?