Guest User

Untitled

a guest
Nov 16th, 2017
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. # Python3
  3. """Creates resized copies of the user's selected app icon to all sizes that iOS 8 requires
  4. and renames those copies to the names iOS 8 requires."""
  5.  
  6. import os
  7. import sys
  8. import shutil
  9. from typing import List, Tuple
  10. from tkinter import Tk
  11. from tkinter.filedialog import askopenfilename
  12. from PIL import Image
  13.  
  14. # iOS 8 required and recommended app icon sizes
  15. icon_sizes = [29, 58, 87, 40, 80, 60, 120, 180, 76, 152, 512, 1024]
  16.  
  17. # Corresponding names
  18. icon_names = ["Icon-Small.png",
  19. "Icon-Small@2x.png",
  20. "Icon-Small@3x.png",
  21. "Icon-Small-40.png",
  22. "Icon-Small-40@2x.png",
  23. "Icon-60.png",
  24. "Icon-60@2x.png",
  25. "Icon-60@3x.png",
  26. "Icon-76.png",
  27. "Icon-76@2x.png",
  28. "iTunesArtwork.png",
  29. "iTunesArtwork@2x.png",
  30. ]
  31.  
  32. # folder name in which copies are stored
  33. destination = "output"
  34.  
  35.  
  36. def open_img():
  37. """Opens file dialog and returns image selected"""
  38. Tk().withdraw()
  39. filename = askopenfilename()
  40. try:
  41. return Image.open(filename)
  42. except Exception as e:
  43. print("No file opened: {}".format(e))
  44. sys.exit(1)
  45.  
  46.  
  47. def resize(img, size: int):
  48. """Resizes given square image to given side dimension"""
  49. return img.resize((size, size), Image.ANTIALIAS)
  50.  
  51.  
  52. def save_imgs(names_and_images: List[Tuple[str, type]]):
  53. """Saves all created images to destination folder"""
  54. for name, img in names_and_images:
  55. img.save(os.path.join(destination, name))
  56.  
  57.  
  58. if __name__ == "__main__":
  59. original = open_img()
  60.  
  61. names_and_sizes = list(zip(icon_names, icon_sizes))
  62. names_and_images = [(name, resize(original, size))
  63. for name, size in names_and_sizes]
  64.  
  65. if os.path.exists(destination):
  66. shutil.rmtree(destination, ignore_errors=True)
  67. os.mkdir(destination)
  68.  
  69. save_imgs(names_and_images)
Add Comment
Please, Sign In to add comment