from PIL importImage
im =Image.open('dead_parrot.jpg')# Can be many different formats.
pix = im.load()print im.size # Get the width and hight of the image for iterating overprint pix[x,y]# Get the RGBA Value of the a pixel of an image
pix[x,y]= value # Set the RGBA Value of the image (tuple)
im.save('alive_parrot.png')# Save the modified pixels as .png
It’s probably best to use the Python Image Library to do this which I’m afraid is a separate download.
The easiest way to do what you want is via the load() method on the Image object which returns a pixel access object which you can manipulate like an array:
from PIL import Image
im = Image.open('dead_parrot.jpg') # Can be many different formats.
pix = im.load()
print im.size # Get the width and hight of the image for iterating over
print pix[x,y] # Get the RGBA Value of the a pixel of an image
pix[x,y] = value # Set the RGBA Value of the image (tuple)
im.save('alive_parrot.png') # Save the modified pixels as .png
Alternatively, look at ImageDraw which gives a much richer API for creating images.
Here is my working code snippet printing the pixel colours from an
image:
import os, sys
import Image
im = Image.open("image.jpg")
x = 3
y = 4
pix = im.load()
print pix[x,y]
回答 4
photo =Image.open('IN.jpg')#your image
photo = photo.convert('RGB')
width = photo.size[0]#define W and H
height = photo.size[1]for y in range(0, height):#each pixel has coordinates
row =""for x in range(0, width):
RGB = photo.getpixel((x,y))
R,G,B = RGB #now you can use the RGB value
photo = Image.open('IN.jpg') #your image
photo = photo.convert('RGB')
width = photo.size[0] #define W and H
height = photo.size[1]
for y in range(0, height): #each pixel has coordinates
row = ""
for x in range(0, width):
RGB = photo.getpixel((x,y))
R,G,B = RGB #now you can use the RGB value
Image manipulation is a complex topic, and it’s best if you do use a library. I can recommend gdmodule which provides easy access to many different image formats from within Python.
There’s a really good article on wiki.wxpython.org entitled Working With Images. The article mentions the possiblity of using wxWidgets (wxImage), PIL or PythonMagick. Personally, I’ve used PIL and wxWidgets and both make image manipulation fairly easy.
from pygame import surfarray, image, display
import pygame
import numpy #important to import
pygame.init()
image = image.load("myimagefile.jpg")#surface to render
resolution =(image.get_width(),image.get_height())
screen = display.set_mode(resolution)#create space for display
screen.blit(image,(0,0))#superpose image on screen
display.flip()
surfarray.use_arraytype("numpy")#important!
screenpix = surfarray.pixels3d(image)#pixels in 3d array:#[x][y][rgb]for y in range(resolution[1]):for x in range(resolution[0]):for color in range(3):
screenpix[x][y][color]+=128#reverting colors
screen.blit(surfarray.make_surface(screenpix),(0,0))#superpose on screen
display.flip()#update displaywhile1:print finished
You can use pygame‘s surfarray module. This module has a 3d pixel array returning method called pixels3d(surface). I’ve shown usage below:
from pygame import surfarray, image, display
import pygame
import numpy #important to import
pygame.init()
image = image.load("myimagefile.jpg") #surface to render
resolution = (image.get_width(),image.get_height())
screen = display.set_mode(resolution) #create space for display
screen.blit(image, (0,0)) #superpose image on screen
display.flip()
surfarray.use_arraytype("numpy") #important!
screenpix = surfarray.pixels3d(image) #pixels in 3d array:
#[x][y][rgb]
for y in range(resolution[1]):
for x in range(resolution[0]):
for color in range(3):
screenpix[x][y][color] += 128
#reverting colors
screen.blit(surfarray.make_surface(screenpix), (0,0)) #superpose on screen
display.flip() #update display
while 1:
print finished
I hope been helpful. Last word: screen is locked for lifetime of screenpix.
import PIL
importImage
FILENAME='fn.gif'#image can be in gif jpeg or png format
im=Image.open(FILENAME).convert('RGB')
pix=im.load()
w=im.size[0]
h=im.size[1]for i in range(w):for j in range(h):print pix[i,j]
install PIL using the command “sudo apt-get install python-imaging” and run the following program. It will print RGB values of the image. If the image is large redirect the output to a file using ‘>’ later open the file to see RGB values
import PIL
import Image
FILENAME='fn.gif' #image can be in gif jpeg or png format
im=Image.open(FILENAME).convert('RGB')
pix=im.load()
w=im.size[0]
h=im.size[1]
for i in range(w):
for j in range(h):
print pix[i,j]
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img=mpimg.imread('Cricket_ACT_official_logo.png')
imgplot = plt.imshow(img)
回答 12
如果您希望以RGB颜色代码的形式包含三位数,则以下代码应能做到这一点。
i =Image.open(path)
pixels = i.load()# this is not a list, nor is it list()'able
width, height = i.size
all_pixels =[]for x in range(width):for y in range(height):
cpixel = pixels[x, y]
all_pixels.append(cpixel)
If you are looking to have three digits in the form of an RGB colour code, the following code should do just that.
i = Image.open(path)
pixels = i.load() # this is not a list, nor is it list()'able
width, height = i.size
all_pixels = []
for x in range(width):
for y in range(height):
cpixel = pixels[x, y]
all_pixels.append(cpixel)