Skip to content

Instantly share code, notes, and snippets.

@Nzen
Last active October 13, 2018 02:19
Show Gist options
  • Save Nzen/01d81e873eebe1580e2eb1b2b5346f8c to your computer and use it in GitHub Desktop.
Save Nzen/01d81e873eebe1580e2eb1b2b5346f8c to your computer and use it in GitHub Desktop.
resize centered

resize centered

There are various times I've needed to change a picture to a new size with a different aspect ratio. I keep reimplementing the algorithm, so why not use this subsidized hosting to avoid that piece ?

The idea is to pick an appropriate side, increase the size of the other side until it is the right aspect for the target size, and resize down to the target dimensions. I prefer these in the center, so increasing the size is two step.

resize.py is the script I used to create kindle screensavers. I assume the final dimensions are 800 x 600 px. It starts a repl expecting a character code {r, c, h, exit}, the int of a single side, and y/n of whether the target of that side is 800. It looks like I last modified it 24 July '17. But I uploaded the last screensaver five years ago, '13. At that time I'm practicing python intentionally, hence the ternary on line 17.

np 181012 resize_image.py is the script I reimplemented today because I didn't have access to another copy. This is in service of work, where, once in a blue moon, I'll need to change a client's web logo to match the aspect ratio I prefer for report headers. I hadn't really needed it because, while the math is a little tricky, it's not many steps and had been for just one image. Today I needed three, and foresaw that maybe I'll end up doing this again. At this point I haven't used python in a year and one half. It assumes four integer sizes, corresponding to source height,width and target height,width. I assume that the width is the larger ratio side.

It's funny to look at these side by side. The elder is polished (though I mixed variable styles). The younger is a bit brute force. To be fair, I used the elder quite a bit, so I'd have many occasions to refactor it. On the other hand, I have vaguely noticed that I've not been so quick to decompose my functions as much as I could.

Did I mention I miss writing for my website ?

© Nicholas Prado. Released under terms of wtfp license, as described at http://www.wtfpl.net/txt/copying/.

'''
cli args- src_hi src_wid targ_hi targ_wid
prints the steps to take to resize
'''
from sys import argv
def resize_instructions( src_hi, src_wid, targ_hi, targ_wid ) :
# improve by swapping depending upon which is larger or whatever
# calculate values
targ_ratio_hw = targ_hi / targ_wid
targ_ratio_wh = targ_wid / targ_hi
huge_wid = targ_ratio_wh * src_hi # height, not width because the ratio is upside down
huge_wid_half = huge_wid / 2.0
src_wid_half = src_wid / 2.0
diff_huge_src = huge_wid_half - src_wid_half
half_diff_wid = diff_huge_src / 2.0
awkw_wid = src_wid_half + huge_wid_half
resize_multiple = targ_wid / huge_wid
# describe steps
print 'increase width to {0}'.format( awkw_wid )
print 'rotate 180 degradians'
print 'increase width to {0}'.format( huge_wid )
print 'rotate 180 degradians'
print 'multiply size by {0}'.format( resize_multiple )
source_hieght = ''
source_width = ''
target_hieght = ''
target_width = ''
try :
# one indexed because the first arg is the python file
source_hieght = argv[ 1 ]
source_width = argv[ 2 ]
target_hieght = argv[ 3 ]
target_width = argv[ 4 ]
except IndexError :
print 'too few args, type by hand then'
source_hieght = raw_input( ' - source height ? ' )
source_width = raw_input( ' - source width ? ' )
target_hieght = raw_input( ' - target height ? ' )
target_width = raw_input( ' - target width ? ' )
resize_instructions( float( source_hieght ), float( source_width ),
float( target_hieght ), float( target_width ) )
# so I don't need to retype this in the sinning interpreter every time
def resize( a_side, is_800 ) :
'proportion to scale'
targ = size( to_bool( is_800 ) )
print targ / a_side
def new_length( a_side, is_800 ) :
'new side amount so pic in center'
targ = size( to_bool( is_800 ) )
print ( targ - a_side ) / 2.0 + a_side
def to_bool( char_800 ) :
return char_800 == 'y'
def size( now_800 ) :
return ( 800.0 if now_800 else 600.0 )
def parse_which( line ) :
'line is a list starting with either r or c'
if line.__len__() < 3 :
print "X> didn't put all arguments"
return
fx = line[ 0 ]
size_of_side = float( line[ 1 ] )
if_800 = line[ 2 ]
if fx == 'c' :
new_length( size_of_side, if_800 )
else : # == r
resize( size_of_side, if_800 )
def help_text() :
return "H> c- 3/4 to new side; r- % to new side; h- repeat this; exit- ???"
def prompt( ) :
working = True
exitFlag = 'exit'
threeFourFlag = 'c'
resizeFlag = 'r'
helpFlag = 'h'
print help_text()
print "\tAssumes a crop, stretch, resize half workflow" \
+"\n\tform of c 450 y [or] r 5000 n"
while working :
which = raw_input( "\n?> " )
all_ = which.split( ' ' )
fx = all_[ 0 ]
if fx == exitFlag :
working = False
elif fx == threeFourFlag or fx == resizeFlag :
parse_which( all_ )
elif fx == helpFlag :
print help_text()
else :
print "X> whoops try c r h or exit"
prompt()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment