Python 来自阵列的图像和使用点的图像
我使用Image.point和Image.fromarray对图像执行完全相同的操作,将所有像素的值一起增加相同的值。问题是我得到了完全不同的图像 使用点Python 来自阵列的图像和使用点的图像,python,image,numpy,python-imaging-library,Python,Image,Numpy,Python Imaging Library,我使用Image.point和Image.fromarray对图像执行完全相同的操作,将所有像素的值一起增加相同的值。问题是我得到了完全不同的图像 使用点 def getValue(val): return math.floor(255*float(val)/100) def func(i): return int(i+getValue(50)) out = img.point(func) 使用数组和numpy arr = np.array(np.asarray(img).
def getValue(val):
return math.floor(255*float(val)/100)
def func(i):
return int(i+getValue(50))
out = img.point(func)
使用数组和numpy
arr = np.array(np.asarray(img).astype('float'))
value = math.floor(255*float(50)/100)
arr[...,0] += value
arr[...,1] += value
arr[...,2] += value
out = Image.fromarray(arr.astype('uint8'), 'RGB')
我正在使用相同的图像(jpg)
初始图像
带点的图像
带数组的图像
它们怎么会如此不同呢?数组中的值大于255,然后将其转换为uint8。。。您希望这些值在图像中变成什么?如果希望它们为255,请先剪辑它们:
out_arr_clip = Image.fromarray(arr.clip(0,255).astype('uint8'), 'RGB')
顺便说一下,不需要单独添加到每个色带:
arr = np.asarray(img, dtype=float) # also simplified
value = math.floor(255*float(50)/100)
arr += value # the same as doing this in three separate lines
如果每个波段的值不同,您仍然可以这样做,因为:
修正它:)
没有考虑越界。所以我做了
for i in range(3):
conditions = [arr[...,i] > 255, arr[...,i] < 0]
choices = [255, 0]
arr[...,i] = np.select(conditions, choices, default=arr[...,i]
范围(3)内的i的:
条件=[arr[…,i]>255,arr[…,i]<0]
选项=[255,0]
arr[…,i]=np.select(条件,选项,默认值=arr[…,i]
工作起来很有魅力……:)数组中的值大于255,然后将其转换为uint8
。。。您希望这些值在图像中变成什么?实际上,您的图像看起来不错,但您破坏了调色板(请参阅前面的注释)。这个链接是相关的:@Apostolos是的,clip
肯定更容易,但是解决它并发布您自己的解决方案的荣誉:)谢谢您的帮助…我对这个图像处理有了新的认识,这是我第一次使用numpy。虽然arr也包含alpha通道,我不想改变它。所以我必须对每个乐队分别做,我想我必须这样做:)@Apostolos啊,我明白了;如果您只想编辑频道0,1,2
,那么您仍然可以在一行中编辑<代码>arr[…,:3]+=值
for i in range(3):
conditions = [arr[...,i] > 255, arr[...,i] < 0]
choices = [255, 0]
arr[...,i] = np.select(conditions, choices, default=arr[...,i]