mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-26 21:30:55 +08:00
commit
3f2a222f3e
@ -1,83 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Resize images using Python)
|
||||
[#]: via: (https://opensource.com/life/15/2/resize-images-python)
|
||||
[#]: author: (Dayo Ntwari https://opensource.com/users/dayontwari)
|
||||
|
||||
Resize images using Python
|
||||
======
|
||||
A quick explanation of how to resize images in Python while keeping the
|
||||
same aspect ratio.
|
||||
![Python in a tree][1]
|
||||
|
||||
I love [Python][2], and I've been learning it for a while now. Some time ago, I wrote a Python script where I needed to resize a bunch of images while at the same time keeping the aspect ratio (the proportions) intact. So I looked around and found [Pillow][3], a Python imaging library and "friendly fork" of an old library just called PIL.
|
||||
|
||||
To install Pillow, use the `pip` module of Python:
|
||||
|
||||
|
||||
```
|
||||
`$ python3 -m pip install Pillow`
|
||||
```
|
||||
|
||||
### Scaling by width
|
||||
|
||||
Here's a basic script to resize an image using the Pillow module:
|
||||
|
||||
|
||||
```
|
||||
from PIL import Image
|
||||
|
||||
basewidth = 300
|
||||
img = Image.open('fullsized_image.jpg')
|
||||
wpercent = (basewidth / float(img.size[0]))
|
||||
hsize = int((float(img.size[1]) * float(wpercent)))
|
||||
img = img.resize((basewidth, hsize), Image.ANTIALIAS)
|
||||
img.save('resized_image.jpg')
|
||||
```
|
||||
|
||||
These few lines of Python code resize an image (**fullsized_image.jpg**) using Pillow to a width of 300 pixels, which is set in the variable **basewidth** and a height proportional to the new width. The proportional height is calculated by determining what percentage 300 pixels is of the original width (**img.size[0]**) and then multiplying the original height (**img.size[1]**) by that percentage. The resulting height value is saved in the variable **hsize.**
|
||||
|
||||
You can change **basewidth** to any other number if you need a different width for your images. Also, notice I saved the resized image under a different name, **resized_image.jpg**, because I wanted to preserve the full-size image (**fullsized_image.jpg**) as well. You don't have to do this, of course. You can use the same filename to overwrite the full-size image with the resized image, if that is what you want.
|
||||
|
||||
### Scaling by height
|
||||
|
||||
If the height is fixed and the width proportionally variable, it's pretty much the same thing, you just need to switch things around a bit:
|
||||
|
||||
|
||||
```
|
||||
from PIL import Image
|
||||
|
||||
baseheight = 560
|
||||
img = Image.open('fullsized_image.jpg')
|
||||
hpercent = (baseheight / float(img.size[1]))
|
||||
wsize = int((float(img.size[0]) * float(hpercent)))
|
||||
img = img.resize((wsize, baseheight), Image.ANTIALIAS)
|
||||
img.save('resized_image.jpg')
|
||||
```
|
||||
|
||||
Notice **basewidth** is now **baseheight**, since height is fixed. In the third line, we are calculating the height percentage, so we need **img.size[1]** instead of **img.size[0]**. The size attribute is a tuple containing width and height in pixels; **size[0]** refers to the first tuple element, which is the width, and **size[1]** is the second element, which is height. Line 4 also has this switch between **size[0]** for width and **size[1]** for height.
|
||||
|
||||
_Originally published on Dayo Ntwari's [blog][4] and republished under Creative Commons with permission._
|
||||
|
||||
_This article was updated in January 2021 by the editor._
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/life/15/2/resize-images-python
|
||||
|
||||
作者:[Dayo Ntwari][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/dayontwari
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/life-python.jpg?itok=F2PYP2wT (Python in a tree)
|
||||
[2]: http://python.org/ (Python Programming Language – Official Website)
|
||||
[3]: https://pypi.org/project/Pillow/ (Python Imaging Library)
|
||||
[4]: https://dayontwari.wordpress.com/2015/01/06/how-to-resize-images-with-python/
|
82
translated/tech/20210101 Resize images using Python.md
Normal file
82
translated/tech/20210101 Resize images using Python.md
Normal file
@ -0,0 +1,82 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Resize images using Python)
|
||||
[#]: via: (https://opensource.com/life/15/2/resize-images-python)
|
||||
[#]: author: (Dayo Ntwari https://opensource.com/users/dayontwari)
|
||||
|
||||
使用 Python 调整图像大小
|
||||
======
|
||||
快速解释如何在 Python 中调整图像大小,同时保持相同的长宽比。
|
||||
![Python in a tree][1]
|
||||
|
||||
我喜欢 [Python][2],而且我已经学了一段时间了。前段时间,我写了一个 Python 脚本,在这个脚本中,我需要调整一堆图片的大小,同时保持长宽比(比例)不变。于是我四处寻找,发现了 [Pillow][3],这是一个 Python 图像库,也是一个叫做 PIL 的旧库的”友好分叉“。
|
||||
|
||||
要安装 Pillow,请使用 Python 的 `pip` 模块:
|
||||
|
||||
|
||||
```
|
||||
`$ python3 -m pip install Pillow`
|
||||
```
|
||||
|
||||
### 按宽度缩放
|
||||
|
||||
这是一个使用 Pillow 模块来调整图片大小的基本脚本:
|
||||
|
||||
|
||||
```
|
||||
from PIL import Image
|
||||
|
||||
basewidth = 300
|
||||
img = Image.open('fullsized_image.jpg')
|
||||
wpercent = (basewidth / float(img.size[0]))
|
||||
hsize = int((float(img.size[1]) * float(wpercent)))
|
||||
img = img.resize((basewidth, hsize), Image.ANTIALIAS)
|
||||
img.save('resized_image.jpg')
|
||||
```
|
||||
|
||||
这几行 Python 代码使用 Pillow 将一张图片 (**fullsized_image.jpg**) 调整为 300 像素的宽度,宽度在变量 **basewidth** 中设置,高度则与新的宽度成比例。比例高度的计算方法是:确定 300 像素占原宽度 (**img.size[0]**) 的百分比,然后将原高度 (**img.size[1]**) 乘以该百分比。所得的高度值保存在变量 **hsize** 中。
|
||||
|
||||
如果你需要不同的图片宽度,你可以将 **basewidth** 改为任何其他数字。另外,请注意,因为我想保留全尺寸的图片 (**fullsized_image.jpg**),因此我将调整后的图片以一个不同的名称 **resized_image.jpg** 保存。当然,你不必这么做。如果你想要这么做,你可以使用相同的文件名将调整后的图片覆盖全尺寸的图片。
|
||||
|
||||
### 按高度缩放
|
||||
|
||||
如果高度是固定的,而宽度是按比例变化的,那也基本差不多,你只需要把东西换一下:
|
||||
|
||||
|
||||
```
|
||||
from PIL import Image
|
||||
|
||||
baseheight = 560
|
||||
img = Image.open('fullsized_image.jpg')
|
||||
hpercent = (baseheight / float(img.size[1]))
|
||||
wsize = int((float(img.size[0]) * float(hpercent)))
|
||||
img = img.resize((wsize, baseheight), Image.ANTIALIAS)
|
||||
img.save('resized_image.jpg')
|
||||
```
|
||||
|
||||
注意 **basewidth** 现在是 **baseheight**,因为高度是固定的。在第三行中,我们在计算高度百分比,所以我们需要 **img.size[1]** 而不是 **img.size[0]**。size 属性是一个元组,包含宽度和高度,单位是像素,**size[0]** 指的是第一个元组元素,也就是宽度,**size[1]** 是第二个元素,也就是高度。第 4 行也有这样的切换,**size[0]** 代表宽度,**size[1]** 代表高度。
|
||||
|
||||
_最初发表在 Dayo Ntwari 的[博客][4]上,经允许后在 Creative Commons 许可下转载。_
|
||||
|
||||
_本文由编辑于 2021 年 1 月更新。_
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/life/15/2/resize-images-python
|
||||
|
||||
作者:[Dayo Ntwari][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/dayontwari
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/life-python.jpg?itok=F2PYP2wT (Python in a tree)
|
||||
[2]: http://python.org/ (Python Programming Language – Official Website)
|
||||
[3]: https://pypi.org/project/Pillow/ (Python Imaging Library)
|
||||
[4]: https://dayontwari.wordpress.com/2015/01/06/how-to-resize-images-with-python/
|
Loading…
Reference in New Issue
Block a user