I am trying to set float value to pixels in an image. But the value as shown after that is different from the value that has been set. It seems this is a precision problem as mention in this answer. My question is are there any way to set the value of the pixel to an exact number (precision reduce is ok).
In my case, 0.11111111 turns into 0.111111119389534. So the difference are small but after a few calculations, it could becomes a huge margin.
Here is my test code in Blender 2.8:
import bpy
import gpu
import bgl
import random
from mathutils import Matrix
from gpu_extras.batch import batch_for_shader
import numpy as np
from math import *
# from gpu_extras.presets import draw_circle_2d
import time
IMAGE_NAME = "Generated Image"
WIDTH = 4
HEIGHT = 4
# print(buffer)
# push buffer to blender image
if IMAGE_NAME in bpy.data.images:
image = bpy.data.images[IMAGE_NAME]
if image:
bpy.data.images.remove(image)
bpy.data.images.new(IMAGE_NAME, WIDTH, HEIGHT, alpha=False, float_buffer=True, is_data=True)
else:
bpy.data.images.new(IMAGE_NAME, WIDTH, HEIGHT, alpha=False, float_buffer=True, is_data=True)
image = bpy.data.images[IMAGE_NAME]
pixels = list(image.pixels)
#FIXED: set data
for i in range(0, WIDTH*HEIGHT*4, 4):
pixels[i] = 0.11111111
pixels[i+1] = 0.22222222
pixels[i+2] = 0.33333333
pixels[i+3] = 0.44444444
image.pixels[:] = pixels
image.update()
print(list(image.pixels))