Skip to content

Commit

Permalink
Split code in libs, add renaming over old information for mockup test…
Browse files Browse the repository at this point in the history
…, more comments and debug mode for helper classes
  • Loading branch information
Olivier Brunel committed May 3, 2020
1 parent 264b88f commit e44753e
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 70 deletions.
130 changes: 127 additions & 3 deletions FramePhotoHelpers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,136 @@

import os, re
from ProgressBar.ProgressBar import ProgressBar

SQUARE_RATIO_DEFAULT_TOLERANCE = 0.02
MOCKUP_EXTENSION_INFO = r"-(portrait|landscape|square)-s([0-9]+)x([0-9]+)c([0-9]+)x([0-9]+)"


#
# Role: return orientation from width and height
# Return:
# - "square" if within square tolerance
# - "portrait" if height > width
# - "landscape" otherwise
#
def getOrientation(width, height, squaretolerance = SQUARE_RATIO_DEFAULT_TOLERANCE):
if 1-squaretolerance <= height/width <= 1+squaretolerance:
return "square"
elif width >= height:
elif width > height:
return "portrait"
elif height <= width:
else:
return "landscape"


#
# Check if filename contains mockup frame information
#
def hasMockupInfo(filename):
return re.search(MOCKUP_EXTENSION_INFO, filename)


#
# Delete mockup frame information from filename
#
def deleteMockupInfo(filename):
return re.sub(MOCKUP_EXTENSION_INFO, "", filename)


#
# Return mockup frame information from filename
# Generates an exception if information not valid or filename doesn't contain information
#
def getMockupInfo(filename):
info = hasMockupInfo(filename)
if info:
return (info.group(1),int(info.group(2)),int(info.group(3)),int(info.group(4)),int(info.group(5)))
else:
raise Exception("Could find mockup information")


#
# Return a filename with mockup frame information embedded in it
#
def addMockupInfo(filename, framesize, framecoordinates):
mockupnameadd = ""

# Orientation
if framesize[0] > framesize[1]:
mockupnameadd += "landscape"
elif framesize[0] < framesize[1]:
mockupnameadd += "portrait"
elif framesize[0] == framesize[1]:
mockupnameadd += "square"

# Make frame position and size suffix
mockupnameadd += "-s"+str(framesize[0])+"x"+str(framesize[1])
mockupnameadd += "c"+str(framecoordinates[0])+"x"+str(framecoordinates[1])

# Get mockupname and ext
(mockupnamenoext, ext) = os.path.splitext(os.path.basename(filename))

# Remove already present extension
mockupnamenoext = deleteMockupInfo(mockupnamenoext)

# Return result
return (mockupnamenoext+"-"+mockupnameadd+ext)


#
# Role: hold information every image will need
#
class BasicPicture:
# Basic information of an image object
def __init__(self, image, debug=False):
self.image = image
self.debug = debug
self.lookupBasicInfo()
def lookupBasicInfo(self):
self.fullpath = os.path.abspath(self.image.filename)
(self.filename, self.extension) = os.path.splitext(os.path.basename(self.fullpath))
(self.width, self.height) = self.image.size

#
# Role: hold information on mockups
# - mockup image size
# - frame orientation and size
#
class Mockup(BasicPicture):
# Information specific to mockups
def __init__(self, image, debug=False):
super().__init__(image, debug)
self.valid = True
self.lookupMockupInfo()
def lookupMockupInfo(self):
try:
(self.frameorientation,
self.framewidth,
self.frameheight,
self.framecoordinatex,
self.framecoordinatey) = getMockupInfo(self.filename)
except:
# Could not find info
if self.debug:
print("Mockup "+self.filename+" not valid")
self.valid = False
else:
# Check results
if not (
self.framewidth > 0 and self.frameheight > 0 and
self.framecoordinatex > 0 and self.framecoordinatey > 0
):
self.valid = False
else:
# Work out orientation
self.frameorientation = getOrientation(self.framewidth,self.frameheight)

#
# Role: hold information on photos
# - orientation and size
#
class Photo(BasicPicture):
# Information specific to photos
def __init__(self, image, debug=False):
super().__init__(image, debug)
self.lookupPhotoInfo()
def lookupPhotoInfo(self):
self.orientation = getOrientation(self.width, self.height)
61 changes: 3 additions & 58 deletions FramePhotoLib.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,8 @@
import sys, os, re, time
import sys, os, time
import multiprocessing
from PIL import Image
import FramePhotoHelpers

