次のようなリストにピクセルのリスト (3 つの RGB 値を持つタプルとして表される) があるとしますlist(im.getdata())
。
[(0,0,0),(255,255,255),(38,29,58)...]
この形式で RGB 値 (各タプルがピクセルに対応) を使用して新しい画像を作成するにはどうすればよいですか?
ご協力いただきありがとうございます。
次のようなリストにピクセルのリスト (3 つの RGB 値を持つタプルとして表される) があるとしますlist(im.getdata())
。
[(0,0,0),(255,255,255),(38,29,58)...]
この形式で RGB 値 (各タプルがピクセルに対応) を使用して新しい画像を作成するにはどうすればよいですか?
ご協力いただきありがとうございます。
次のように実行できます。
list_of_pixels = list(im.getdata())
# Do something to the pixels...
im2 = Image.new(im.mode, im.size)
im2.putdata(list_of_pixels)
そのためにも使用できますscipy
:
#!/usr/bin/env python
import scipy.misc
import numpy as np
# Image size
width = 640
height = 480
channels = 3
# Create an empty image
img = np.zeros((height, width, channels), dtype=np.uint8)
# Draw something (http://stackoverflow.com/a/10032271/562769)
xx, yy = np.mgrid[:height, :width]
circle = (xx - 100) ** 2 + (yy - 100) ** 2
# Set the RGB values
for y in range(img.shape[0]):
for x in range(img.shape[1]):
r, g, b = circle[y][x], circle[y][x], circle[y][x]
img[y][x][0] = r
img[y][x][1] = g
img[y][x][2] = b
# Display the image
scipy.misc.imshow(img)
# Save the image
scipy.misc.imsave("image.png", img)
与える