Getting list of pixel values from PIL - python

I'm trying to convert a black & white .jpg image into a list which I can then modulate into an audio signal.
I have imported the PIL module and am trying to call the built-in function: list(im.getdata()). When I call it, python crashes. Is there some way of breaking down the image (always 320x240) into 240 lines to make the computations easier? Or am I just calling the wrong function?

Python shouldn't crash when you call getdata(). The image might be corrupted or there is something wrong with your PIL installation. Try it with another image or post the image you are using.
This should break down the image the way you want:
from PIL import Image
im = Image.open('um_000000.png')
pixels = list(im.getdata())
width, height = im.size
pixels = [pixels[i * width:(i + 1) * width] for i in xrange(height)]

If you have numpy installed you can try:
data = numpy.asarray(im)
(I say "try" here, because it's unclear why getdata() isn't working for you, and I don't know whether asarray uses getdata, but it's worth a test.)

I assume you are getting an error like.. TypeError: 'PixelAccess' object is not iterable...?
See the Image.load documentation for how to access pixels..
Basically, to get the list of pixels in an image, using PIL:
from PIL import Image
i = Image.open("myfile.png")
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)
That appends every pixel to the all_pixels - if the file is an RGB image (even if it only contains a black-and-white image) these will be a tuple, for example:
(255, 255, 255)
To convert the image to monochrome, you just average the three values - so, the last three lines of code would become..
cpixel = pixels[x, y]
bw_value = int(round(sum(cpixel) / float(len(cpixel))))
# the above could probably be bw_value = sum(cpixel)/len(cpixel)
all_pixels.append(bw_value)
Or to get the luminance (weighted average):
cpixel = pixels[x, y]
luma = (0.3 * cpixel[0]) + (0.59 * cpixel[1]) + (0.11 * cpixel[2])
all_pixels.append(luma)
Or pure 1-bit looking black and white:
cpixel = pixels[x, y]
if round(sum(cpixel)) / float(len(cpixel)) > 127:
all_pixels.append(255)
else:
all_pixels.append(0)
There is probably methods within PIL to do such RGB -> BW conversions quicker, but this works, and isn't particularly slow.
If you only want to perform calculations on each row, you could skip adding all the pixels to an intermediate list.. For example, to calculate the average value of each row:
from PIL import Image
i = Image.open("myfile.png")
pixels = i.load() # this is not a list
width, height = i.size
row_averages = []
for y in range(height):
cur_row_ttl = 0
for x in range(width):
cur_pixel = pixels[x, y]
cur_pixel_mono = sum(cur_pixel) / len(cur_pixel)
cur_row_ttl += cur_pixel_mono
cur_row_avg = cur_row_ttl / width
row_averages.append(cur_row_avg)
print "Brighest row:",
print max(row_averages)

Or if you want to count white or black pixels
This is also a solution:
from PIL import Image
import operator
img = Image.open("your_file.png").convert('1')
black, white = img.getcolors()
print black[0]
print white[0]

pixVals = list(pilImg.getdata())
output is a list of all RGB values from the picture:
[(248, 246, 247), (246, 248, 247), (244, 248, 247), (244, 248, 247), (246, 248, 247), (248, 246, 247), (250, 246, 247), (251, 245, 247), (253, 244, 247), (254, 243, 247)]

Not PIL, but scipy.misc.imread might still be interesting:
import scipy.misc
im = scipy.misc.imread('um_000000.png', flatten=False, mode='RGB')
print(im.shape)
gives
(480, 640, 3)
so it is (height, width, channels). So you can iterate over it by
for y in range(im.shape[0]):
for x in range(im.shape[1]):
color = tuple(im[y][x])
r, g, b = color

data = numpy.asarray(im)
Notice:In PIL, img is RGBA. In cv2, img is BGRA.
My robust solution:
def cv_from_pil_img(pil_img):
assert pil_img.mode=="RGBA"
return cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGBA2BGRA)

As I commented above, problem seems to be the conversion from PIL internal list format to a standard python list type. I've found that Image.tostring() is much faster, and depending on your needs it might be enough. In my case, I needed to calculate the CRC32 digest of image data, and it suited fine.
If you need to perform more complex calculations, tom10 response involving numpy might be what you need.

