一张图片我能用Python玩出花,你信吗?

2023-10-17 16:20

本文主要是介绍一张图片我能用Python玩出花,你信吗?,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.利用python绘制一个小猪佩奇

turtle库是一个很好的python图形绘制库,利用它我们可以绘制各种各样的图形、小动物。这个库其实并不难,实际你怎么绘制这个图形,对应的代码,就跟着你的实际绘制图形的方向走下去,即可。

from turtle import *# 绘制鼻子
def nose(x,y):penup()goto(x,y)pendown()setheading(-30)begin_fill()a=0.4for i in range(120): if 0<=i<30 or 60<=i<90:a=a+0.08 left(3) forward(a) else:a=a-0.08 left(3)forward(a)end_fill()penup()setheading(90)forward(25)setheading(0)forward(10)pendown()pencolor(255,155,192)setheading(10)begin_fill()circle(5)color(160,82,45)end_fill()penup()setheading(0)forward(20)pendown()pencolor(255,155,192)setheading(10)begin_fill()circle(5)color(160,82,45)end_fill() # 绘制头部
def head(x,y):color((255,155,192),"pink")penup()goto(x,y)setheading(0)pendown()begin_fill()setheading(180)circle(300,-30)circle(100,-60)circle(80,-100)circle(150,-20)circle(60,-95)setheading(161)circle(-300,15)penup()goto(-100,100)pendown()setheading(-30)a=0.4for i in range(60): if 0<=i<30 or 60<=i<90:a=a+0.08 left(3) forward(a) else:a=a-0.08 left(3)forward(a)end_fill() # 绘制耳朵
def ears(x,y): color((255,155,192),"pink")penup()goto(x,y)pendown()begin_fill()setheading(100)circle(-50,50)circle(-10,120)circle(-50,54)end_fill()penup()setheading(90)forward(-12)setheading(0)forward(30)pendown()begin_fill()setheading(100)circle(-50,50)circle(-10,120)circle(-50,56)end_fill() # 绘制眼睛
def eyes(x,y):color((255,155,192),"white")penup()setheading(90)forward(-20)setheading(0)forward(-95)pendown()begin_fill()circle(15)end_fill()color("black")penup()setheading(90)forward(12)setheading(0)forward(-3)pendown()begin_fill()circle(3)end_fill()color((255,155,192),"white")penup()setheading(90)forward(-25)setheading(0)forward(40)pendown()begin_fill()circle(15)end_fill()color("black")penup()setheading(90)forward(12)setheading(0)forward(-3)pendown()begin_fill()circle(3)end_fill() # 绘制腮帮
def cheek(x,y):color((255,155,192))penup()goto(x,y)pendown()setheading(0)begin_fill()circle(30)end_fill() # 绘制嘴巴
def mouth(x,y): color(239,69,19)penup()goto(x,y)pendown()setheading(-80)circle(30,40)circle(40,80) # 绘制身体
def body(x,y):color("red",(255,99,71))penup()goto(x,y)pendown()begin_fill()setheading(-130)circle(100,10)circle(300,30)setheading(0)forward(230)setheading(90)circle(300,30)circle(100,3)color((255,155,192),(255,100,100))setheading(-135)circle(-80,63)circle(-150,24)end_fill() # 绘制手
def hands(x,y):color((255,155,192))penup()goto(x,y)pendown()setheading(-160)circle(300,15)penup()setheading(90)forward(15)setheading(0)forward(0)pendown()setheading(-10)circle(-20,90)penup()setheading(90)forward(30)setheading(0)forward(237)pendown()setheading(-20)circle(-300,15)penup()setheading(90)forward(20)setheading(0)forward(0)pendown()setheading(-170)circle(20,90) # 绘制脚
def foot(x,y):pensize(10)color((240,128,128))penup()goto(x,y)pendown()setheading(-90)forward(40)setheading(-180)color("black")pensize(15)forward(20)pensize(10)color((240,128,128))penup()setheading(90)forward(40)setheading(0)forward(90)pendown()setheading(-90)forward(40)setheading(-180)color("black")pensize(15)forward(20) # 绘制尾巴
def tail(x,y):pensize(4)color((255,155,192))penup()goto(x,y)pendown()setheading(0)circle(70,20)circle(10,330)circle(70,30) # 设置画布和画笔
def setting(): pensize(4)hideturtle()colormode(255)color((255,155,192),"pink")setup(840,500)speed(10) def main():setting() # 画布和画笔设置nose(-100,100)    # 鼻子head(-69,167)     # 头ears(0,160)       # 耳朵eyes(0,140)       # 眼睛cheek(80,10)      # 腮帮mouth(-20,30)     # 嘴巴body(-32,-8)      # 身体hands(-56,-45)    # 手foot(2,-177)      # 脚tail(148,-155)    # 尾巴done()            # 结束
if __name__ == '__main__':main()

结果如下:

2.利用python给小猪佩奇换背景色

换背景色的原理:每一个图像都是由像素点构成的,我们想要替换他们的颜色,就是找到每个像素点对应的位置,然后用指定颜色,去替换它!一般证件照背景色并不是同一种蓝色像素点,无法完成像素点的定位,这就需要我们对图像进行【腐蚀】或【膨胀】,完成图片黑白话,这样白色的像素点是255,就可以很好的定位了。


在学习过程中有什么不懂得可以加我的
python学习qun,855408893
群里有不错的学习视频教程、开发工具与电子书籍。  
与你分享python企业当下人才需求及怎么从零基础学习好python,和学习什么
mport cv2 import numpy as np # 读取照片
img=cv2.imread('zhu.jpg') # 图像缩放
img = cv2.resize(img,None,fx=0.5,fy=0.5)
rows,cols,channels = img.shape print(rows,cols,channels)
cv2.imshow('img',img) # 图片转换为灰度图
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
cv2.imshow('hsv',hsv) # 图片的二值化处理
lower_blue=np.array([90,70,70])
upper_blue=np.array([110,255,255])
mask = cv2.inRange(hsv, lower_blue, upper_blue) #腐蚀膨胀
erode=cv2.erode(mask,None,iterations=1)
cv2.imshow('erode',erode)dilate=cv2.dilate(erode,None,iterations=1)
cv2.imshow('dilate',dilate) #遍历替换
for i in range(rows): for j in range(cols): if erode[i,j]==255: # 像素点为255表示的是白色,我们就是要将白色处的像素点,替换为红色img[i,j]=(0,0,255)#此处替换颜色,为BGR通道
cv2.imshow('res',img) # 窗口等待的命令,0表示无限等待
cv2.waitKey(0)

结果如下:

3.利用python将小猪佩奇切分为九宫格

将图片切分为九宫格的原理就是:找到图片对应位置的坐标,然后进行切割。由于是九宫格,我们切分的是3*3,然后利用双层循环遍历对应位置的坐标后,进行图片切割

from PIL import Image import sys #将图片填充为正方形
def fill_image(image):width, height = image.size #选取长和宽中较大值作为新图片的,小的地方,用图片填充为等宽等高new_image_length = width if width > height else height #生成新图片[白底]new_image = Image.new(image.mode, (new_image_length, new_image_length), color='white') #将之前的图粘贴在新图上,居中if width > height:#原图宽大于高,则填充图片的竖直维度#(x,y)二元组表示粘贴上图相对下图的起始位置new_image.paste(image, (0, int((new_image_length - height) / 2))) else:new_image.paste(image, (int((new_image_length - width) / 2),0)) return new_image #切图
def cut_image(image):width, height = image.sizeitem_width = int(width / 3)item_height = int(height / 3)box_list = [] #双重循环,生成9张图片基于原图的位置# 注意:图片左上角是(0,0),右下角是(width,height)for i in range(0,3): for j in range(0,3): print((j*item_width,i*item_height,(j+1)*item_width,(i+1)*item_height))box = (j*item_width,i*item_height,(j+1)*item_width,(i+1)*item_height)box_list.append(box)image_list = [image.crop(box) for box in box_list] return image_list #保存
def save_images(image_list):index = 1for image in image_list:image.save(str(index) + '.jpg')index += 1 file_path = "zhuzhu.jpg" image = Image.open(file_path)
image = fill_image(image)
image_list = cut_image(image)
save_images(image_list)

结果如下:

4.利用python制作小猪佩奇动态二维码

代码说明:如果我们利用的背景图是gif动态图,生成的就是动态二维码。如果利用的背景是静态图,生成的是静态二维码。


