added image aspect ratio fixing

This commit is contained in:
muskit
2022-11-22 21:46:42 -08:00
parent a604d481e8
commit 2a5ee3aa46
4 changed files with 35 additions and 0 deletions
+1
View File
@@ -146,6 +146,7 @@ cython_debug/
secrets.ini secrets.ini
queue.txt queue.txt
img.png img.png
img_adjusted.png
src/img.png src/img.png
_queue_backup.txt _queue_backup.txt
_current_ttweet.txt _current_ttweet.txt
+1
View File
@@ -2,4 +2,5 @@ nest-asyncio
pytz pytz
tweet-capture tweet-capture
tweepy tweepy
opencv-python
git+https://github.com/muskit/twint_2022_fix.git git+https://github.com/muskit/twint_2022_fix.git
+31
View File
@@ -0,0 +1,31 @@
import cv2
from math import ceil
# [3/4 --> 16/9]
def fix_aspect_ratio(img_file):
"""Returns path to new image that better fits the aspect ratio of a single-image post."""
img = cv2.imread(img_file)
img_ratio = img.shape[1]/img.shape[0]
print(f'{img_file}: {img_ratio}')
if img_ratio > 16/9:
print('too wide; add height')
x, y = img.shape[1], (img.shape[1]*9)/16
elif img_ratio < 3/4:
print('too skinny; add width')
x, y = (img.shape[0]*3)/4, img.shape[0]
else:
print('ratio is good; no need to readjust')
return img_file
img_adjusted = cv2.copyMakeBorder(
img,
top=ceil((y-img.shape[0])/2),
bottom=ceil((y-img.shape[0])/2),
left=ceil((x-img.shape[1])/2),
right=ceil((x-img.shape[1])/2),
borderType=cv2.BORDER_REPLICATE
)
adjusted_file = f'{img_file.rsplit(".", 1)[0]}_adjusted.png'
cv2.imwrite(filename=adjusted_file, img=img_adjusted)
return adjusted_file
+2
View File
@@ -10,6 +10,7 @@ import twint
import twapi import twapi
from tweetcapture import TweetCapture from tweetcapture import TweetCapture
from recrop import fix_aspect_ratio
import talent_lists import talent_lists
# returns system path to this project, which is # returns system path to this project, which is
@@ -61,6 +62,7 @@ async def create_ttweet_image(ttweet):
mode=4, mode=4,
night_mode=1 night_mode=1
) )
img = fix_aspect_ratio(img)
except: except:
print('unable to create tweet image') print('unable to create tweet image')
traceback.print_exc() traceback.print_exc()