Python中不安全的图像处理(如C#中的LockBits) [英] Unsafe Image Processing in Python like LockBits in C#

查看:118
本文介绍了Python中不安全的图像处理(如C#中的LockBits)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Python中执行不安全的图像处理?

Is it possible to do Unsafe Image Processing in Python?

与C#一样,由于Image的getPixel方法运行太慢,我在Python中处理像素时遇到了困难.

As with C# I encountered a hard wall with my pixel processing in Python as getPixel method from Image is simply running too slow.

是否可以像c#中的LockBits一样直接访问内存中的图像?这将使我的程序运行更快.

Is it possible to get a direct access to the image in memory like LockBits in c# does? It will make my program run much faster.

谢谢

标记

推荐答案

对此没有什么不安全"的地方.

There is nothing "unsafe" about this.

一旦您了解了Python的工作原理,调用一种方法来检索每个像素上的信息将变得很慢,这将成为一种专利.

Once you understand how Python works, it becomes patent that calling a method to retrieve information on each pixel is going to be slow.

首先,尽管您未提供任何信息,但我假设您使用的是枕头"-Python图像库(PIL),它是使用Python进行图像处理的最著名的库.由于它是第三方软件包,因此您没有义务使用它. (PIL在图像上确实有一种getpixel方法,但没有一种getPixel方法)

First of all, although you give no information on it, I assume you are using "Pillow" - the Python Image Library (PIL) which is the most well known library for image manipulation using Python. As it is a third party package, nothing obliges you to use that. (PIL does have a getpixel method on images, but not a getPixel one)

以可操作的方式拥有所有数据的一种直接方法是创建图像数据的字节数组对象-给定图像的img变量,您可以执行以下操作:

One straightforward way to have all data in a manipulable way is to create a bytearray object of the image data - given an image in a img variable you can do:

data = bytearray(img.tobytes())  

就是这样,您可以线性访问图像上的所有数据.要获得特定的像素,您需要获取图像的宽度,高度和每像素字节数.后者不是PIL中的直接Image属性,因此必须在给定Image mode的情况下对其进行计算.最常见的图像类型是RGB,RGBA和L.

And that is it, you have linear access to all data on your image. To get to an specific Pixel in there, you need to get the image width , heigth and bytes-per-pixel. The later one is not a direct Image attribute in PIL, so you have to compute it given the Image's mode. The most common image types are RGB, RGBA and L.

因此,如果要在图像上写出"x,y,宽度,大小"的矩形,可以执行以下操作:

So, if you want to write out a rectangle at "x, y, width, size" at an image, you can do this:

def rectangle(img, x,y, width, height):
    data = bytearray(img.tobytes())
    blank_data = (255,) * witdh * bpp
    bpp = 3 if data.mode == 'RGB' else 4 if data.mode == 'RGBA' else 1
    stride = img.width * bpp
    for i in range(y, y + height):
         data[i * stride + x * bpp: i * stride + (x + width) * bpp)] = blank_data
    return Image.frombytes(img.mode, (img.width, img.height), bytes(data))

这没什么用,只是用于简单的操作.需要使用Python在图像中应用过滤器和其他更复杂算法的人们通常使用numpy访问该图像-python高性能数据处理包,这与许多其他具有特定于图像的功能的包紧密结合-通常安装为scipy.

That is not much used, but for simple manipulation. People needing to apply filters and other more complicated algorithms in images with Python usually access the image using numpy - python high-performance data manipulation package, wich is tightly coupled with a lot of other packages that have things specific for images - usually installed as scipy.

因此,要将图像作为ndarray,它已经为您完成了上述所有坐标->字节转换,您可以使用:

So, to have the image as an ndarray, which already does all of the above coordinate -> bytes conversion for you, you can use:

 import scipy.misc
 data = scipy.misc.imread(<filename>)

(在 https://docs.scipy.org/doc/scipy中检查文档/reference/)

这篇关于Python中不安全的图像处理(如C#中的LockBits)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