Python 成像替代方案

发布于 2024-08-05 12:45:36 字数 323 浏览 8 评论 0原文

我有 python 代码,只需要对照片做一些简单的事情:裁剪、调整大小和覆盖水印。我使用过 PIL,重新采样/调整大小的结果很糟糕。我使用过imagemagick,其界面和命令是通过将一只猫包装在一个盒子里,然后在键盘上反复将它扔下一组楼梯来设计的。

我正在寻找一些不是 PIL 或 Imagemagick 的东西,我可以将其与 python 一起使用来进行简单、高质量的图像转换。就此而言,如果命令行界面良好,它甚至不需要 Python 绑定。

哦,它需要相对平台无关,我们的生产服务器是 Linux,但我们的一些开发人员在 Windows 上开发。它也不需要安装一堆愚蠢的 GUI 代码来用作库。

I have python code that needs to do just a couple simple things to photographs: crop, resize, and overlay a watermark. I've used PIL, and the resample/resize results are TERRIBLE. I've used imagemagick, and the interface and commands were designed by packaging a cat in a box, and then repeatedly throwing it down a set of stairs at a keyboard.

I'm looking for something which is not PIL or Imagemagick that I can use with python to do simple, high-quality image transformations. For that matter, it doesn't even have to have python bindings if the command line interface is good.

Oh, and it needs to be relatively platform agnostic, our production servers are linux, but some of our devs develop on windows. It can't require the installation of a bunch of silly gui code to use as a library, either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

我三岁 2024-08-12 12:45:36

我使用过 PIL,重新采样/调整大小的结果很糟糕。

它们不应该是这样,只要您:

  1. 仅使用 Image.ANTIALIAS 过滤进行缩小操作
  2. 仅使用 Image.BICUBIC 过滤进行放大操作。
  3. 如果您使用调色板图像,请记住在调整大小之前转换为“RGB”颜色模式,
  4. 不要使用 .thumbnail()。 很糟糕的
  5. 保存 JPEG 时将 quality= 级别设置为适当的值(默认值相当低)是

I've used PIL, and the resample/resize results are TERRIBLE.

They shouldn't be, as long as you:

  1. use only Image.ANTIALIAS filtering for downscaling operations
  2. use only Image.BICUBIC filtering for upscaling operations.
  3. remember to convert to 'RGB' colour mode before the resize if you are using a paletted image
  4. don't use .thumbnail(). it's crap
  5. set the quality= level to something appropriate when saving JPEGs (the default is quite low)
攒眉千度 2024-08-12 12:45:36

我不确定为什么 Image.thumbnail 受到如此严厉的批评。在我正在运行的当前版本中,它所做的只是找出所需的大小并就地调整图像大小。只要您使用正确的重新采样过滤器并首先转换为 RGB(如 bobince 所说),缩略图就不应该与调整大小有任何不同。

这是缩略图方法的实际来源:

def thumbnail(self, size, resample=NEAREST):
  # preserve aspect ratio
  x, y = self.size
  if x > size[0]: y = max(y * size[0] / x, 1); x = size[0]
  if y > size[1]: x = max(x * size[1] / y, 1); y = size[1]
  size = x, y

  if size == self.size:
      return

  self.draft(None, size)

  self.load()

  try:
      im = self.resize(size, resample)
  except ValueError:
      if resample != ANTIALIAS:
          raise
      im = self.resize(size, NEAREST) # fallback

  self.im = im.im
  self.mode = im.mode
  self.size = size

  self.readonly = 0

I'm unsure as to why Image.thumbnail is getting such flak. In the present release that I'm running off of it does little more than figure out the desired size and resize the image in place. As long as you're using the proper resample filter and convert to RGB first (as bobince says) thumbnail shouldn't be any different than resize.

Here's the actual source for the thumbnail method:

