画像編集(表示なし)にpygameを使用しています。(はい、PIL/pillow のような他のオプションがあることは知っていますが、これには pygameが機能するはずです。)
数式に従って各ピクセルのアルファ値を個別に設定している画像を描画して保存したい (複雑な RGBA プロファイルを描画している)。これpixels_alpha
が正しい方法のようです。しかし、変更pixels_alpha
すると無視され、画像は透明のままになります。これが私のコードです...
import pygame as pg
import os
def init_transparent(img_width, img_height):
"""
Create a new surface with width and height, starting with transparent
background. This part works!
"""
os.environ['SDL_VIDEODRIVER'] = 'dummy'
pg.init()
pg.display.init()
# next command enables alpha
# see http://stackoverflow.com/questions/14948711/
pg.display.set_mode((img_width, img_height), 0, 32)
# pg.SRCALPHA is a flag that turns on per-pixel alpha.
return pg.Surface((img_width, img_height), pg.SRCALPHA)
def make_semitransparent_image():
surf = init_transparent(20, 20)
# alphas should be a direct reference to the alpha data in surf
alphas = pg.surfarray.pixels_alpha(surf)
# alphas is a uint8-dtype numpy array. Right now it's all zeros.
for i in range(20):
for j in range(20):
# Since pixels_alpha gave me a uint8 array, I infer that
# alpha=255 means opaque. (Right?)
alphas[i,j] = 200
pg.image.save(surf, '/home/steve/Desktop/test.png')
make_semitransparent_image()
繰り返しますが、画像を保存しますが、設定した値に関係なく、画像は完全に透明に見えますalphas
。ここで何が間違っていますか?(Python 2.7、pygame 1.9.1release を使用) (事前に感謝します!)