Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import os, sys
- from PIL import Image
- size = (128, 128)
- def jpegtothumb(infile: "original.jpeg", outfile: "new_name"):
- """
- converts `original.jpeg` to `new_name.jpeg` (thumbnail)
- default size to convert is (128, 128) in pixels
- default size keeps proportions from "infile"
- the format of "outfile" is JPEG
- Example:
- Assuming "original.jpeg" # infile
- JPEG (474, 315) RGB
- "new_name.jpeg" # thumbnail
- JPEG (128, 85) RGB # keep proportions
- The "original.jpeg" to be modified must be in the same directory
- where the function is executed. If not, report:
- OSError: "cannot create thumbnail for original.jpeg"
- """
- if infile != outfile:
- try:
- with Image.open(infile) as im:
- im.thumbnail(size)
- im.save(outfile + ".jpeg", "JPEG")
- except OSError:
- print("cannot create thumbnail for", infile)
- # jpegtothumb("original.jpeg", "new_name") <-- use
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement