Skip to content

Commit

Permalink
Chapter 17 finished
Browse files Browse the repository at this point in the history
  • Loading branch information
chenguohui committed Jul 12, 2017
1 parent 3217e0a commit 1689394
Show file tree
Hide file tree
Showing 23 changed files with 129 additions and 0 deletions.
8 changes: 8 additions & 0 deletions 17-image/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Pillow
`pip install pillow`
<pre>
from PIL import ImageColor
from PIL import Image
</pre>


Binary file added 17-image/catlogo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 17-image/cropped.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions 17-image/drawAndText.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from PIL import Image, ImageDraw
im = Image.new('RGBA', (200, 200), 'white')
draw = ImageDraw.Draw(im)
draw.line([(0, 0), (199, 0), (199, 199), (0, 199), (0, 0)], fill='black')
draw.rectangle((20, 30, 60, 60), fill='blue')
draw.ellipse((120, 30, 160, 60), fill='red')
draw.polygon(((57, 87), (79, 62), (94, 85), (120, 90), (103, 113)),
fill='brown')
for i in range(100, 200, 10):
draw.line([(i, 0), (200, i - 100)], fill='green')

im.save('drawing.png')
from PIL import ImageFont
import os
im = Image.new('RGBA', (200, 200), 'white')
draw = ImageDraw.Draw(im)
draw.text((20, 150), 'Hello', fill='purple')
fontsFolder = 'C:/Windows/Fonts'
arialFont = ImageFont.truetype(os.path.join(fontsFolder, 'arial.ttf'), 32)
draw.text((100, 150), 'Howdy', fill='gray', font=arialFont)
im.save('text.png')
Binary file added 17-image/drawing.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 53 additions & 0 deletions 17-image/imageOp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
catIm = Image.open('zophie.png')
width, height = catIm.size
catIm.save('zophie.jpg')
im = Image.new('RGBA', (100,200), 'purple')
im.save('purpleImage.png')
im2 = Image.new('RGBA', (20, 20))
im2.save('transparentImage.png')
croppedIm = catIm.crop((335,345, 565,560))
croppedIm.save('cropped.png')
catCopyIm = catIm.copy()
facedIm = catIm.crop((335,345, 565,560))
faceIm = catIm.crop((335,345, 565,560))
catCopyIm.paste(faceIm, (0, 0))
catCopyIm.paste(faceIm, (400, 500))
catCopyIm.save('pasted.png')
w, h = catIm.size
fw, fh = faceIm.size
catCopyTwo = catIm.copy()
for left in range(0, w, fw):
for top in range(0, h, fh):
print(left, top)
catCopyTwo.paste(faceIm, (left, top))

catCopyTwo.save('tiled.png')
qIm = catIm.resize((int(w/2), int(h/2)))

qIm.save('quartersized.png')
sveltedIm = catIm.resize((w, h+300))

sveltedIm.save('svelte.png')
catIm.rotate(90).save('rotated90.png')
catIm.rotate(180).save('rotated180.png')
catIm.rotate(270).save('rotated270.png')
catIm.rotate(6).save('rotated6.png')
catIm.rotate(6, expand=True).save('rotated6_expanded.png')
im = Image.new('RGBA', (100, 100))
im.getpixel((0,0))
(0, 0, 0, 0)
for x in range(100):
for y in range(50):
im.putpixel((x, y), (210, 210, 210))

for x in range(100):
for y in range(50, 100):
im.putpixel((x, y), ImageColor.getcolor('darkgray', 'RGBA'))


from PIL import ImageColor
for x in range(100):
for y in range(50, 100):
im.putpixel((x, y), ImageColor.getcolor('darkgray', 'RGBA'))

im.save('putPixel.png')
Binary file added 17-image/pasted.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions 17-image/png2jpg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from PIL import Image

catIm = Image.open('zophie.png')
catIm.save('zophie.jpg')
Binary file added 17-image/purpleImage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 17-image/putPixel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 17-image/quartersized.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 43 additions & 0 deletions 17-image/resizeAndAddLogo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#! python3
# resizeAndAddLogo.py - Resizes all images in current working directory to fit
# in a 300x300 square, and adds catlogo.png to the lower-right corner.

import os
from PIL import Image

SQUARE_FIT_SIZE = 300
LOGO_FILENAME = 'catlogo.png'

logoIm = Image.open(LOGO_FILENAME)
logoWidth, logoHeight = logoIm.size

os.makedirs('withLogo', exist_ok=True)
# Loop over all files in the working directory.
for filename in os.listdir('.'):
if not (filename.endswith('.png') or filename.endswith('.jpg')) \
or filename == LOGO_FILENAME:
continue # skip non-image files and the logo file itself

im = Image.open(filename)
width, height = im.size

# Check if image needs to be resized.
if width > SQUARE_FIT_SIZE and height > SQUARE_FIT_SIZE:
# Calculate the new width and height to resize to.
if width > height:
height = int((SQUARE_FIT_SIZE / width) * height)
width = SQUARE_FIT_SIZE
else:
width = int((SQUARE_FIT_SIZE / height) * width)
height = SQUARE_FIT_SIZE

# Resize the image.
print('Resizing %s...' % (filename))
im = im.resize((width, height))

# Add logo.
print('Adding logo to %s...' % (filename))
im.paste(logoIm, (width - logoWidth, height - logoHeight), logoIm)

# Save changes.
im.save(os.path.join('withLogo', filename))
Binary file added 17-image/rotated180.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 17-image/rotated270.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 17-image/rotated6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 17-image/rotated6_expanded.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 17-image/rotated90.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 17-image/svelte.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 17-image/text.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 17-image/tiled.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 17-image/transparentImage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 17-image/zophie.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 17-image/zophie.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1689394

Please sign in to comment.