def thumbnail(self, size, resample=NEAREST):
  # preserve aspect ratio
  x, y = self.size
  if x > size[0]: y = max(y * size[0] / x, 1); x = size[0]
  if y > size[1]: x = max(x * size[1] / y, 1); y = size[1]
  size = x, y

  if size == self.size:
      return

  self.draft(None, size)

  self.load()

  try:
      im = self.resize(size, resample)
  except ValueError:
      if resample != ANTIALIAS:
          raise
      im = self.resize(size, NEAREST) # fallback

  self.im = im.im
  self.mode = im.mode
  self.size = size

  self.readonly = 0
时间你老了 2024-08-12 12:45:36

PIL 可以很好地调整大小。确保您的源图像处于 RGB 模式,而不是调色板颜色,并尝试不同的算法选择。

PIL can do good resizing. Make sure your source image is in RGB mode, not palette colors, and try the different algorithm choices.

葬花如无物 2024-08-12 12:45:36

虽然 imagemagick 似乎是事实上的开源图像库,但可能 DevIL (跨平台,似乎执行简单的图像操作)或 FreeImage

While imagemagick seems to be the de facto open-source imaging library, possibly DevIL (cross platform, seems to do simple image operations) or FreeImage.

蓝梦月影 2024-08-12 12:45:36

您检查过 pypi 吗?粗略搜索显示了一些与图像相关的工具,我还发现了 python-gd,但不知道它有多有用。

我自己对 PIL 从来没有遇到过任何问题,但某种变化可能会很有趣。

Have you checked pypi? A cursory search shows some image related tools there, I also discovered python-gd, no clue how useful it might be though.

I've never had any issues with PIL myself, but some kind of variety might be interesting.

丑疤怪 2024-08-12 12:45:36

我认为 GIMP 有一个合理的命令行界面。

GIMP has a reasonable command-line interface, I think.

帥小哥 2024-08-12 12:45:36

看一下其中一些图像库:

hxxp://pypi.python.org/pypi/collective.croppingimagefield/0.1beta

hxxp://pypi.python.org/pypi/cropresize/0.1.1

hxxp://pypi .python.org/pypi/image_resize/1.0

Take a look at some of these imaging libraries:

hxxp://pypi.python.org/pypi/collective.croppingimagefield/0.1beta

hxxp://pypi.python.org/pypi/cropresize/0.1.1

hxxp://pypi.python.org/pypi/image_resize/1.0

怼怹恏 2024-08-12 12:45:36

我使用过 PIL,重新采样/调整大小的结果很糟糕。

PIL 中的调整大小在很多方面都受到了破坏,而且 PIL 很长一段时间都没有得到维护。从 Pillow 2.7 开始,大多数问题都已解决性能显着提高。确保您使用的是最新的 Pillow

I've used PIL, and the resample/resize results are TERRIBLE.

Resizing in PIL was broken in many ways and PIL is not maintained for a long time. Starting from Pillow 2.7 most of the problems are fixed along with dramatically performance improvement. Make sure you are using latest Pillow.

你另情深 2024-08-12 12:45:36

上次我比较时,这个缩减器的输出几乎与 GIMP 的“立方”选项:

 import Image

 def stretch(im, size, filter=Image.NEAREST):
     im.load()
     im = im._new(im.im.stretch(size, filter))
     return im

IIRC 的输出相同,差异在视觉上无法区分 - 由于舍入,一些像素值 +/-1,并且它们往往是圆形边缘。速度也不慢。

cf: http://www.mail-archive.com/ [电子邮件受保护]/msg00248.html

Last time I compared, this downscaler's output is almost identical to that of GIMP's "cubic" option:

 import Image

 def stretch(im, size, filter=Image.NEAREST):
     im.load()
     im = im._new(im.im.stretch(size, filter))
     return im

IIRC, the differences are visually indistinguishable -- some pixel values +/-1 due to rounding, and they tend to be round the edges. It's not slow either.

cf: http://www.mail-archive.com/[email protected]/msg00248.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文