Merge remote-tracking branch 'LCTT/master'

This commit is contained in:
Xingyu Wang 2021-01-11 09:12:47 +08:00
commit 8ff49f1809
6 changed files with 384 additions and 168 deletions

View File

@ -0,0 +1,85 @@
[#]: collector: (lujun9972)
[#]: translator: (wxy)
[#]: reviewer: (wxy)
[#]: publisher: (wxy)
[#]: url: (https://linux.cn/article-13002-1.html)
[#]: subject: (6 ways this fullscreen text editor improves focus)
[#]: via: (https://opensource.com/article/20/12/focuswriter)
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
全屏文本编辑器 Focuswriter 提高关注力的六种方式
======
> 这款全屏编辑器可以帮助你设定写作目标,并将干扰降到最低,从而实现你的目标。
![双手打字机][1]
计算机的好处是,它们真的很擅长一心多用。
计算机的坏处是,它们实在太擅长一心多用。
无论你认为自己作为一个人,在多任务处理方面是好是坏,有时你需要一点帮助来集中注意力,尤其是当你试图撰写清晰和简明的交流信息时。而这正是开发 Focuswriter 的原因。
### 安装
在 Linux 上,你可以从 [Flathub][2] 以 Flatpak 包安装 Focuswriter。
在 Windows 或 Linux 上(如果你不使用 Flatpak你可以[从它的网站安装 Focuswriter][3]。你也可以从源代码安装,它也可以从 Focuswriter 网站上获得。
### 使用 Focuswriter
不可否认Focuswriter 实际上处于文本编辑器和文字处理器的重叠的地带。它的默认格式是 <ruby>开放文档文本<rt>Open Document Text</rt></ruby>.odt格式所以它允许你设置文本样式和对齐文本、标记页眉以及开启和关闭智能引号。但它的文字处理器功能也就到此为止因为 Focuswriter 确实主要专注于_写作_而不是对你所写的东西进行样式设计。
Focuswriter 以几种不同的方式鼓励专注力:
#### 吸引力
Focuswriter 看起来并不像你平常的电脑应用程序,更不像你平常的文本编辑器。当它启动时,它会在你的文档后面加载一个主题。主题一般是良性的 —— 默认是写字台,但也有星空、远方的风景等等。当然,你并不是要全神贯注于此主题,但它确实可以帮助你避免被徘徊于你的编辑器后面的其他应用窗口或电脑桌面分散注意力。因为它很容易定制,所以你几乎不大可能找不到你喜欢的主题。
![木质写字台背景主题上的 Focuswriter 白色盒子与灰色字样][4]
#### 音效
Focuswriter 使用起来其实很有趣。除了视觉主题外它还有一个_可选的_打字机音频主题。启用后每按一次键都会发出机械打字机按键的声音。当你按下**回车键**时,回车的声音令人满意,这是对你完成一段文字(或一行文字,如果你为了改善版本控制而每行写一句话)的奖励。
这个音频主题完全是可选的,默认情况下是不开启的。你可能会怀疑它的效果,但你不应该低估发出这些生产力声音而有具有现代文本编辑能力所带来的满足感。
#### 全屏显示
Focuswriter 不仅在默认情况下启动全屏,而且还从视图中隐藏了其菜单和其他部件。要访问主菜单栏,你要将光标放在屏幕顶部,直到它出现。右边隐藏了一个滚动条,左边隐藏了一个文档切换器小部件,底部则隐藏着一个状态栏。
#### 极简主义
Focuswriter 的编辑功能并不多。它没有高级搜索选项,也没有代码格式化或语法高亮的专门选项。它的设计是让你在文档中写入文本,而不是其他的什么。即使是上下文右键菜单也只提供基本的编辑功能(复制、剪切、粘贴等)或拼写检查选项。
#### 聚焦一行
Focuswriter 的另一个可选功能是它能够“灰化”除了你的当前文本行之外的所有文本行。这可以帮助你的眼睛懒洋洋地定位你当前的文字行,如果你不是一个盲打打字员,或者如果你正在抄写一些你手写在纸张上的东西,眼睛必须经常远离屏幕,这可能特别有用。
#### 目标追踪
你可以在 Focuswriter 的首选项中为自己设置一个每日字数目标或定时目标。当你工作时,你的进度会被跟踪,并显示在屏幕底部的状态栏中。除非你将光标悬停在它上面,否则它是隐藏在你的视野中的,所以你不会被迫沉迷于它,但它足够方便,可以帮助你保持进度。
### 即使你认为它很傻,也请尝试一下 Focuswriter
以前也听说过全屏文本编辑器,但我一直不觉得全屏文本编辑器对我来说有什么重要意义。毕竟,几乎所有的应用程序都可以全屏显示,而我个人也没有任何关注力问题。
不过在尝试过 Focuswriter 之后我明白了一个好的全屏“关注力优先”文本编辑器的魅力所在。也许没有必要但话又说回来开源是如此的兴盛我们有大量的冗余应用这些应用功能略有差异但实现上差异很大。Focuswriter 让写作变得有趣,它让工作变得愉快。下载它,设定一个目标,然后开始写作吧!
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/12/focuswriter
作者:[Seth Kenlon][a]
选题:[lujun9972][b]
译者:[wxy](https://github.com/wxy)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/seth
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/typewriter-hands.jpg?itok=oPugBzgv (Typewriter with hands)
[2]: https://flathub.org/apps/details/org.gottcode.FocusWriter
[3]: https://gottcode.org/focuswriter/
[4]: https://opensource.com/sites/default/files/uploads/focuswriter-31_days_focuswriter-opensource.png (Focuswriter white box with gray wording on wooden writing desk background theme)

View File

@ -1,84 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: ( )
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (6 ways this fullscreen text editor improves focus)
[#]: via: (https://opensource.com/article/20/12/focuswriter)
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
6 ways this fullscreen text editor improves focus
======
This fullscreen editor helps you set your writing goals and keeps
distractions to a minimum so you can achieve them.
![Typewriter with hands][1]
The great thing about computers is that theyre really good at multi-tasking.
The bad thing about computers is that theyre really good at multi-tasking.
Whether you consider yourself, as a human, good or bad at multi-tasking, sometimes you need a little help focusing. One of those times is when youre trying to compose clear and concise communication. And thats exactly why Focuswriter was developed.
### Install
On Linux, you can install Focuswriter as a Flatpak from [Flathub][2].
On Windows or Linux (if you dont use Flatpak), you can [install Focuswriter from its website][3]. You can also install from source code, also available from the Focuswriter webpage.
### Using Focuswriter
Focuswriter is, admittedly, actually a cross between a text editor and a word processor. Its default format is the Open Document Text (.odt) format, so it allows you to style and align text, mark headers, and toggle smart quotes on and off. But thats where its word processor features end because Focuswriter does mostly focus on _writing_, not on styling what youve written.
Focuswriter encourages focus in a few different ways.
#### Attractiveness
Focuswriter doesnt look like your usual computer application, much less your usual text editor. When it launches, it loads a theme behind your document. The theme is generally benign—a writing desk is the default, but theres also a starry sky, a faraway landscape, and so on. Youre not meant to become preoccupied with the theme, of course, but it does help you from getting distracted by some other application window or your computer desktop lingering behind your editor. Because its easy to customize, theres little chance of you being left without a theme you like.
![Focuswriter white box with gray wording on wooden writing desk background theme][4]
#### Sound effects
Focuswriter is actually fun to use. Aside from its visual theme, it also has an _optional_ typewriter audio theme. Once enabled, every keypress makes the sound of a mechanical typewriter key. When you press **Return**, the satisfying sound of a carriage return is your reward for completing a paragraph (or a line, if you write one sentence per line to improve version control).
This audio theme is entirely opt-in, and its not on by default. You may doubt its efficacy, but you shouldnt underestimate the satisfaction of making those sounds of productivity while also wielding the powers of modern text editing.
#### Fullscreen display
Focuswriter not only launches fullscreen by default, but it also hides its menus and other widgets from view. To access the main menu bar, you place your cursor to the top of your screen until it appears. Theres a scroll bar hidden on the right, a document switcher widget on the left, and a status bar at the bottom.
#### Minimalism
There isnt much to Focuswriters editing capabilities. It doesnt have advanced search options or specialized options for code formatting or syntax highlighting. It lets you write text, and little else, into a document, by design. Even the contextual right-click menu only offers basic editing functions (copy, cut, paste, and so on) or spell-checking options.
#### Line focus
Another optional feature of Focuswriter is its ability to "grey out" all lines of text but your current one. This helps your eyes lazily locate your current line of text, which can be especially helpful if youre not a touch-typist or if youre transcribing something youve handwritten on physical paper and must look away from the screen often.
#### Goal tracker
You can set a daily word count goal or a timed goal for yourself in Focuswriters preferences. Your progress is tracked as you work and displayed in the status bar at the bottom of the screen. Its hidden from your view unless you hover your cursor over it, so youre not compelled to obsess over it, but its convenient enough to help you stay on track.
### Try Focuswriter (even if you think its silly)
Ive heard about fullscreen text editors before, and Ive never felt it was important for me to try one. After all, nearly any application can be made fullscreen, and I dont personally have trouble focusing anyway.
Having tried Focuswriter, though, I understand the appeal of a good fullscreen "focus first" text editor. It may not be necessary, but then again, open source is thriving. We have the luxury of redundant sets of applications that have minor differences in function but major differences in implementation. Focuswriter makes writing fun; it makes the work enjoyable. Download it, set a goal, and get writing!
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/12/focuswriter
作者:[Seth Kenlon][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/seth
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/typewriter-hands.jpg?itok=oPugBzgv (Typewriter with hands)
[2]: https://flathub.org/apps/details/org.gottcode.FocusWriter
[3]: https://gottcode.org/focuswriter/
[4]: https://opensource.com/sites/default/files/uploads/focuswriter-31_days_focuswriter-opensource.png (Focuswriter white box with gray wording on wooden writing desk background theme)

View File

@ -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/

View File

@ -1,5 +1,5 @@
[#]: collector: (lujun9972)
[#]: translator: ( )
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )

View File

@ -0,0 +1,216 @@
[#]: collector: (lujun9972)
[#]: translator: ( )
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (9 Decentralized, P2P and Open Source Alternatives to Mainstream Social Media Platforms Like Twitter, Facebook, YouTube and Reddit)
[#]: via: (https://itsfoss.com/mainstream-social-media-alternaives/)
[#]: author: (Abhishek Prakash https://itsfoss.com/author/abhishek/)
9 Decentralized, P2P and Open Source Alternatives to Mainstream Social Media Platforms Like Twitter, Facebook, YouTube and Reddit
======
You probably are aware that [Facebook is going to share the user data from its end to end encrypted chat service WhatsApp][1]. This is not optional. You have to accept that or stop using WhatsApp altogether.
Privacy cautious people had seen it coming a long time ago. After all, [Facebook paid $19 billion to buy a mobile app like WhatsApp][2] that hardly made any money at that time. Now its time for Facebook to get the return on its $19 billion investment. They will share your data with advertisers so that you get more personalized (read invasive) ads.
If you are fed up with the “**my way or highway**” attitude of the big tech like Facebook, Google, Twitter, perhaps you may try some alternative social media platforms.
These alternative social platforms are open source, use a decentralized approach with P2P or Blockchain technologies, and you may be able to self-host some of them.
### Open source and decentralized social networks
![Image Credit: Datonel on DeviantArt][3]
Ill be honest with you here. These alternative platforms may not give you the same kind of experience you are accustomed to, but these platforms would not infringe on your privacy and freedom of speech. Thats a trade off.
#### 1\. Minds
Alternative to: Facebook and YouTube
Features: Open Source code base, Blockchain
Self-host: No
On Minds, you can post videos, blogs, images and set statuses. You can also message and video chat securely with groups or directly with friends. Trending feeds and hashtags allows you to discover articles of your interest.
Thats not it. You also have the option to earn tokens for your contributions. These tokens can be used to upgrade your channel. Creators can receive direct payments in USD, Bitcoin and Ether from fans.
[Minds][4]
#### 2\. Aether
Alternative to: Reddit
Features: Open Source, P2P
Self-host: No
![][5]
Aether is an open source, P2P platform for self-governing communities with auditable moderation and mod elections.
The content on Aether is ephemeral in nature and it is kept only for six months unless someone saves it. Since it is P2P, there is no centralized servers.
An interesting feature of Aether is its democratic communities. Communities elect mods and can impeach them by votes.
[Aether][6]
#### 3\. Mastodon
Alternative to: Twitter
Features: Open Source, Decentralized
Self-host: Yes
![][7]
[Mastodon][8] is already known among FOSS enthusiasts. We have covered [Mastodon as an open source Twitter alternative][9] in the past, and [we also have a profile on Mastodon][10].
Mastodon isnt a single website like Twitter, its a network of thousands of communities operated by different organizations and individuals that provide a seamless social media experience. You can host your own Mastodon instance and choose to connect it with other Mastodon instances or you simply join one of the existing Mastodon instances like [Mastodon Social][11].
[Mastodon][8]
#### 4\. LBRY
Alternative to: YouTube
Features: Open Source, Decentralized, Blockchain
Self-host: No
![][12]
At the core, [LBRY][13] is a blockchain based decentralization protocol. On top of that protocol, you get a digital marketplace powered by its own cryptocurrency.
Though LBRY allows creators to offer l kind of digital content like movies, books and games, it is essentially promoted as an YouTube alternative.
We have covered [LBRY on Its FOSS][14] in the past and you may read that for more details. If you are joining LBRY, dont forget to follow Its FOSS there.
[LBRY][15]
#### 5\. KARMA
Alternative to: Instagram
Features: Decentralized, Blockchain
Self-host: No
![][16]
Heres another blockchain based social network governed by cryptocurrency.
KARMA is an Instagram clone built on top of open source blockchain platform, [EOSIO][17]. Every like and share your content gets, earns you KARMA tokens. You can use these tokens to boost your content or convert it to real money through one of the partner crypto exchanges.
KARMA is a mobile only app and available on Play Store and App Store.
[KARMA][18]
#### 6\. Peertube
Alternative to: YouTube
Features: Decentralized, P2P
Self-host: No
![][19]
Developed by French company Framasoft, PeerTube is a decentralized video streaming platform. PeerTube uses the [BitTorrent protocol][20] to share bandwidth between users.
PeerTube aims to resist corporate monopoly. It does not rely on ads and does not track you. Keep in mind that your IP address is not anonymous here.
There are various instances of PeerTube available where you can host your videos. Some instances may charge money while most are free.
[PeerTube][21]
#### 7\. Diaspora
Alternative to: Facebook
Features: Decentralized, Open Source
Self-host: Yes
Diaspora was one of the earliest decentralized social networks. This was back in 2010 and Diaspora was touted as a Facebook alternative. It did get some deserving limelight in its initial years but it got confined to only a handful of niche members.
Similar to Mastodon, Diaspora is composed of pods. You can register with a pod or host your own pod. The Big Tech doesnt own your data, you do.
[Diaspora][22]
#### 8\. Dtube
Alternative to: YouTube
Features: Decentralized, Blockchain
Self-host: No
![][23]
Dtube is a blockhain based decentralized YouTube clone. I use the word YouTube clone because the interface is way too similar to YouTube.
Like most other blockchain based social networks, Dtube is governed by DTube Coins (DTC) that creator earns when someone watches or interact with their content. The coins can be used to promote the content or cashed out from partner crypto exhcnages.
[DTube][24]
#### 9\. Signal
Alternative to: WhatsApp, Facebook Messenger
Features: Open Source
Self-host: No
![][25]
Unlike the end to end encrypted chats in WhatsApp, Signal doesnt track you, share your data and invade your privacy.
[Signal rose to fame][26] when Edward Snowden endorsed it. It got even more famous when [Elon Musk][27] tweeted about it after WhatsApp sharing user data with Facebook.
Signal uses its own open source Signal protocol to give you end-to-end encrypted messages and calls.
[Signal][28]
#### What else?
There are some other platforms that are not open source or decentralized, but they respect your privacy and free speech.
* [MeWe][29]: Alternative to Facebook
* [Voice][30]: Alternative to Medium
There is also Element messenger based on Matrix protocol which you may try.
I know there are probably several other such alternative social media platforms. Care to share them? I might add them to this list.
If you had to choose one of the platforms from the list, which one would you choose?
--------------------------------------------------------------------------------
via: https://itsfoss.com/mainstream-social-media-alternaives/
作者:[Abhishek Prakash][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://itsfoss.com/author/abhishek/
[b]: https://github.com/lujun9972
[1]: https://arstechnica.com/tech-policy/2021/01/whatsapp-users-must-share-their-data-with-facebook-or-stop-using-the-app/
[2]: https://money.cnn.com/2014/02/19/technology/social/facebook-whatsapp/index.html
[3]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2021/01/1984-quote.png?resize=800%2C450&ssl=1
[4]: https://www.minds.com/
[5]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/01/aether-reddit-alternative.png?resize=800%2C600&ssl=1
[6]: https://getaether.net
[7]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/mastodon.png?resize=800%2C623&ssl=1
[8]: https://joinmastodon.org/
[9]: https://itsfoss.com/mastodon-open-source-alternative-twitter/
[10]: https://mastodon.social/@itsfoss
[11]: https://mastodon.social
[12]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2020/04/lbry-interface.jpg?resize=800%2C420&ssl=1
[13]: https://lbry.org
[14]: https://itsfoss.com/lbry/
[15]: https://lbry.tv/$/invite/@itsfoss:0
[16]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2021/01/karma-app.jpg?resize=800%2C431&ssl=1
[17]: https://eos.io
[18]: https://karmaapp.io
[19]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/peertube-federation-multiplicity.jpg?resize=600%2C341&ssl=1
[20]: https://www.slashroot.in/what-bittorrent-protocol-and-how-does-bittorrent-protocol-work
[21]: https://joinpeertube.org
[22]: https://diasporafoundation.org
[23]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/dtube.jpg?resize=800%2C516&ssl=1
[24]: https://d.tube
[25]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2019/12/signal-shot.jpg?resize=800%2C565&ssl=1
[26]: https://itsfoss.com/signal-messaging-app/
[27]: https://www.britannica.com/biography/Elon-Musk
[28]: https://www.signal.org
[29]: https://mewe.com
[30]: https://www.voice.com

View 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/