Looks like PILlow may have changed tostring() to tobytes(). When trying to extract RGBA pixels to get them into an OpenGL texture, the following worked for me (within the glTexImage2D call which I omit for brevity).
from PIL import Image
img = Image.open("mandrill.png").rotate(180).transpose(Image.FLIP_LEFT_RIGHT)
# use img.convert("RGBA").tobytes() as texels

Related

Putpixel function not generating all the pixels

My goal is to generate a color per pixel in order to fill up the whole canvas however the image generated always turns out black with only one of its pixels changed color, I can't seem to figure what I'm doing wrong.
import random
from PIL import Image
canvas = Image.new("RGB", (300,300))
y = random.randint(1, canvas.width)
x = random.randint(1, canvas.width)
r = random.randint(0,255)
g = random.randint(0,255)
b = random.randint(0,255)
rgb = (r,g,b)
for i in range(canvas.width):
canvas.putpixel((x,y), (rgb))
canvas.save("test.png", "PNG")
print("Image saved successfully.")
You really should try and avoid using for loops in any Python image processing - they are slow and error-prone.
The easiest and fastest way to make a random image is using vectorised Numpy functions like this:
import numpy as np
from PIL import Image
# Create Numpy array 300x300x3 of random uint8
data = np.random.randint(0, 256, (300,300,3), dtype=np.uint8)
# Make into PIL Image
im = Image.fromarray(data)
The problem with your code is that you are not iterating over every pixel. I've modified your code to iterate over every pixel, check whether or not it is black (0,0,0), then place a pixel on that iteration with your randomly-generated rgb value. Then, I regenerate 3 new random numbers and place them back into the rgb tuple causing the next pixel in the loop to have a different rgb value.
The x and y definitions are redundant, as you want a random color for every pixel but do not want random pixels, so I have removed them. I added a declaration, pixels = canvas.load() which allocates memory for the pixels so you can iterate over them and change each individual color. I heavily relied on this similar stackoverflow question, if you want further information. Here is my code:
canvas = Image.new("RGB", (300,300))
pixels = canvas.load()
width, height = canvas.size
for i in range(width):
for j in range(height):
if pixels[i,j] == (0,0,0):
r = random.randint(0,255)
g = random.randint(0,255)
b = random.randint(0,255)
rgb = (r,g,b)
canvas.putpixel((i,j), (rgb))
canvas.save("test.png", "PNG")
print("Image saved successfully.")
Here is the output produced:

How do you use masks from pngs with moviepy?

I am trying to use masks with moviepy.
I would like to use an image file like this (http://imgur.com/1IGHflk) and overlay it as a mask on top of a video, so that only the black part of the image is transparent in the video.
I am able to get this to work when I make the mask image exactly the same size as the video, but I can't figure out how to make it work with a mask image which is smaller. I would like to take the small mask image given, and position it onto the video (further I would actually like to take this mask image and put it onto a video as a mask in few different locations at once).
I've included the code below where I tried to do this as well as the error I get. Any help is appreciated (or just a link to a simple example showing how to use moviepy masks). Thank you!
from moviepy.editor import VideoFileClip, concatenate_videoclips, ImageClip, vfx, AudioFileClip, \
concatenate_audioclips, CompositeAudioClip, ColorClip, CompositeVideoClip, VideoClip
import random
if __name__ == '__main__':
DEFAULT_CLIP_HEIGHT = 720
DEFAULT_CLIP_WIDTH = 1280
color_clip = ColorClip(size=(DEFAULT_CLIP_WIDTH, DEFAULT_CLIP_HEIGHT), col=((0, 0, 255)))
color_clip = color_clip.set_duration(15)
color_clip = color_clip.set_fps(DEFAULT_FPS)
color_clip2 = ColorClip(size=(DEFAULT_CLIP_WIDTH, DEFAULT_CLIP_HEIGHT), col=((255, 0, 0)))
color_clip2 = color_clip2.set_duration(15)
color_clip2 = color_clip2.set_fps(DEFAULT_FPS)
rounded_border_path = os.path.join(PROJECT_PATH, 'black_on_white_small.png')
mask_clip = ImageClip(rounded_border_path)
mask_clip = mask_clip.set_duration(10)
mask_clip = mask_clip.set_fps(DEFAULT_FPS)
mask_clip = mask_clip.set_position(('center', 'center'))
comp_clip = CompositeVideoClip([mask_clip], bg_color=(255, 255, 255),
size=(DEFAULT_CLIP_WIDTH, DEFAULT_CLIP_HEIGHT), ismask=True)
color_clip.mask = comp_clip
comp_clip = CompositeVideoClip([color_clip2, color_clip], size=(DEFAULT_CLIP_WIDTH, DEFAULT_CLIP_HEIGHT))
o_path = os.path.join(PROJECT_PATH, 'comp_color.mp4')
comp_clip.write_videofile(o_path, fps=DEFAULT_FPS, codec='libx264')
yields error:
File "/Users/maxfowler/Dropbox/desktop/cs/upwork_montage/custom_moviepy/moviepy/video/compositing/CompositeVideoClip.py", line 110, in make_frame
f = c.blit_on(f, t)
File "/Users/maxfowler/Dropbox/desktop/cs/upwork_montage/custom_moviepy/moviepy/video/VideoClip.py", line 570, in blit_on
return blit(img, picture, pos, mask=mask, ismask=self.ismask)
File "/Users/maxfowler/Dropbox/desktop/cs/upwork_montage/custom_moviepy/moviepy/video/tools/drawing.py", line 43, in blit
1.0 * mask * blitted + (1.0 - mask) * blit_region)
ValueError: operands could not be broadcast together with shapes (720,1280,9) (720,1280,3)
It seems that you are using a whole image (height x width x 3) as a mask. Masks in MoviePy must be (height x width x 1) with float values between 0 (transparent) and 1 (totally opaque).
Does that help ?
For anyone googling this now that it's year's later, your mask image needs to be greyscale rather than full colour.
This includes the inserting of the background colour
bg_color=(255, 255, 255)
There should only be one variable, rather than the three for RGB. eg:
bg_color=(255)

