I am using the Python Imaging Library to colorize a black and white image with a lookup table that defines the color relationships. The lookup table is simply a 256-element list of RGB tuples:

>>> len(colors)

>>> colors[0]

(255, 237, 237)

>>> colors[127]

(50, 196, 33)

My first version used the getpixel() and putpixel() methods:

for x in range(w):

for y in range(h):

pix = img.getpixel((x,y))

img.putpixel((x,y), colors[pix[0]])

This was horribly slow. A profile report pointed to the putpixel and getpixel methods as the culprits. A little investigation (i.e, read the docs) and I find "Note that this method is relatively slow." re: putpixel. (actual runtime: 53s in putpixel and 50s getpixel for a 1024x1024 image)

Based on the suggestion in the docs, I used im.load() and direct pixel access instead:

pixels = img.load()

for x in range(w):

for y in range(h):

pix = pixels[x, y]

pixels[x, y] = colors[pix[0]]

Processing sped up by an order of magnitude, but is still slow: about 3.5s to process a 1024x1024 image.

A more thorough study of the PIL docs seems to indicate Image.point() is exactly intended for this purpose:

im.point(table) => image

im.point(function) => image

Returns a copy of the image where each pixel has been mapped through the given table. The table should contains 256 values per band in the image. If a function is used instead, it should take a single argument. The function is called once for each possible pixel value, and the resulting table is applied to all bands of the image.

I've spent some time hacking around with the interface, but can't quite seem to get it right. Forgive my ignorance, but PIL's docs are curt and I don't have much image processing experience. I've googled around a bit and turned up a few examples, but nothing that made the usage "click" for me. Thus, finally, my questions:

Is Image.point() the right tool for this job?

What format/structure does Image.point() expect the table?

Can someone rough out an example implementation? Every iteration I've tried so far has ended up with a straight black image.

解决方案Is Image.point() the right tool for

this job?

Yes indeed, Image.point() is perfect for this job

What format/structure does

Image.point() expect the table?

You should flatten the list so instead of [(12, 140, 10), (10, 100, 200), ...] use:

[12, 140, 10, 10, 100, 200, ...]

Here is a quick example I just tried:

im = im.point(range(256, 0, -1) * 3)

And by the way, if you need more control over colors and you feel Image.point is not for you you can also use Image.getdata and Image.putdata to change colors more quickly than both load and putpixel. It is slower than Image.point though.

Image.getdata gives you the list of all pixels, modify them and write them back using Image.putdata. It is that simple. But try to do it using Image.point first.

I made a mistake in the first explanation, I'll explain correctly now:

The color table actually is like this

[0, 1, 2, 3, 4, 5, ...255, 0, 1, 2, 3, ....255, 0, 1, 2, 3, ...255]

Each band range next to the other.

To change the color (0, 0, 0) to (10, 100, 10) it need to become like this:

[10, 1, 2, 3, 4, 5, ...255, 100, 1, 2, 3, ....255, 10, 1, 2, 3, ...255]

To transform your color list into the right format try this:

table = sum(zip(*colors), ())

I think my first example should demonstrate the formate for you.

I am using the Python Imaging Library to colorize a black and white image with a lookup table that defines the color relationships. The lookup table is simply a 256-element list of RGB tuples:>&gt... Image .open :打开一张图片,获取 Image 对象。 Image .convert(mode='L') :将图片转为灰度图像 Image .crop :截取图片 的一块区域 Image .paste :将另外一张图片粘贴到当前图片 Image .save :保存图片
Image Draw模块提供了图像对象的简单2D绘制。用户可以 使用 这个模块创建新的图像,注释或润饰已存在图像,为web应用实时产生各种图形。 PIL 一个更高级绘图库见The aggdraw Module。 一、 Image Draw模块的概念 Coordinates 绘图接口 使用 PIL 一样的坐标系统,即(0,0)为左上角。 Colours 为了指定颜色,用户可以使
import pylab from PIL import Image , Image Enhance from sk image import img_as_ubyte, img_as_float import numpy as np def plot_ image ( image , title=""): # pylab.gray() pylab.title(title, size=20) pylab.imshow( image , cmap='gray') # image .show()
图像转换成九种不同的格式,分别1,L,P,RGB,RGBA,CMYK,YCbCr,I,F 1.模式“1” 模式“1”为二值图像,非黑即白。但是它每个 像素 用8个bit表示,0表示黑,255表示白。 2.模式“L” 模式”L”为灰色图像,它的每个 像素 用8个bit表示,0表示黑,255表示白,其他数字表示不同的灰度。在 PIL ,从模式“RGB”转换为“L”模式是按照下面的公式转换的: L = R * 2...
因此做了以下的实验,最终发现torchvision+ PIL 的方式似乎更胜一筹; from torchvision import transforms import tensorflow as tf import cv2 import time imgpath = "/home/night/Py
Paste 定义1:im.paste( image ,box) 含义1:将一张图粘贴到另一张图像上。变量box或者是一个给定左上角的2元组,或者是定义了左,上,右和下 像素 坐标的4元组,或者为空(与(0,0)一样)。如果给定4元组,被粘贴的图像的尺寸必须与区域尺寸一样。 如果模式不匹配,被粘贴的图像将被转换为当前图像的模式。
首先,需要安装 PIL (Python Imaging Library)库,这是一个用于 处理 图像的库。然后,读取json文件,并从 提取坐标信息。接下来, 使用 PIL 读取原图,并 使用 坐标信息绘制在原图上。代码示例如下: import json from PIL import Image , Image Draw # 读取json文件 with open('coordinates.json', 'r') as f: coordinates = json.load(f) # 读取原图 img = Image .open('original.jpg') draw = Image Draw.Draw(img) # 绘制坐标 for coordinate in coordinates: x, y = coordinate['x'], coordinate['y'] draw. point ((x, y), fill='red') # 保存结果图 img.save('result.jpg') 希望这对你有所帮助!