开发者

Simple Image Manipulation with Python

开发者 https://www.devze.com 2023-01-19 01:31 出处:网络
What I\'m trying to do: I want to give the user the ability to upload a picture that is any size. This image is then resized if it is over 1024 wide or over 768 high. It then resizes the image to be

What I'm trying to do:

I want to give the user the ability to upload a picture that is any size. This image is then resized if it is over 1024 wide or over 768 high. It then resizes the image to be within those bounds, but keeping proportions. Then it adds a semi-transparent watermark to the lower right corner, and saves the file.

Before it adds the watermark, it will create a copy of the image and resize it down to a thumbnail size (also keeping proportions) and saves it in a separate folder.

The Problems with PIL:

As far as resizing goes, I was hoping it would have a way to do smart resizing (keep proportions). Also, I didn't seem to have much control over the quality level when saving it as a JPEG. I had to save it as a PNG to keep full quality which was pretty heavy.

For the thumbnail, it sounds that it might be pretty difficult, reading through the documentation of PIL, but I could be wrong.

The Question

Are there any other, more advanced image libraries for Python that may be a bit more up to date, or include some features that I am looking for? Are there any public functions that do what I'm looking for that I could use? I don't m开发者_Python百科ind writing this stuff myself, but wanted to check first. Thanks!


As far as resizing goes, I was hoping it would have a way to do smart resizing (keep proportions).

Seeing as this is probably one or two lines in Python, I don't see why this needs to be in the interface of the library.

Also, I didn't seem to have much control over the quality level when saving it as a JPEG.

Quoting the handbook:

The save method supports the following options:

quality

The image quality, on a scale from 1 (worst) to 95 (best). The default is 75. Values above 95 should be avoided; 100 completely disables the JPEG quantization stage.

Did you try that? If so, why didn't it help?


Agree with Jim about proportions. You are talking about such a trivial operation that can easily be inlined wherever, that I wouldn't even look for the feature/option in the API to be honest. The moment you call .resize(factor = 0.8, keepRatio = True ), you're typing the same amount of text of punching in .resize(hFactor = 0.8, vFactor = 0.8).

As for libraries, you could have a look at imageMagick with PythonMagick: http://www.imagemagick.org/script/index.php

It does resizing (with respectable interpolators) and writes to several formats, offers text overlays out of the box, or simple compositing if you have your own watermark you want to paste in.

Used that and not PIL in the last couple places where I had to deal with the problem, and for the simple stuff i needed I was satisfied, that was through magick++ though, not with the Python bindings, but I doubt the experience would be very different. Completely out of touch with PIL though, so I don't know how it would compare.


In a pinch, convert the Image to a numpy array, modify it as you please, and convert back.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号