Vertically fade image to transparency using Python PIL library

I have looked at tutorials, other stackoverflow questions and the PIL documentation itself, but I'm still still not sure how to do it.
I'd like to start fading an image vertically at approximately 55% down the y-axis, and have the image completely transparent at approximately 75%. It's important that I preserve the full height of the image, even though the last 25% or so should be completely transparent.
Is this possible to do with PIL?
Sure it's doable.
Let's assume that you're starting off with an image with no transparency (because otherwise, your question is ambiguous).
Step 1: Add an alpha plane. That's just putalpha, unless you're dealing with a non-planar image, in which case you'll need to convert it to RGB or L first.
Step 2: Iterate through the pixels you want to change by using the pixel array returned by load (or getpixel and setpixel if you have to deal with ancient versions of PIL).
Step 3: There is no step 3. Unless you count saving the image. In which case, OK, step 3 is saving the image.
from PIL import Image
im = Image.open('bird.jpg')
im.putalpha(255)
width, height = im.size
pixels = im.load()
for y in range(int(height*.55), int(height*.75)):
alpha = 255-int((y - height*.55)/height/.20 * 255)
for x in range(width):
pixels[x, y] = pixels[x, y][:3] + (alpha,)
for y in range(y, height):
for x in range(width):
pixels[x, y] = pixels[x, y][:3] + (0,)
im.save('birdfade.png')
Here the alpha drops off linearly from 255 to 0; it you want it to drop off according to a different curve, or you're using RGB16 instead of RGB8, or you're using L instead of RGB, you should be able to figure out how to change it.
If you want to do this faster, you can use numpy instead of a Python loop for step 2. Or you can reverse steps 1 and 2—construct an alpha plane in advance, and apply it all at once by passing it to putalpha instead of 255. Or… Since this took under half a second on the biggest image I had lying around, I'm not too worried about performance, unless you have to do a million of them and you want a faster version.
Using NumPy:
import numpy as np
import Image
img = Image.open(FILENAME).convert('RGBA')
arr = np.array(img)
alpha = arr[:, :, 3]
n = len(alpha)
alpha[:] = np.interp(np.arange(n), [0, 0.55*n, 0.75*n, n], [255, 255, 0, 0])[:,np.newaxis]
img = Image.fromarray(arr, mode='RGBA')
img.save('/tmp/out.png')
You have to change the code of #abarnert a bit if you want to fade image which already had a transparent background (detail):
from PIL import Image
im = Image.open('bird.jpg')
width, height = im.size
pixels = im.load()
for y in range(int(height*.55), int(height*.75)):
for x in range(width):
alpha = pixels[x, y][3]-int((y - height*.55)/height/.20 * 255) # change made here
if alpha <= 0:
alpha = 0
pixels[x, y] = pixels[x, y][:3] + (alpha,)
for y in range(y, height):
for x in range(width):
pixels[x, y] = pixels[x, y][:3] + (0,)
bg.save('birdfade.png')

