python画超长图-python实现按长宽比缩放图片
使用python按图片固定长宽比缩放图片到指定图片大小,空白部分填充为黑色。
代码
# -*- coding: utf-8 -*-
from PIL import Image
class image_aspect():
def __init__(self, image_file, aspect_width, aspect_height):
self.img = Image.open(image_file)
self.aspect_width = aspect_width
self.aspect_height = aspect_height
self.result_image = None
def change_aspect_rate(self):
img_width = self.img.size[0]
img_height = self.img.size[1]
if (img_width / img_height) > (self.aspect_width / self.aspect_height):
rate = self.aspect_width / img_width
else:
rate = self.aspect_height / img_height
rate = round(rate, 1)
print(rate)
self.img = self.img.resize((int(img_width * rate), int(img_height * rate)))
return self
def past_background(self):
self.result_image = Image.new("RGB", [self.aspect_width, self.aspect_height], (0, 0, 0, 255))
self.result_image.paste(self.img, (int((self.aspect_width - self.img.size[0]) / 2), int((self.aspect_height - self.img.size[1]) / 2)))
return self
def save_result(self, file_name):
self.result_image.save(file_name)
if __name__ == "__main__":
image_aspect("./source/test.jpg", 1920, 1080).change_aspect_rate().past_background().save_result("./target/test.jpg")
感言
有兴趣的朋友可以将图片路径,长宽值,背景颜色等参数化
封装成api做为个公共服务
本文源码下载
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
python画超长图-python实现按长宽比缩放图片相关推荐
- python画超长图-python 画长图
广告关闭 腾讯云双11爆品提前享,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高满返5000元! 最近遇到个任务需要画一些坐标图,我就在想,用了这么长时间的 python 了,能不 ...
- python画超长图-python—networkx:求图的平均路径长度并画出直方图
要绘制一个动态网络,到处找资料,收集相关的networkx绘图资料,计算路径的代码如下: NetworkX Examples-BASIC--properties #!/usr/bin/env pyth ...
- python画超长图-Python 拼接多张尺寸大小不一样的图片制作长图
from os import listdir from PIL import Image # 获取当前文件夹下所以图片 ims = [Image.open('../images/%s' % fn) f ...
- python画超长图-利用Python画图,千变万化,各种画图技巧!
如图所示,利用Python的turtle画了一个美国队长盾牌的标志: # 所需依赖:python3 sublime Python代码: # print 打印 print('hello world!') ...
- python画折线图-python如何画折线图
python画折线图利用的是matplotlib.pyplot.plot的工具来绘制折线图,这里先给出一个段代码和结果图:# -*- coding: UTF-8 -*- import numpy as ...
- 如何使用python画折线图-Python 使用 matplotlib 画折线图教程
话不多说,直接上代码.最近修改一篇论文,实验部分的图全部重画了一下,也正好在此进行整理.使用 Python 的 matplotlib 库来画图还是比较方便的,今天整理一下用 matplotlib 画折 ...
- python画雷达图-Python 详解雷达图/蛛网图
雷达图-pyecharts 蛛网图,最早知道是在玩FIFA游戏的时候,球员的能力用蛛网图来表示与比较,那时觉得非常新鲜.后来,在实际的工作中,其实很少用到:一方面,直接提供蛛网图的工具少:另一方面,过 ...
- python画折线图-python绘制简单折线图代码示例
1.画最简单的直线图 代码如下: import numpy as np import matplotlib.pyplot as plt x=[0,1] y=[0,1] plt.figure() plt ...
- python画双折线图-python双折线图
广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! practice.png双y轴折线图(plot both of those pl ...
最新文章
- python绘制繁花曲线代码_使用python和pygame绘制繁花曲线的方法
- 字典添加数据_【Python基础学习】4. 数据类型之字典及其操作
- HDU 5834 Magic boy Bi Luo with his excited tree 树形DP
- oracle11gr2克隆安装,克隆安装Oracle 11G HOME
- 高质量程序设计指南--笔记
- loic 工具使用指导
- 生信技能树——GEO芯片数据的合并
- Linux系统CentOS 7中安装配置JDK
- cad剖切线的快捷键_CAD有哪些常用的快捷键?
- matlab单点弦截法求解,弦截法求根c语言
- 网页html教学反思,教学反思怎么写
- Tapestry(二):Tapestry基本知识
- Qt 学习(三) —— Qt 模块
- 好用看得见!深度操作系统Deepin装机使用体验
- str_replace替换多个字符串
- Elasticsearch学习笔记:MUST_NOT not working with EXIST in NESTED query
- spark.read.option参数
- nested exception is java.lang.NoClassDefFoundError:org/sprin
- Windows 11答疑:大家最关心的10个问题
- Hipo-iPod 处理对象
热门文章
- [CTS2019]无处安放(提交答案)
- null与undefined的异同点
- 读书笔记 摘自:《为什么精英都是时间控》
- Cirium分析:航空公司和机场重返准点率竞赛
- GPU架构与管线总结
- 理工附中2021年高考成绩查询,2021年江苏理工学院录取结果查询,附查询网址入口...
- 使用xpath解析爬取链家
- 计算机开机出现代码卡顿,电脑开机后很卡怎么办
- 通俗易懂理解几何光学(六)光学系统的像质评价
- 要多大内存才满足_什么是延迟满足能力?“延迟满足”能力对孩子有多重要家长要清楚...