what about:
img = Image.open(GIF_FILENAME)
rgbimg = Image.new("RGBA", img.size)
rgbimg.paste(img)
rgbimg.save('foo.jpg')
[EDIT]
created a test:
from PIL import Image
from collections import defaultdict
import pprint
img = Image.open("kEwfFs3.png")
rgbimg = Image.new("RGBA", img.size)
rgbimg.paste(img)
found_colors = defaultdict(int)
for x in range(0, rgbimg.size[0]):
for y in range(0, rgbimg.size[1]):
pix_val = rgbimg.getpixel((x, y))
found_colors[pix_val] += 1
pprint.pprint(dict(found_colors))
rgbimg.save('kEwfFs3.jpg')
And that outputs:
{(0, 0, 0, 255): 747802,
(1, 1, 1, 255): 397,
(2, 2, 2, 255): 299,
(3, 3, 3, 255): 255,
(4, 4, 4, 255): 221,
(5, 5, 5, 255): 200,
(6, 6, 6, 255): 187,
(7, 7, 7, 255): 138,
(8, 8, 8, 255): 160,
(9, 9, 9, 255): 152,
(10, 10, 10, 255): 122,
(11, 11, 11, 255): 116,
(12, 12, 12, 255): 144,
(13, 13, 13, 255): 117,
(14, 14, 14, 255): 117,
(15, 15, 15, 255): 102,
(16, 16, 16, 255): 119,
(17, 17, 17, 255): 299641,
(18, 18, 18, 255): 273,
(19, 19, 19, 255): 233,
.................... etc .......
.................... etc .......
(249, 249, 249, 255): 616,
(250, 250, 250, 255): 656,
(251, 251, 251, 255): 862,
(252, 252, 252, 255): 1109,
(253, 253, 253, 255): 1648,
(254, 254, 254, 255): 2964175}
Which is what you would expect. Is your output different?
Answer from RickyA on Stack Overflowwhat about:
img = Image.open(GIF_FILENAME)
rgbimg = Image.new("RGBA", img.size)
rgbimg.paste(img)
rgbimg.save('foo.jpg')
[EDIT]
created a test:
from PIL import Image
from collections import defaultdict
import pprint
img = Image.open("kEwfFs3.png")
rgbimg = Image.new("RGBA", img.size)
rgbimg.paste(img)
found_colors = defaultdict(int)
for x in range(0, rgbimg.size[0]):
for y in range(0, rgbimg.size[1]):
pix_val = rgbimg.getpixel((x, y))
found_colors[pix_val] += 1
pprint.pprint(dict(found_colors))
rgbimg.save('kEwfFs3.jpg')
And that outputs:
{(0, 0, 0, 255): 747802,
(1, 1, 1, 255): 397,
(2, 2, 2, 255): 299,
(3, 3, 3, 255): 255,
(4, 4, 4, 255): 221,
(5, 5, 5, 255): 200,
(6, 6, 6, 255): 187,
(7, 7, 7, 255): 138,
(8, 8, 8, 255): 160,
(9, 9, 9, 255): 152,
(10, 10, 10, 255): 122,
(11, 11, 11, 255): 116,
(12, 12, 12, 255): 144,
(13, 13, 13, 255): 117,
(14, 14, 14, 255): 117,
(15, 15, 15, 255): 102,
(16, 16, 16, 255): 119,
(17, 17, 17, 255): 299641,
(18, 18, 18, 255): 273,
(19, 19, 19, 255): 233,
.................... etc .......
.................... etc .......
(249, 249, 249, 255): 616,
(250, 250, 250, 255): 656,
(251, 251, 251, 255): 862,
(252, 252, 252, 255): 1109,
(253, 253, 253, 255): 1648,
(254, 254, 254, 255): 2964175}
Which is what you would expect. Is your output different?
I ran into the same problem with an I;16 (16-bit grayscale) tiff, converted to RGB. Some digging into the manual reveals the problem has to do with the lut PIL is using to convert grayscale images to RGB. It is working in an 8-bit color space; that is it clips all values above 255. So a quick and simple solution is to manually convert to RGB using your own lut which scales the values within range using the point method like so:
path = 'path\to\image'
img = Image.open(path)
img.point(lambda p: p*0.0039063096, mode='RGB')
img = img.convert('RGB')
img.show() # check it out!
I determined the "lut" formula by just dividing 256 by the 16-bit equivalent, 65535.
Grayscale PNG image incorrectly converted to RGB
matplotlib - How can I convert an RGB image into grayscale in Python? - Stack Overflow
Converting I;16B PNG to RGB ruins the image
Convert grayscale images to RGB
Videos
How about doing it with Pillow:
from PIL import Image
img = Image.open('image.png').convert('L')
img.save('greyscale.png')
If an alpha (transparency) channel is present in the input image and should be preserved, use mode LA:
img = Image.open('image.png').convert('LA')
Using matplotlib and the formula
Y' = 0.2989 R + 0.5870 G + 0.1140 B
you could do:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])
img = mpimg.imread('image.png')
gray = rgb2gray(img)
plt.imshow(gray, cmap=plt.get_cmap('gray'), vmin=0, vmax=1)
plt.show()
You can also use scikit-image, which provides some functions to convert an image in ndarray, like rgb2gray.
from skimage import color
from skimage import io
img = color.rgb2gray(io.imread('image.png'))
Notes: The weights used in this conversion are calibrated for contemporary CRT phosphors: Y = 0.2125 R + 0.7154 G + 0.0721 B
Alternatively, you can read image in grayscale by:
from skimage import io
img = io.imread('image.png', as_gray=True)
Experts,
I am pasting a source image into a target image withi PIL (pillow)
The target image was converted into a grayscale picture.
The source is an colorized picture.
After pasting the result is completly grayscale.
Expectation would be that the source stays as a colorized within the target
Example code :
<from PIL import Image, ExifTags, ImageEnhance, ImageFilterimport PIL.ImageOps
target= PIL.ImageOps.grayscale(target)
target.paste(source,(0,x))
/>