PIL Check pixel that it's on with eval function

Is there any way using the eval function in PIL to run through all pixels, while checking to see what each value is? The program runs through an image to see if each pixel is a certain rgb, and if it is, then it will turn that pixel into transparency. the eval function in PIL seems it would do the job, but can my function that converts the pixels check the value of the pixel it's on? Thanks in advance.
Updated: Ahh, I see what you want to do. Here is an example using only PIL. It converts all white pixels to red with 50% alpha:
import Image
img = Image.open('stack.png').convert('RGBA')
width, _ = img.size
for i, px in enumerate(img.getdata()):
if px[:3] == (255, 255, 255):
y = i / width
x = i % width
img.putpixel((x, y), (255, 0, 0, 127))
img.save('stack-red.png')
Orig answer: Yes, the Image.eval() function lets you pass in a function which evaluates each pixel, and lets you determine a new pixel value:
import Image
img1 = Image.open('foo.png')
# replace dark pixels with black
img2 = Image.eval(img1, lambda px: 0 if px <= 64 else px)
No, eval will not pass an RGB tuple to a function. It maps a function over each band. You could, however process each band using eval and then use an ImageChops operation to logically combine the bands and get a mask that will be pixel-tuple specific.
By the way, this could be done much more cleanly and efficiently in NumPy if you are so inclined..
import numpy as np
import Image
import ImageChops
im_and = ImageChops.lighter
im = Image.open('test.png')
a = np.array(im)
R,G,B,A = im.split()
color_matches = []
for level,band in zip((255,255,255), (R,G,B)):
b = Image.eval(band, lambda px: 255-(255*(px==level)))
color_matches.append(b)
r,g,b = color_matches
mask = im_and(r, im_and(g, b))
im.putalpha(mask)
im.save('test2.png')

PIL Best Way To Replace Color?

