logo
TOOLS TO HELP YOU LEARN
teachers, students, everyone
Tools | Blog | Account | Learn

Fun with Images and the Camera

There are few things you’ll need to do to make sure your camera module is ready to go with python code.

1) Make sure the camera is enabled
sudo raspi-config
Enable camera
click enter
You’ll need to reboot

2) install python-picamera
sudo apt-get update
sudo apt-get install python-picamera

3) another module worth installing is Pillow
sudo easy_install pip

sudo pip install pillow

4) Install quality Image viewer
sudo apt-get install imagemagick

5) lastly, feh is a good image slideshow option
sudo apt-get install feh

Take Photos with Pi Camera:
raspistill -o cam.jpg

1) Quick Python show image
from PIL import Image
import time

image = Image.open('clouds.jpg')
image.show(title='clouds',command='feh')

#time.sleep(5);
#image.close()
#image = Image.open('joys.jpg')
#image.show()

1) Python code to show an image
pygame code to display one image

==========================
import pygame

pygame.init()

display_width = 500
display_height = 300

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption(‘Photo App’)

black = (0,0,0)
white = (255,255,255)

clock = pygame.time.Clock()
crashed = False
carImg = pygame.image.load('clouds.jpg')

def car(x,y):
gameDisplay.blit(carImg, (x,y))

x = (display_width * 0.1)
y = (display_height * 0.1)

while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True

gameDisplay.fill(white)
car(x,y)


pygame.display.update()
clock.tick(60)

pygame.quit()
quit()

===========================================
one thing you may have to do is give permission
(type host + into command line on pi)
export DISPLAY=:0.0
http://www.thegeekstuff.com/2010/06/xhost-cannot-open-display/

 

2) Take photo with camera
a) sudo raspistill -o image.jpg -w 640 -h 480 2>&1
the camera preview should popup on the screen and after a few seconds a pic should be taken and stored in the current directory with the name image.jpg
b) Python Code
import picamera
from time import sleep

camera = picamera.PiCamera()
camera.start_preview()
camera.capture('image1.jpg')
sleep(5)
camera.capture('image2.jpg')
camera.stop_preview()

3) Look at all the images in a directory
import os
import glob
path = os.path.abspath(__file__)
dir_path = os.path.dirname(path)
print (dir_path)
files=glob.glob(dir_path+"/*.jpg")
for file in files:
print(file)

4) Look at pixels in an image
from PIL import Image
im = Image.open('feelface.png')
rgb_im = im.convert('RGB')
r, g, b = rgb_im.getpixel((1, 1))

print r, g, b

for num in range(0,20): #to iterate between 10 to 20
for i in range(0,20): #to iterate on the factors of the number
r, g, b = rgb_im.getpixel((num, i))
print r, g, b

================================================
IF YOU HAVE TIME EXPERIMENT WITH THE FOLLOWING:
================================================

1) OS commands
import os
os.system("sudo python scale1.py")

2) Slideshow using feh
COMMAND TO SHOW IMAGES IN FOLDER:
feh -F -Z -D 2.0