670 likes | 810 Views
Digital Image M anipulation -2012 CS4HS @ UCA. References: Introduction to computing and programming in Python – a multimedia approach by Mark Guzdial and Barbara Ericson JES: Jython Environment for Students , http://code.google.com/p/mediacomp-jes/
E N D
Digital Image Manipulation -2012 CS4HS @ UCA References: Introduction to computing and programming in Python – a multimedia approach by Mark Guzdial and Barbara Ericson JES: Jython Environment for Students, http://code.google.com/p/mediacomp-jes/ http://nifty.stanford.edu/2011/parlante-image-puzzle/ by Nick Parlante
Outline of tasks 0. JES Introduction 1. Load and investigate details of an image 2. Alternate pixel RGB values 3. Solve image puzzles 4. Embed information in a picture 5. Fix the Temple of Hephaestus virtually 6. Land Mark on the moon 2012 CS4HS @ UCA
Outline of tasks 0. JES Introduction: 1.Load and investigate details of an image 2. Alternate pixel RGB values 3. Solve image puzzles 4. Embed information in a picture 5. Fix the Temple of Hephaestus virtually 6. Land Mark on the moon 2012 CS4HS @ UCA
JES: JythonEnvironment for Students • JES has a set of functions to manipulate media data (digital image and sounds) • Let us open the environment 2012 CS4HS @ UCA
JES 4.3 2012 CS4HS @ UCA
Outline of tasks 0. JES Introduction: 1. Load and investigate details of an image 2.Alternate pixel RGB values 3. Solve image puzzles 4. Embed information in a picture 5. Fix the Temple of Hephaestus virtually 6. Land Mark on the moon 2012 CS4HS @ UCA
Pack a file to work with 2012 CS4HS @ UCA
Let us pick the beach.jpg 2012 CS4HS @ UCA
Let the computer remember it • Assign a name to the file we can then refer and use it later. myFile = pickAFile() 2012 CS4HS @ UCA
View the picture • Let us make myFile an picture object myPict = makePicture(myFile) • Does print myPict work? print(myPict) • View myPict with show(myPict) 2012 CS4HS @ UCA
Make a function: pick and show • We do not want to repeatedly type the same commands to load and show a picture every time • Let us create it once and be able to use it whenever needed 2012 CS4HS @ UCA
pickAndShow() 2012 CS4HS @ UCA
Let us save it • File -> Save Program • Specify the location (your USB drive), and give it a name (say prog1) • Exit JES • Open JES again • File -> Open Program • Select prog1 • It comes back 2012 CS4HS @ UCA
Load program and use it • Click “Load Program” • Then, in the command line area type pict1 = pickAndShow() pict2 = pickAndShow() 2012 CS4HS @ UCA
Investigate the details • Now let us investigate the details of a digital picture • Pick “MediaTools” from the menu bar, then select “Picture Tool …” 2012 CS4HS @ UCA
(0, 0) (639, 0) x y (639, 479) (0, 479) 2012 CS4HS @ UCA
Outline of tasks 0. JES Introduction: 1. Load and investigate details of an image 2. Alternate pixel RGB values 3. Solve image puzzles 4. Embed information in a picture 5. Fix the Temple of Hephaestus virtually 6. Land Mark on the moon 2012 CS4HS @ UCA
Get a pixel and find its color value 2012 CS4HS @ UCA
Change color value of a pixel 2012 CS4HS @ UCA
Image manipulation • It is all about changing color values of pixels • The process should be: • get a specific pixel first, and • then reset its color. • Do we want to type all the command lines above for every pixel again and again? • The picture has 640 x 480 = 307,200 pixels. • How should we do it? • For example, how do we zero out red values for all pixels? 2012 CS4HS @ UCA
JES Functions • JES Functions -> Pictures -> getPixels • By using the function getPixels, we can get all pixels at once. • We then loop through each pixel to zero out its red value. • Let us open our prog1 and create another function named ZeroRed 2012 CS4HS @ UCA
Now let us use it • Load Program • Pick a picture • Zero red out for all pixels in the picture • View the changes with Picture Tool … 2012 CS4HS @ UCA
Work with the pictureModule • We provide you a collection of functions in the pictureModule.py • Open and load the module • Here are some available functions: • pickAndShow() • changeRed( pict, alpha ) • changeGreen( pict, alpha ) • changeBlue( pict, alpha ) • …… 2012 CS4HS @ UCA
chageRed(pict, alpha) defchangeRed( pict, alpha ): allPixels = getPixels( pict ) for p in allPixels: value = alpha * getRed( p ) if value > 255: value = 255 setRed( p, value) 2012 CS4HS @ UCA
Change values of Green and Blue • changeGreen( pict, alpha) • changeBlue ( pict, alpha) 2012 CS4HS @ UCA
Outline of tasks 0. JES Introduction: 1. Load and investigate details of an image 2. Alternate pixel RGB values 3. Solve image puzzles 4. Embed information in a picture 5. Fix the Temple of Hephaestus virtually 6. Land Mark on the moon 2012 CS4HS @ UCA
Solve a puzzle • Load the image copper-puzzel.png pic = pickAndShow() • A picture is hidden in. • Let’s find it? 2012 CS4HS @ UCA
Solution • Zero out redness • Increase greenness 20 times for all pixels • Increase the blueness 20 times for all pixels 2012 CS4HS @ UCA
Solve two more puzzles • iron-puzzle.png, and Hint: Increase redness 15 times for all pixels • canvas.png Hint: Increase blueness 16 times 2012 CS4HS @ UCA
How do we create a puzzle? • Zero out R, G, or B • Weak the remaining color • Add randomly generated values for the zero out color 2012 CS4HS @ UCA
Outline of tasks 0. JES Introduction: 1. Load and investigate details of an image 2. Alternate pixel RGB values 3. Solve image puzzles 4. Embed information in a picture 5. Fix the Temple of Hephaestus virtually 6. Land Mark on the moon 2012 CS4HS @ UCA
Hiding a message in a picture (steganography) A scenic picture (original) This picture with a hidden message 2012 CS4HS @ UCA
Problem specification • Inputs: • A cover image • A message • Outputs: • An image seems exactly as the original but with the message embedded • From the image, one can recover the embedded message 2012 CS4HS @ UCA
Solution design • To embed the message, we need to identify the position of each pixel of the message and remember them. • To remember the position of a pixel, we only need to change the original very little such that the altered image seem exactly as the original • When we need to reveal the message, we only need to print the pixels at the positions remembered. 2012 CS4HS @ UCA
Solution design continue • To identify the position of each pixel of the message, we check if a pixel is close to black (or whatever color the text is). • To remember the position of a pixel, we do the followings: • Preparation: Make the red (or green, blue) value of each pixel even. • Memorization: For each message pixel, add one to the red value (become odd now). • Note: These two steps change the original very little • To reveal the message, we only need to print the pixels whose red value is odd. 2012 CS4HS @ UCA
Implementation: encode def stegano(msgPic, coverPic): for pxl in getPixels(coverPic): r = getRed(pxl) if r%2 ==1: setRed(pxl, r-1) for x in range(1, getWidth(coverPic)): for y in range(1, getHeight(coverPic)): msgPxl = getPixel(msgPic, x, y) coverPxl = getPixel(coverPic, x, y) if distance(getColor(msgPxl), black) < 100.0: setRed(coverPxl, getRed(coverPxl) +1) return coverPic 2012 CS4HS @ UCA
Implementation: decode def deStega(segaPic): w = getWidth(segaPic) h = getHeight(segaPic) msg = makeEmptyPicture(w, h) for x in range(0,w): for y in range(0, h): encPxl = getPixel(segaPic, x, y) msgPxl = getPixel(msg, x, y) if (getRed(encPxl)%2) ==1: setColor(msgPxl, black) show(msg) return msg 2012 CS4HS @ UCA
Integration and maintenance • The application should be either to encode a message in a cover image or discover the message from an encoded image. • For the former, we need to pick a cover image, then a message as an image. Then, encode it. • For the later, we need to recover the message from its cover image. 2012 CS4HS @ UCA
Let us do it • Open and load the pictureModule • Load two pictures, one is the cover image and the other is the text image, with coverPic = pickAndShow() msg = pickAndShow() • Embed the message in the cover image secret = stegano (coverPic, msg) • Save it with JES Functions -> Pictures -> writePictureTo • Note: use the .png extension not .jpg 2012 CS4HS @ UCA
Recover the message • Load the picture that you just saved pic = pickAndShow() • The, decipher it with deStega(pic) 2012 CS4HS @ UCA
Outline of tasks 0. JES Introduction: 1. Load and investigate details of an image 2. Alternate pixel RGB values 3. Solve image puzzles 4. Embed information in a picture 5. Fix the Temple of Hephaestus virtually 6. Land Mark on the moon 2012 CS4HS @ UCA
Let us fix the temple in Athens • What is the problem specification? • How do we design a solution? • Implement it • Test and refine the computational solution 2012 CS4HS @ UCA
How do we fix it? • Due the symmetry, we may flip the left half to the right • How do we do it? 2012 CS4HS @ UCA
Try to fix it with reflection 2012 CS4HS @ UCA
Mirroring a picture -vertically 2012 CS4HS @ UCA
Specification • The center line is x = midpoint = width/2. • The x-coordinate of a pixel on the left half is: width/2 - offset • The x-coordinate of a pixel on the right half is: width/2 + offset 2012 CS4HS @ UCA