#
# Role: hold information every image will need
class BasicPicture:
# Basic information of an image object
def __init__(self, image):
self.image = image
self.lookupBasicInfo()
def lookupBasicInfo(self):
self.fullpath = os.path.abspath(self.image.filename)
(self.filename, self.extension) = os.path.splitext(os.path.basename(self.fullpath))
(self.width, self.height) = self.image.size

#
# Role: hold information on mockups
# - mockup image size
# - frame orientation and size
class Mockup(BasicPicture):
# Information specific to mockups
def __init__(self, image):
super().__init__(image)
self.valid = True
self.lookupMockupInfo()
def lookupMockupInfo(self):
info = re.search("(portrait|landscape|square)-s([0-9]+)x([0-9]+)c([0-9]+)x([0-9]+)", self.filename)
try:
self.frameorientation = info.group(1)
self.framewidth = int(info.group(2))
self.frameheight = int(info.group(3))
self.framecoordinatex = int(info.group(4))
self.framecoordinatey = int(info.group(5))
except:
# Could not find info
self.valid = False
else:
# Check results
if not (
self.framewidth > 0 and self.frameheight > 0 and
self.framecoordinatex > 0 and self.framecoordinatey > 0
):
self.valid = False
else:
# Work out orientation
self.frameorientation = FramePhotoHelpers.getOrientation(self.framewidth,self.frameheight)

#
# Role: hold information on photos
# - orientation and size
class Photo(BasicPicture):
# Information specific to photos
def __init__(self, image):
super().__init__(image)
self.lookupPhotoInfo()
def lookupPhotoInfo(self):
self.orientation = FramePhotoHelpers.getOrientation(self.width, self.height)

#
# The big photo framer class
# Role: hold information on
Expand Down Expand Up @@ -93,7 +38,7 @@ def lookupMockups(self):
except:
pass
else:
mockup = Mockup(mockup_im)
mockup = FramePhotoHelpers.Mockup(mockup_im, self.debug)
if mockup.valid:
self.mockupList.append(mockup)
break # Don't look in subdirs
Expand All @@ -119,7 +64,7 @@ def lookupPhotos(self):
except:
pass
else:
self.photoList.append(Photo(photo_im))
self.photoList.append(FramePhotoHelpers.Photo(photo_im, self.debug))
break # Don't look in subdirs

if len(self.photoList) > 0:
Expand Down
26 changes: 17 additions & 9 deletions test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import sys, os, getopt
from PIL import Image
import FramePhotoHelpers


#
# Print help message
#
def help(message = ""):
print('Help - parameters: path/to/mockup framewidth [frameheight] framestartx framestarty')
print("Available options are:")
Expand All @@ -14,6 +19,11 @@ def help(message = ""):

print("\n"+message)


#
# Test a mockup with frame size and coordinates
# then propose a new name with parameters included
#
def testMockup(mockuppath, framesize, framecoordinates, noask):
application_path = os.path.dirname(os.path.abspath(__file__))

Expand Down Expand Up @@ -41,15 +51,7 @@ def testMockup(mockuppath, framesize, framecoordinates, noask):

# Helper for frame name in case it's good
print("If this result is good (meaning you dimensioned the frame correctly and the photo fits perfectly in it), you should rename the frame as follows in order to use it as a frame for kshhhactivate:")
mockupnameadd = "landscape"
if framesize[0] < framesize[1]:
mockupnameadd = "portrait"
elif framesize[0] == framesize[1]:
mockupnameadd = "square"
mockupnameadd += "-s"+str(framesize[0])+"x"+str(framesize[1])
mockupnameadd += "c"+str(framecoordinates[0])+"x"+str(framecoordinates[1])
(mockupnamenoext, ext) = os.path.splitext(os.path.basename(mockuppath))
mockupname = mockupnamenoext+"-"+mockupnameadd+ext
mockupname = FramePhotoHelpers.addMockupInfo(mockuppath,framesize,framecoordinates)
print(mockupname)

if not noask:
Expand All @@ -62,6 +64,9 @@ def testMockup(mockuppath, framesize, framecoordinates, noask):
print("\nCheck file "+testresultfilename+" at "+wheretosaveresultfile)


#
# Main function, called with argv passed on command line
#
def main(argv):
# Look for options
try:
Expand Down Expand Up @@ -116,6 +121,9 @@ def main(argv):
# Go do the job!
testMockup(mockuppath, (framewidth, frameheight), (framestartx, framestarty), noask)


#
# Will handle main here before handing off to mockup test function
#
if __name__ == "__main__":
main(sys.argv[1:])

0 comments on commit e44753e

Please sign in to comment.