Advertisement
timscampi

Python Cover Crop for Media Centers

Jul 19th, 2011
1,405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.42 KB | None | 0 0
  1. ##################################################
  2. # Python Cover Crop for Media Centers            #
  3. ##################################################
  4. # Coded by Timscampi                             #
  5. # v0.1 (07/19/2011) - Initial release            #
  6. # v0.2 (07/20/2011) - Added 20 presets           #
  7. ##################################################
  8. # Script under GNU GPL v3                        #
  9. # http://www.gnu.org/licenses/gpl-3.0.html       #
  10. ##################################################
  11.  
  12. # Imports
  13. from PIL import Image
  14. import os
  15.  
  16. # Settings
  17. debugmode = 1 # Value of 0 = OFF and 1 = ON; Show useful debug messages during processing
  18.  
  19. # List all .jpg files with same name as directory
  20. for dirname, dirnames, filenames in os.walk('.'):
  21.     for filename in filenames:
  22.         filecheck = dirname.replace(".\\","")
  23.         if os.path.join(dirname, filename) == (dirname + "\\" + filecheck + ".jpg"):
  24.             # Process all files matching the pattern <dirname>.jpg
  25.             print "Processing " + os.path.join(dirname, filename)
  26.             filepath = os.path.join(dirname, filename)
  27.             image = Image.open(filepath)
  28.  
  29.             # Get the image dimensions
  30.             width, height = image.size
  31.             # Show image dimensions if debug is ON
  32.             if debugmode == 1:
  33.                 print "  Debug: Width:", width, "| Height:", height
  34.  
  35.             # Calculate dimensions of cropped image
  36.             croppedwidth = int(width / 2.11)
  37.             croppedheight = height
  38.  
  39.             #Presets
  40.             #Preset SOD (SDMS, SDDE); crop 3 pixels
  41.             if ("SDDE" in filename) or ("SDMS" in filename):
  42.                 croppedwidth = croppedwidth - 3
  43.                 print "  - Preset: Soft On Demand"
  44.             #Preset Natura High (NHDT); crop 2 pixels
  45.             if ("NHDT" in filename):
  46.                 croppedwidth = croppedwidth - 2
  47.                 print "  - Preset: Natura High"
  48.             #Preset HTY; crop 1 pixel
  49.             if ("HTV" in filename):
  50.                 croppedwidth = croppedwidth - 1
  51.                 print "  - Preset: HTV"
  52.             #Preset Prestige (EVO, DAY, ZER, EZD, DOM); crop 1 pixel
  53.             if ("EVO" in filename) or ("DAY" in filename) or ("ZER" in filename) or ("EZD" in filename) or ("DOM" in filename) and (height == 522):
  54.                 croppedwidth = croppedwidth - 1
  55.                 print "  - Preset: Prestige (522)"
  56.             #Preset Prestige (DOM); overcrop a little
  57.             if ("DOM" in filename) and (height == 488):
  58.                 croppedwidth = croppedwidth + 13
  59.                 print "  - Preset: Prestige (488)"
  60.             #Preset DIM; crop 5 pixels
  61.             if ("DIM" in filename):
  62.                 croppedwidth = croppedwidth - 5
  63.                 print "  - Preset: DIM"
  64.             #Preset DNPD front on the left, crop 0 pixels
  65.             if ("DNPD" in filename):
  66.                 print "  - Preset: DNPD"
  67.             #Preset CRZ; crop 5 pixels
  68.             if ("CRZ" in filename) and (height == 541):
  69.                 croppedwidth = croppedwidth - 5
  70.                 print "  - Preset: CRZ"
  71.             #Preset FSET; crop 2 pixels
  72.             if ("FSET" in filename) and (height == 675):
  73.                 croppedwidth = croppedwidth - 2
  74.                 print "  - Preset: FSET"
  75.             #Preset Natural High (ABC-001); Couldn't find any cover to test with
  76.             # !!!BROKEN!!! Cannot find a cover scan
  77.             #if ("ABC" in filename):
  78.             #   print "  - Preset: FSET"
  79.             #Preset Moodyz (MIRD dual discs); center the overcropping
  80.             if ("MIRD" in filename) and (height == 499):
  81.                 print "  - Preset: Moodyz dual discs"
  82.                 #Need a reference cover to adjust
  83.             #Preset Opera (OPRD); crop 1 pixel
  84.             if ("OPRD" in filename) and (height == 540):
  85.                 croppedwidth = croppedwidth - 1
  86.                 print "  - Preset: Opera"
  87.             #Preset Jade (P9); crop 2 pixels
  88.             if ("P9" in filename) and (height == 470):
  89.                 croppedwidth = croppedwidth - 2
  90.                 print "  - Preset: Jade"
  91.             #Preset Rocket (RCT); crop 2 pixels
  92.             if ("RCT" in filename) and (height == 471):
  93.                 croppedwidth = croppedwidth - 2
  94.                 print "  - Preset: Rocket"
  95.             #Preset Image (SIMG); crop 10 pixels
  96.             if ("SIMG" in filename) and (height == 864):
  97.                 croppedwidth = croppedwidth - 10
  98.                 print "  - Preset: Image (864)"
  99.             #Preset Image (SIMG); crop 4 pixels
  100.             if ("SIMG" in filename) and (height == 541):
  101.                 croppedwidth = croppedwidth - 4
  102.                 print "  - Preset: Image (541)"
  103.             #Preset Image (SVDVD); crop 2 pixels   
  104.             if ("SVDVD" in filename) and (height == 950):
  105.                 croppedwidth = croppedwidth - 4
  106.                 print "  - Preset: Image (SVDVD)"
  107.             #Preset Image (XV-65); crop 6 pixels
  108.             if ("XV-65" in filename) and (width == 750):
  109.                 croppedwidth = croppedwidth - 6
  110.                 print "  - Preset: Image (XV-65)"
  111.             #Preset 800x538; crop 2 pixels
  112.             if (height == 538) and (width == 800):
  113.                 croppedwidth = croppedwidth - 2
  114.                 print "  - Preset: Default (800x538)"
  115.             #Preset 800x537; crop 1 pixel
  116.             if (height == 537) and (width == 800):
  117.                 croppedwidth = croppedwidth - 1
  118.                 print "  - Preset: Default (800x537)"  
  119.  
  120.             if debugmode == 1:
  121.                 print "  Debug: Cropped Width:", croppedwidth, "| Cropped Height:", croppedheight
  122.  
  123.             # Let's crop the image
  124.             if "DNPD" in filename:
  125.                 # Handling some weird inverted covers
  126.                 croptupple = [0,0,croppedwidth,height]
  127.             else:
  128.                 croptupple = [(width - croppedwidth),0,width,height]
  129.             if debugmode == 1:
  130.                 print "  Debug: cropping dimensions:",croptupple
  131.             croppedimage = image.crop(croptupple)
  132.             if debugmode == 1:
  133.                 print "  Debug: cropped successfully !"
  134.  
  135.             # Save both images to the folder
  136.             folderpath = dirname + "\\folder.jpg"
  137.             posterpath = dirname + "\\poster.jpg"
  138.             croppedimage.save(folderpath)
  139.             if debugmode == 1:
  140.                 print "  Debug: folder.jpg saved !"
  141.             croppedimage.save(posterpath)
  142.             if debugmode == 1:
  143.                 print "  Debug: poster.jpg saved !"
  144. print "Process complete."
  145. os.system("pause")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement