Advertisement
JPablos

Crear thumbnails de 'JPEG'. Python

Mar 26th, 2021 (edited)
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import os, sys
  4. from PIL import Image
  5.  
  6. size = (128, 128)
  7.  
  8. def jpegtothumb(infile: "original.jpeg", outfile: "new_name"):
  9.    
  10.     """
  11.    converts `original.jpeg` to `new_name.jpeg` (thumbnail)
  12.    default size to convert is (128, 128) in pixels
  13.    default size keeps proportions from "infile"
  14.    the format of "outfile" is JPEG
  15.  
  16.        Example:
  17.                Assuming    "original.jpeg"         # infile
  18.                            JPEG (474, 315) RGB
  19.  
  20.                            "new_name.jpeg"         # thumbnail
  21.                            JPEG (128, 85) RGB      # keep proportions
  22.  
  23.    The "original.jpeg" to be modified must be in the same directory
  24.    where the function is executed. If not, report:
  25.  
  26.                OSError: "cannot create thumbnail for original.jpeg"
  27.    """
  28.    
  29.     if infile != outfile:
  30.         try:
  31.             with Image.open(infile) as im:
  32.                 im.thumbnail(size)
  33.                 im.save(outfile + ".jpeg", "JPEG")
  34.         except OSError:
  35.             print("cannot create thumbnail for", infile)
  36.  
  37. # jpegtothumb("original.jpeg", "new_name")   <-- use
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement