Skip to content

Commit

Permalink
Rotate images using the Python Pillow library.
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeShortCloud committed Nov 7, 2018
1 parent 15ac2f2 commit 192ee02
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
21 changes: 15 additions & 6 deletions cgc/cgc.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def run_cmd(cmd):
logging.debug("convert command output: %s", str(cmd_return))
return cmd_return

def convert_rotate(self, image_path_src, image_path_dest, degrees="90"):
def image_rotate(self, image_path_src, image_path_dest, degrees="90"):
"""Execute the convert command to rotate an image.
Args:
Expand All @@ -137,12 +137,21 @@ def convert_rotate(self, image_path_src, image_path_dest, degrees="90"):
Returns:
boolean: If the convert command completed successfully.
"""
cmd = ["convert", "-rotate", degrees, image_path_src,
image_path_dest]

if self.run_cmd(cmd)["rc"] != 0:
image = Image.open(image_path_src)
image_rotated = None

if degrees == "90":
image_rotated = image.transpose(Image.ROTATE_90)
elif degrees == "180":
image_rotated = image.transpose(Image.ROTATE_180)
elif degrees == "270":
image_rotated = image.transpose(Image.ROTATE_270)
else:
logging.error("Incorrect degrees specified. Please use " + \
"90, 180, or 270.")
return False

image_rotated.save(image_path_dest)
return True

def convert_rotate_by_dimensions(self, image_path):
Expand All @@ -159,7 +168,7 @@ def convert_rotate_by_dimensions(self, image_path):
if width > height:
logging.debug("Rotating image: %s", image_path)

if not self.convert_rotate(image_path, image_path):
if not self.image_rotate(image_path, image_path):
return False

return True
Expand Down
2 changes: 1 addition & 1 deletion cgc_tdd.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ The "Card Games Converter" (CGC) is a utility for converting pictures of cards i
* rc (int) = Return code.
* stdout (str bytes) = Standard output.
* stderr (str bytes) = Standard error.
* convert_rotate = Rotate an image.
* image_rotate = Rotate an image.
* Input
* image_path (str) = The full image path to use.
* Ouput
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="cgc",
version="1.3.1",
version="1.4.0",
author="Luke Short",
author_email="ekultails@gmail.com",
url="https://github.com/ekultails/card_games_converter",
Expand Down
4 changes: 2 additions & 2 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ def test_run_cmd(self):
and (convert_results["rc"] != 0):
self.assertTrue(False)

def test_convert_rotate(self):
def test_image_rotate(self):
image_dimensions_old = self.cgc.image_info(self.last_image_card)
return_status = self.cgc.convert_rotate(self.last_image_card,
return_status = self.cgc.image_rotate(self.last_image_card,
self.tmp_card)
image_dimensions_new = self.cgc.image_info(self.tmp_card)

Expand Down

0 comments on commit 192ee02

Please sign in to comment.