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 Overflow
Top answer
1 of 2
28

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?

2 of 2
5

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.

🌐
Medium
medium.com › @jamalhussainshah › grayscale-to-rgb-image-converter-6606aeb42296
Grayscale to RGB Image Converter. This Python script utilizes the Pillow… | by Jamal Shah | Medium
January 24, 2024 - Grayscale to RGB Image Converter This Python script utilizes the Pillow (PIL) library to convert grayscale images to RGB format. The code defines two functions: convert_grayscale_to_rgb for …
Discussions

Grayscale PNG image incorrectly converted to RGB
I have a PNG image which incorrectly converts to RGB mode. This is a grayscale 16-bit image, presumably created in Photoshop. When I open it with Pillow, mode of an image is described as "I". I tri... More on github.com
🌐 github.com
3
June 12, 2017
matplotlib - How can I convert an RGB image into grayscale in Python? - Stack Overflow
It would have been much more useful ... PIL/Pillow) for SciPy (which does). Nonetheless, thanks for the dedicated benchmarking! The discernable slowdown imposed by SciKit is fascinating... and horrifying. 2017-11-16T07:47:58.483Z+00:00 ... Furthermore, in case you want to read the image as RGB, do some processing and then convert to Gray Scale you could use cvtcolor from OpenCV: gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) ... Ftr: The 0 flag is cv2.CV_LOAD_IMAGE_GRAYSCALE... More on stackoverflow.com
🌐 stackoverflow.com
Converting I;16B PNG to RGB ruins the image
Hello! I have a image grayscale image (spectrogram) that looks fine on 32bit (F), but when i try to convert it to RGB, most data is loss, that means the image is almost fully black (majority of the white/gray parts turned to black!), wou... More on github.com
🌐 github.com
4
January 26, 2022
Convert grayscale images to RGB
Hello, I am trying to classify ImageNet using vgg and I am using a custom dataset as follows train_dataset=CustomDataset(csv_file='/home/tboonesifuentes/Databases/ImageNet/Train/train.csv',root_dir='/home/tboonesifuent… More on discuss.pytorch.org
🌐 discuss.pytorch.org
12
0
March 2, 2021
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-pil-imageops-greyscale-method
Python PIL | ImageOps.grayscale() method - GeeksforGeeks
October 27, 2021 - PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The ImageOps module contains a number of ‘ready-made’ image processing operations. This module is somewhat experimental, and most operators only work on L and RGB images. ImageOps.grayscale() Convert the image to grayscale.
🌐
GitHub
github.com › python-pillow › Pillow › issues › 2574
Grayscale PNG image incorrectly converted to RGB · Issue #2574 · python-pillow/Pillow
June 12, 2017 - I have a PNG image which incorrectly converts to RGB mode. This is a grayscale 16-bit image, presumably created in Photoshop. When I open it with Pillow, mode of an image is described as "I". I tried to convert it by calling: from PIL im...
Author   nomarek
🌐
Pillow Documentation
pillow.readthedocs.io › en › stable › reference › Image.html
Image module - Pillow (PIL Fork) 12.2.0 documentation
The default method of converting a grayscale (“L”) or “RGB” image into a bilevel (mode “1”) image uses Floyd-Steinberg dither to approximate the original image luminosity levels.
🌐
Codecademy
codecademy.com › docs › python:pillow › image module › .convert()
Python:Pillow | Image Module | .convert() | Codecademy
March 21, 2025 - RGBA: 32-bit true color with transparency, for smooth blending. CMYK: 32-bit color separation, used in printing. YCbCr: 24-bit color video format, commonly used in video encoding. I: 32-bit signed integer, provides high precision. F: 32-bit floating point, offers accuracy and a wide dynamic range. In this example, the .convert() method uses the L mode to convert an image to grayscale...
Find elsewhere
🌐
LinkedIn
linkedin.com › pulse › grayscale-rgb-image-converter-jamal-shah-4neuf
Grayscale to RGB Image Converter
January 24, 2024 - Description: This Python script utilizes the Pillow (PIL) library to convert grayscale images to RGB format. The code defines two functions: convert_grayscale_to_rgb for converting a single grayscale image to RGB, and process_images_in_directory ...
🌐
HolyPython
holypython.com › home › python pil tutorial › how to convert an image to black & white in python (pil)
How to convert an image to Black & White in Python (PIL) | HolyPython.com
December 10, 2022 - Steps for manually manipulating pixels to achieve a grayscale image from a color image: We will open a color image. We will get each pixel’s data from it as tuples using .getdata() method. We will iterate through the list of tuples and average each pixel’s RGB (24 bit) value to one single lumination value.
🌐
GitHub
github.com › python-pillow › Pillow › issues › 5991
Converting I;16B PNG to RGB ruins the image · Issue #5991 · python-pillow/Pillow
January 26, 2022 - Hello! I have a image grayscale image (spectrogram) that looks fine on 32bit (F), but when i try to convert it to RGB, most data is loss, that means the image is almost fully black (majority of the white/gray parts turned to black!), wou...
Author   clairerity
🌐
Brandonrohrer
brandonrohrer.com › convert_rgb_to_grayscale.html
How to Convert an RGB Image to Grayscale
If close is good enough or if you really care about speed, use the linear approximation of gamma correction. This is the approach used by MATLAB, Pillow, and OpenCV. It is included in my Lodgepole image and video processing toolbox: import lodgepole.image_tools as lit gray_img = lit.rgb2gray_approx(color_img)
🌐
Python Examples
pythonexamples.org › pillow-convert-image-to-grayscale
Convert Image to Grayscale in Pillow
Learn how to convert a color image to grayscale using the Pillow library in Python. This tutorial includes step-by-step instructions and code examples to effectively use the Image.convert() function.
🌐
Brandonrohrer
brandonrohrer.com › convert_rgb_to_grayscale
How to Convert an RGB Image to Grayscale - Brandon Rohrer
If close is good enough or if you really care about speed, use the linear approximation of gamma correction. This is the approach used by MATLAB, Pillow, and OpenCV. It is included in my Lodgepole image and video processing toolbox: import lodgepole.image_tools as lit gray_img = lit.rgb2gray_approx(color_img)
🌐
Quora
quora.com › How-do-I-convert-a-grayscale-image-to-an-RGB-image-in-Python
How to convert a grayscale image to an RGB image in Python - Quora
Answer (1 of 3): If you’re asking for a simple method the answer is no. Or what you’re asking is simply merging r,g,b channels together the answer is in the next section Let me explain Simply take an image containing an rainbow, it is very easy to a human to identify to say “it a rainbow ...
🌐
Reddit
reddit.com › r/learnpython › pil pillow paste a color picture into a grayscale result in a grayscale
r/learnpython on Reddit: PIL pillow paste a color picture into a grayscale result in a grayscale
August 6, 2020 -

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))