在学习过程中有什么不懂得可以加我的
python学习qun,855408893
群里有不错的学习视频教程、开发工具与电子书籍。  
与你分享python企业当下人才需求及怎么从零基础学习好python,和学习什么
from MyQR import myqr # 生成的二维码最终在你电脑的存储位置 # 当你使用了动态图作为背景,这里可以写成".gif",保存出来的就是gif动态二维码!
save_name = r'C:\Users\Administrator\Desktop\fdasfa\D.gif' myqr.run(words='https://jq.qq.com/?_wv=1027&k=aKS84NJF',version=10,  # 容错率level='H',  # 纠错水平,范围是L、M、Q、H,从左到右依次升高colorized=True, # False为黑白contrast=1.5,  # 用以调节图片的对比度,1.0 表示原始图片。brightness=1.0,  # 用来调节图片的亮度。save_name=save_name, #存储的文件名# 背景图片的路径,你如果给的是".png/.jpg"等静态图片,最终生成的就是静态二维码!# 背景图片的路径,你如果给的是".gif"等动态图片,最终只需要保存为".gif",生成的就是动态二维码!picture=r'C:\Users\Administrator\Desktop\fdasfa\123.gif' )

image

这篇关于一张图片我能用Python玩出花,你信吗?的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/226548

相关文章

python panda库从基础到高级操作分析

《pythonpanda库从基础到高级操作分析》本文介绍了Pandas库的核心功能,包括处理结构化数据的Series和DataFrame数据结构,数据读取、清洗、分组聚合、合并、时间序列分析及大数据... 目录1. Pandas 概述2. 基本操作:数据读取与查看3. 索引操作:精准定位数据4. Group

Python pandas库自学超详细教程

《Pythonpandas库自学超详细教程》文章介绍了Pandas库的基本功能、安装方法及核心操作,涵盖数据导入(CSV/Excel等)、数据结构(Series、DataFrame)、数据清洗、转换... 目录一、什么是Pandas库(1)、Pandas 应用(2)、Pandas 功能(3)、数据结构二、安

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

Python安装Pandas库的两种方法

《Python安装Pandas库的两种方法》本文介绍了三种安装PythonPandas库的方法,通过cmd命令行安装并解决版本冲突,手动下载whl文件安装,更换国内镜像源加速下载,最后建议用pipli... 目录方法一:cmd命令行执行pip install pandas方法二:找到pandas下载库,然后

Python实现网格交易策略的过程

《Python实现网格交易策略的过程》本文讲解Python网格交易策略,利用ccxt获取加密货币数据及backtrader回测,通过设定网格节点,低买高卖获利,适合震荡行情,下面跟我一起看看我们的第一... 网格交易是一种经典的量化交易策略,其核心思想是在价格上下预设多个“网格”,当价格触发特定网格时执行买

Python标准库之数据压缩和存档的应用详解

《Python标准库之数据压缩和存档的应用详解》在数据处理与存储领域,压缩和存档是提升效率的关键技术,Python标准库提供了一套完整的工具链,下面小编就来和大家简单介绍一下吧... 目录一、核心模块架构与设计哲学二、关键模块深度解析1.tarfile:专业级归档工具2.zipfile:跨平台归档首选3.

使用Python构建智能BAT文件生成器的完美解决方案

《使用Python构建智能BAT文件生成器的完美解决方案》这篇文章主要为大家详细介绍了如何使用wxPython构建一个智能的BAT文件生成器,它不仅能够为Python脚本生成启动脚本,还提供了完整的文... 目录引言运行效果图项目背景与需求分析核心需求技术选型核心功能实现1. 数据库设计2. 界面布局设计3

Python进行JSON和Excel文件转换处理指南

《Python进行JSON和Excel文件转换处理指南》在数据交换与系统集成中,JSON与Excel是两种极为常见的数据格式,本文将介绍如何使用Python实现将JSON转换为格式化的Excel文件,... 目录将 jsON 导入为格式化 Excel将 Excel 导出为结构化 JSON处理嵌套 JSON:

Python操作PDF文档的主流库使用指南

《Python操作PDF文档的主流库使用指南》PDF因其跨平台、格式固定的特性成为文档交换的标准,然而,由于其复杂的内部结构,程序化操作PDF一直是个挑战,本文主要为大家整理了Python操作PD... 目录一、 基础操作1.PyPDF2 (及其继任者 pypdf)2.PyMuPDF / fitz3.Fre

python设置环境变量路径实现过程

《python设置环境变量路径实现过程》本文介绍设置Python路径的多种方法:临时设置(Windows用`set`,Linux/macOS用`export`)、永久设置(系统属性或shell配置文件... 目录设置python路径的方法临时设置环境变量(适用于当前会话)永久设置环境变量(Windows系统