Ask Your Question
0

Pil image colors not requested

asked 2020-06-16 11:48:43 +0100

ortollj gravatar image

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))
imgPath="/home/ortollj/Downloads/SageMath/IPYNB/"
im.save(imgPath+'tenXtenPixels.png') 
img = mpimg.imread(imgPath+"tenXtenPixels.png")
imgplot = plt.imshow(img)

image description

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-06-16 22:00:13 +0100

ortollj gravatar image

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))
imgPath="/home/ortollj/Downloads/SageMath/IPYNB/"
im.save(imgPath+'tenXtenPixels.png') 
img = mpimg.imread(imgPath+"tenXtenPixels.png")
imgplot = plt.imshow(img)

image description

edit flag offensive delete link more

Comments

to see colors coding used :

imgColorsList = Image.Image.getcolors(im)  

print(imgColorsList)
ortollj gravatar imageortollj ( 2020-06-16 22:12:16 +0100 )edit

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

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

img = mpimg.imread(imgPath+"tenXtenPixels.png")
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

ortollj gravatar imageortollj ( 2020-06-17 06:56:11 +0100 )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 !)

ortollj gravatar imageortollj ( 2020-06-17 07:24:24 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-06-16 11:48:43 +0100

Seen: 580 times

Last updated: Jun 16 '20