在 Python 中调整图像大小同时保持其纵横比
这篇 Python 文章的目的是解释我们如何在 Python 中调整图像大小同时保持其纵横比。 在 Python 中调整图像大小的方法还将通过适当的示例程序描述其用法。
在 Python 中调整图像大小同时保持其纵横比
在 Python 中,您可以借助一些预定义的包来调整图像的大小。 通过导入这些包并使用重要的函数和方法,您可以在 Python 中调整图像的大小而不会丢失其纵横比。
使用 Python 图像库 (PIL)
Python 的成像库 PIL 是一个用于成像的 Python 库。
脚本的目的是自动化面向系统的任务。 脚本是您为实际任务编写代码而不需要编译的时候。
除了强大的图像处理能力外,PIL还能够处理多种图像格式(包括BMP、DIB、EPS、GIF、ICO、IM、MSP、PCX、PNG、PPM、SGI、TGA、TIFF、WebP、 XBM) 和图像模式,如 RGB、RGBA、B&W 和单色。
此外,PIL 与大多数操作系统兼容,包括 Windows、Linux 和 macOS。
Python 的标准 GUI 库是 Tkinter。 当 Python 与 Tkinter 结合时,可以快速轻松地创建 GUI 应用程序。
使用 Tkinter,您可以以面向对象的方式与 Tk GUI 工具包进行交互。 我们还从 Tkinter 导入 PIL。
假设已选择图片。 尺寸和原图如下。
示例代码:
# First, we have to import the modules here
from tkinter import *
from PIL import Image, ImageTk
# Now we are creating the object
root = Tk()
# Here, just reading the Image
image = Image.open("koala.png")
# Here now, using resize() method, resizing the image
resize_originalimage = image.resize((140, 80))
img_1 = ImageTk.PhotoImage(orgnlimg)
# creating, add resize image, and labeling it
label1 = Label(image=img_1)
label1.image = img_1
label1.pack()
#finally, executing the Tkinter
root.mainloop()
输出:
无论图像是显示在屏幕上还是存储在图像文件中,它都是由离散像素表示的。 当图像数据与屏幕表示的分辨率不同时,会出现混叠效果。
子采样通过平滑数据然后对平滑后的数据进行子采样来减少混叠。 在这种情况下,使用 ANTIALIAS。
Resampling.LANCZOS 是一种对采样数据进行插值以产生新值的方法。 该方法常用于多元插值,例如调整数字图像的大小。
原始图像大小为 284 x 606,一旦代码执行,我们也将能够区分原始图像和调整大小后的图像。
示例代码:
#Python Code to resize an image while maintaining the original aspect ratio, using the PIL Methods
import PIL
from PIL import Image
#setting the size of resized image
Imagewidth = 80
Imageheight=100
img_2 = Image.open('bear.jpg')
wpercent = (Imagewidth/float(img_2.size[0]))
hsize = int((float(img_2.size[1])*float(wpercent)))
#img = img.resize((mywidth,hsize), PIL.Image.ANTIALIAS)
img_2 = img_2.resize((Imagewidth,Imageheight), PIL.Image.Resampling.LANCZOS)
img_2.save('resizedImage.jpg')
img_2.show()
以下为原图。
输出:
当我们运行代码时,您还可以看到调整大小后的图像,该图像保存在名为 resizedImage.jpg 的目录中。
通过在 Python 中保持纵横比来裁剪图像
以下是 PIL 使用的 resize 和 new size 方法,裁剪图像而不是调整图像大小。
示例代码:
# Importing Image class from PIL module by maintaining its aspect ratio
from PIL import Image
# Opens an image in RGB mode
#For the external path, we use this :
#img_3 = Image.open(r"Image Path of the Original Image")
#For internal path
img_3 = Image.open("cat.png")
# The size of the image in pixels (size of the original image)
# (This is not mandatory)
width, height = img_3.size
# Setting the cropped image points
left = 4
top = height / 5
right = 154
bottom = 3 * height / 5
# Cropped image of the above dimension
# (It will not change the original image)
img_3 = img_3.crop((left, top, right, bottom))
newsize = (300, 300)
img_3 = img_3.resize(newsize)
#img_3=img_3.save(newsize)
# Shows the image in the image viewer
img_3.show()
输出:
由于我们将裁剪后的图像视为代码的输出,因此我们可以假设 PIL 不仅用于通过保持纵横比来调整图像大小,而且还用于裁剪。
在 Python 中,我们使用 Image.open 调整大小并使用 PIL 读取图像并保持其纵横比。 计算出新的宽高后,我们使用resize方法调整图片大小,并根据新的宽度用同样的方法保存新图片。
我们希望您发现本文有助于理解如何在 Python 中调整图像大小。
相关文章
Pandas DataFrame DataFrame.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:Python
-
DataFrame.shift() 函数是将 DataFrame 的索引按指定的周期数进行移位。
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
Pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:Python
-
Pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 DataFrame 中。
Pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:Python
-
本教程介绍了如何在 Pandas 中使用 DataFrame.merge()方法合并两个 DataFrames。
Pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:Python
-
本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串