/>

🌐
GitHub
github.com › python-pillow › Pillow › issues › 3800
Incorrect Grayscale Conversion · Issue #3800 · python-pillow/Pillow
April 18, 2019 - It is very hard to detect this difference by just looking at the grayscale ouputs but I am including them for completeness. The difference becomes extremely apparent under aggressive JPEG compression. ... im = np.asarray(Image.open(args.input).convert('L')) im2 = cv2.cvtColor(cv2.imread(args.input), cv2.COLOR_BGR2GRAY) diff = im - im2 cv2.imwrite('pillow_output.png', im) cv2.imwrite('opencv_output.png', im2) cv2.imwrite('diff.png', diff)
Author   Queuecumber
🌐
Physics Forums
physicsforums.com › other sciences › programming and computer science
Converting grayscale images to RGB with Tensorflow • Physics Forums
January 9, 2023 - To convert a grayscale image to an RGB image using the PIL library, you can use the convert method of the Image class and pass in the 'RGB' mode as an argument. For example: ... from PIL import Image # Open the grayscale image image_grayscale ...
🌐
TutorialsPoint
tutorialspoint.com › python_pillow › python_pillow_converting_color_string_to_grayscale_values.htm
Python Pillow - Converting color string to Grayscale values
from PIL import ImageColor #Get the RGB value of the color "blue" blue_rgb = ImageColor.getcolor("blue", mode = "RGB") #Get the RGBA value of the color "green" in RGBA mode green_rgba = ImageColor.getcolor("green", mode="RGBA") print("Blue (RGB):", blue_rgb) print("Green (RGBA):", green_rgba)