I am trying to remove a certain color from my image however it's not working as well as I'd hoped. I tried to do the same thing as seen here Using PIL to make all white pixels transparent? however the image quality is a bit lossy so it leaves a little ghost of odd colored pixels around where what was removed. I tried doing something like change pixel if all three values are below 100 but because the image was poor quality the surrounding pixels weren't even black.
Does anyone know of a better way with PIL in Python to replace a color and anything surrounding it? This is probably the only sure fire way I can think of to remove the objects completely however I can't think of a way to do this.
The picture has a white background and text that is black. Let's just say I want to remove the text entirely from the image without leaving any artifacts behind.
Would really appreciate someone's help! Thanks
The best way to do it is to use the "color to alpha" algorithm used in Gimp to replace a color. It will work perfectly in your case. I reimplemented this algorithm using PIL for an open source python photo processor phatch. You can find the full implementation here. This a pure PIL implementation and it doesn't have other dependences. You can copy the function code and use it. Here is a sample using Gimp:
to
You can apply the color_to_alpha function on the image using black as the color. Then paste the image on a different background color to do the replacement.
By the way, this implementation uses the ImageMath module in PIL. It is much more efficient than accessing pixels using getdata.
EDIT: Here is the full code:
from PIL import Image, ImageMath
def difference1(source, color):
"""When source is bigger than color"""
return (source - color) / (255.0 - color)
def difference2(source, color):
"""When color is bigger than source"""
return (color - source) / color
def color_to_alpha(image, color=None):
image = image.convert('RGBA')
width, height = image.size
color = map(float, color)
img_bands = [band.convert("F") for band in image.split()]
# Find the maximum difference rate between source and color. I had to use two
# difference functions because ImageMath.eval only evaluates the expression
# once.
alpha = ImageMath.eval(
"""float(
max(
max(
max(
difference1(red_band, cred_band),
difference1(green_band, cgreen_band)
),
difference1(blue_band, cblue_band)
),
max(
max(
difference2(red_band, cred_band),
difference2(green_band, cgreen_band)
),
difference2(blue_band, cblue_band)
)
)
)""",
difference1=difference1,
difference2=difference2,
red_band = img_bands[0],
green_band = img_bands[1],
blue_band = img_bands[2],
cred_band = color[0],
cgreen_band = color[1],
cblue_band = color[2]
)
# Calculate the new image colors after the removal of the selected color
new_bands = [
ImageMath.eval(
"convert((image - color) / alpha + color, 'L')",
image = img_bands[i],
color = color[i],
alpha = alpha
)
for i in xrange(3)
]
# Add the new alpha band
new_bands.append(ImageMath.eval(
"convert(alpha_band * alpha, 'L')",
alpha = alpha,
alpha_band = img_bands[3]
))
return Image.merge('RGBA', new_bands)
image = color_to_alpha(image, (0, 0, 0, 255))
background = Image.new('RGB', image.size, (255, 255, 255))
background.paste(image.convert('RGB'), mask=image)
Using numpy and PIL:
This loads the image into a numpy array of shape (W,H,3), where W is the
width and H is the height. The third axis of the array represents the 3 color
channels, R,G,B.
import Image
import numpy as np
orig_color = (255,255,255)
replacement_color = (0,0,0)
img = Image.open(filename).convert('RGB')
data = np.array(img)
data[(data == orig_color).all(axis = -1)] = replacement_color
img2 = Image.fromarray(data, mode='RGB')
img2.show()
Since orig_color is a tuple of length 3, and data has
shape (W,H,3), NumPy
broadcasts
orig_color to an array of shape (W,H,3) to perform the comparison data ==
orig_color. The result in a boolean array of shape (W,H,3).
(data == orig_color).all(axis = -1) is a boolean array of shape (W,H) which
is True wherever the RGB color in data is original_color.
#!/usr/bin/python
from PIL import Image
import sys
img = Image.open(sys.argv[1])
img = img.convert("RGBA")
pixdata = img.load()
# Clean the background noise, if color != white, then set to black.
# change with your color
for y in xrange(img.size[1]):
for x in xrange(img.size[0]):
if pixdata[x, y] == (255, 255, 255, 255):
pixdata[x, y] = (0, 0, 0, 255)
You'll need to represent the image as a 2-dimensional array. This means either making a list of lists of pixels, or viewing the 1-dimensional array as a 2d one with some clever math. Then, for each pixel that is targeted, you'll need to find all surrounding pixels. You could do this with a python generator thus:
def targets(x,y):
yield (x,y) # Center
yield (x+1,y) # Left
yield (x-1,y) # Right
yield (x,y+1) # Above
yield (x,y-1) # Below
yield (x+1,y+1) # Above and to the right
yield (x+1,y-1) # Below and to the right
yield (x-1,y+1) # Above and to the left
yield (x-1,y-1) # Below and to the left
So, you would use it like this:
for x in range(width):
for y in range(height):
px = pixels[x][y]
if px[0] == 255 and px[1] == 255 and px[2] == 255:
for i,j in targets(x,y):
newpixels[i][j] = replacementColor
If the pixels are not easily identifiable e.g you say (r < 100 and g < 100 and b < 100) also doesn't match correctly the black region, it means you have lots of noise.
Best way would be to identify a region and fill it with color you want, you can identify the region manually or may be by edge detection e.g. http://bitecode.co.uk/2008/07/edge-detection-in-python/
or more sophisticated approach would be to use library like opencv (http://opencv.willowgarage.com/wiki/) to identify objects.
This is part of my code, the result would like:
source
target
import os
import struct
from PIL import Image
def changePNGColor(sourceFile, fromRgb, toRgb, deltaRank = 10):
fromRgb = fromRgb.replace('#', '')
toRgb = toRgb.replace('#', '')
fromColor = struct.unpack('BBB', bytes.fromhex(fromRgb))
toColor = struct.unpack('BBB', bytes.fromhex(toRgb))
img = Image.open(sourceFile)
img = img.convert("RGBA")
pixdata = img.load()
for x in range(0, img.size[0]):
for y in range(0, img.size[1]):
rdelta = pixdata[x, y][0] - fromColor[0]
gdelta = pixdata[x, y][0] - fromColor[0]
bdelta = pixdata[x, y][0] - fromColor[0]
if abs(rdelta) <= deltaRank and abs(gdelta) <= deltaRank and abs(bdelta) <= deltaRank:
pixdata[x, y] = (toColor[0] + rdelta, toColor[1] + gdelta, toColor[2] + bdelta, pixdata[x, y][3])
img.save(os.path.dirname(sourceFile) + os.sep + "changeColor" + os.path.splitext(sourceFile)[1])
if __name__ == '__main__':
changePNGColor("./ok_1.png", "#000000", "#ff0000")

Categories

Resources