Skip to content

Commit 1689394

Browse files
committed
Chapter 17 finished
1 parent 3217e0a commit 1689394

23 files changed

+129
-0
lines changed

17-image/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Pillow
2+
`pip install pillow`
3+
<pre>
4+
from PIL import ImageColor
5+
from PIL import Image
6+
</pre>
7+
8+

17-image/catlogo.png

16.3 KB
Loading

17-image/cropped.png

94.7 KB
Loading

17-image/drawAndText.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from PIL import Image, ImageDraw
2+
im = Image.new('RGBA', (200, 200), 'white')
3+
draw = ImageDraw.Draw(im)
4+
draw.line([(0, 0), (199, 0), (199, 199), (0, 199), (0, 0)], fill='black')
5+
draw.rectangle((20, 30, 60, 60), fill='blue')
6+
draw.ellipse((120, 30, 160, 60), fill='red')
7+
draw.polygon(((57, 87), (79, 62), (94, 85), (120, 90), (103, 113)),
8+
fill='brown')
9+
for i in range(100, 200, 10):
10+
draw.line([(i, 0), (200, i - 100)], fill='green')
11+
12+
im.save('drawing.png')
13+
from PIL import ImageFont
14+
import os
15+
im = Image.new('RGBA', (200, 200), 'white')
16+
draw = ImageDraw.Draw(im)
17+
draw.text((20, 150), 'Hello', fill='purple')
18+
fontsFolder = 'C:/Windows/Fonts'
19+
arialFont = ImageFont.truetype(os.path.join(fontsFolder, 'arial.ttf'), 32)
20+
draw.text((100, 150), 'Howdy', fill='gray', font=arialFont)
21+
im.save('text.png')

17-image/drawing.png

1.65 KB
Loading

17-image/imageOp.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
catIm = Image.open('zophie.png')
2+
width, height = catIm.size
3+
catIm.save('zophie.jpg')
4+
im = Image.new('RGBA', (100,200), 'purple')
5+
im.save('purpleImage.png')
6+
im2 = Image.new('RGBA', (20, 20))
7+
im2.save('transparentImage.png')
8+
croppedIm = catIm.crop((335,345, 565,560))
9+
croppedIm.save('cropped.png')
10+
catCopyIm = catIm.copy()
11+
facedIm = catIm.crop((335,345, 565,560))
12+
faceIm = catIm.crop((335,345, 565,560))
13+
catCopyIm.paste(faceIm, (0, 0))
14+
catCopyIm.paste(faceIm, (400, 500))
15+
catCopyIm.save('pasted.png')
16+
w, h = catIm.size
17+
fw, fh = faceIm.size
18+
catCopyTwo = catIm.copy()
19+
for left in range(0, w, fw):
20+
for top in range(0, h, fh):
21+
print(left, top)
22+
catCopyTwo.paste(faceIm, (left, top))
23+
24+
catCopyTwo.save('tiled.png')
25+
qIm = catIm.resize((int(w/2), int(h/2)))
26+
27+
qIm.save('quartersized.png')
28+
sveltedIm = catIm.resize((w, h+300))
29+
30+
sveltedIm.save('svelte.png')
31+
catIm.rotate(90).save('rotated90.png')
32+
catIm.rotate(180).save('rotated180.png')
33+
catIm.rotate(270).save('rotated270.png')
34+
catIm.rotate(6).save('rotated6.png')
35+
catIm.rotate(6, expand=True).save('rotated6_expanded.png')
36+
im = Image.new('RGBA', (100, 100))
37+
im.getpixel((0,0))
38+
(0, 0, 0, 0)
39+
for x in range(100):
40+
for y in range(50):
41+
im.putpixel((x, y), (210, 210, 210))
42+
43+
for x in range(100):
44+
for y in range(50, 100):
45+
im.putpixel((x, y), ImageColor.getcolor('darkgray', 'RGBA'))
46+
47+
48+
from PIL import ImageColor
49+
for x in range(100):
50+
for y in range(50, 100):
51+
im.putpixel((x, y), ImageColor.getcolor('darkgray', 'RGBA'))
52+
53+
im.save('putPixel.png')

17-image/pasted.png

1.37 MB
Loading

17-image/png2jpg.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from PIL import Image
2+
3+
catIm = Image.open('zophie.png')
4+
catIm.save('zophie.jpg')

17-image/purpleImage.png

538 Bytes
Loading

17-image/putPixel.png

323 Bytes
Loading

17-image/quartersized.png

408 KB
Loading

17-image/resizeAndAddLogo.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#! python3
2+
# resizeAndAddLogo.py - Resizes all images in current working directory to fit
3+
# in a 300x300 square, and adds catlogo.png to the lower-right corner.
4+
5+
import os
6+
from PIL import Image
7+
8+
SQUARE_FIT_SIZE = 300
9+
LOGO_FILENAME = 'catlogo.png'
10+
11+
logoIm = Image.open(LOGO_FILENAME)
12+
logoWidth, logoHeight = logoIm.size
13+
14+
os.makedirs('withLogo', exist_ok=True)
15+
# Loop over all files in the working directory.
16+
for filename in os.listdir('.'):
17+
if not (filename.endswith('.png') or filename.endswith('.jpg')) \
18+
or filename == LOGO_FILENAME:
19+
continue # skip non-image files and the logo file itself
20+
21+
im = Image.open(filename)
22+
width, height = im.size
23+
24+
# Check if image needs to be resized.
25+
if width > SQUARE_FIT_SIZE and height > SQUARE_FIT_SIZE:
26+
# Calculate the new width and height to resize to.
27+
if width > height:
28+
height = int((SQUARE_FIT_SIZE / width) * height)
29+
width = SQUARE_FIT_SIZE
30+
else:
31+
width = int((SQUARE_FIT_SIZE / height) * width)
32+
height = SQUARE_FIT_SIZE
33+
34+
# Resize the image.
35+
print('Resizing %s...' % (filename))
36+
im = im.resize((width, height))
37+
38+
# Add logo.
39+
print('Adding logo to %s...' % (filename))
40+
im.paste(logoIm, (width - logoWidth, height - logoHeight), logoIm)
41+
42+
# Save changes.
43+
im.save(os.path.join('withLogo', filename))

17-image/rotated180.png

1.35 MB
Loading

17-image/rotated270.png

1.05 MB
Loading

17-image/rotated6.png

1.31 MB
Loading

17-image/rotated6_expanded.png

1.38 MB
Loading

17-image/rotated90.png

1.05 MB
Loading

17-image/svelte.png

1.36 MB
Loading

17-image/text.png

2.89 KB
Loading

17-image/tiled.png

487 KB
Loading

17-image/transparentImage.png

78 Bytes
Loading

17-image/zophie.jpg

123 KB
Loading

17-image/zophie.png

1.3 MB
Loading

0 commit comments

Comments
 (0)