照片转换

from tkinter import *                                                               #导入tkinter
from tkinter import messagebox                                                      #引入弹出框
#from typing import Any, Union
import pymysql                                                                       #导入pymysql
from pymysql.cursors import Cursor                                                  #导入游标
from hashlib import sha1                                                            #导入hashlib,引用函数
import tkinter as tk
from PIL import Image                                                                #图像缩放
from tkinter import Scrollbar
from tkinter import ttk                                                              # 导入内部包
from tkinter.ttk import Treeview

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("1.gif", 150, 250).change_aspect_rate().past_background().save_result(
        "515.gif")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值