forked from chenguohui/AutomatePython
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3217e0a
commit 1689394
Showing
23 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.