# Pil image colors not requested

Hi

Ubuntu 18.04 SageMath 9.1 Jupyter notebook

does anyone know why the colors are not the ones i asked for? :

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
from matplotlib.pyplot import figure
figS=2
figure(num=None, figsize=(figS, 1*figS), dpi=150, facecolor='w', edgecolor='k')
from PIL import Image, ImageColor
imSize=(10,10) #  Image size= 10x10 pixels
imageModeL=["L", "LA", "P", "PA"]
iMmode=imageModeL[0]
im = Image.new(iMmode, imSize)

for i in range(imSize[0]) :
for j in range(imSize[1]) :
im.putpixel((i,j), ImageColor.getcolor("white",iMmode))
im.putpixel((0,0), ImageColor.getcolor('red',iMmode))
im.putpixel((1,0), ImageColor.getcolor('blue', iMmode))
im.putpixel((3,3), ImageColor.getcolor('pink', iMmode))
im.putpixel( (2, 2), ImageColor.getcolor('green', iMmode))
im.save(imgPath+'tenXtenPixels.png')
imgplot = plt.imshow(img)


edit retag close merge delete

Sort by ยป oldest newest most voted

it's ok I ended up finding. I was not using the correct image mode ! ;-(

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
from matplotlib.pyplot import figure
figS=2
figure(num=None, figsize=(figS, 1*figS), dpi=150, facecolor='w', edgecolor='k')
from PIL import Image, ImageColor
imSize=(10,10) #  Image size= 10x10 pixels
imageModeL=["RGB","L", "LA", "P", "PA"]
iMmode=imageModeL[0]
im = Image.new(iMmode, imSize)

for i in range(imSize[0]) :
for j in range(imSize[1]) :
im.putpixel((i,j), ImageColor.getcolor("white",iMmode))
im.putpixel((0,0), ImageColor.getcolor('red',iMmode))
im.putpixel((1,0), ImageColor.getcolor('blue', iMmode))
im.putpixel((3,3), ImageColor.getcolor('pink', iMmode))
im.putpixel( (2, 2), ImageColor.getcolor('green', iMmode))
im.save(imgPath+'tenXtenPixels.png')
imgplot = plt.imshow(img)


more

to see colors coding used :

imgColorsList = Image.Image.getcolors(im)

print(imgColorsList)

( 2020-06-16 22:12:16 +0200 )edit

Pil doc I forgot to close im so add im.close()

imColorsList = Image.Image.getcolors(im)
print(imColorsList)
im.close()

imgplot = plt.imshow(img)


But there is just something a little odd: after a restart I must execute 2 times the code to see the image

( 2020-06-17 06:56:11 +0200 )edit

Another little odd thing, on Ubuntu 18.04 if I clik on the 10x10 file image tenXtenPixels.png , open it with the default Image Viewer, zoom to 1000% then the pixels are blur (or seem shaky !)

( 2020-06-17 07:24:24 +0200 )edit