MNA
MNA

Reputation: 287

Image.save() And Image.show() Gives Different Result For same Image

I have a python script which takes a image as input and add some effect to image. After adding effect i am saving image, which gives me 100% correct result. But if do img.show() for the same Image i saved previously it shows me input Image instead of effect added Image. Code is given below

import numpy as np
from PIL import Image

img = Image.open("1.png").convert('RGBA')
arr = np.array(img)
alpha = arr[:, :, 3]
n = len(alpha)
alpha[:] = np.interp(np.arange(n), [0, 0.55*n, 0.75*n, n], [255, 255, 0, 0])[:,np.newaxis]
img = Image.fromarray(arr, mode='RGBA')
img.save("2.png")
img.show()

Upvotes: 2

Views: 962

Answers (2)

physicalattraction
physicalattraction

Reputation: 6858

I think the weird thing is not that your shown image is identical to the original image, but that your saved image is not. You are setting img2 from the variable arr, which is taken from img1 and then never changed. You should set arr[:,:,3] = alpha before creating img2.

Example:

>>> import numpy as np
>>> a = np.array([[1,1],[2,2]])
>>> a
array([[1, 1],
       [2, 2]])
>>> b = a[:,1]
>>> b
array([1, 2])
>>> b = [0,3]
>>> a
array([[1, 1],
       [2, 2]])
>>> a[:,1] = b
>>> a
array([[1, 0],
       [2, 3]])

Upvotes: 0

Afif
Afif

Reputation: 66

I am guessing its because the same image viewer is not used in both times. img.show() uses some different image viewer. Try top open both times with the same image viewer.

Upvotes: 1

Related Questions