Remove unnecessary coerce to float

In Python 3, the division operator is floating point division. No longer
need to coerce integers to floating point numbers before division.
This commit is contained in:
Jon Dufresne 2020-01-26 06:21:41 -08:00
parent a0a9b76ab5
commit 63729766c4
11 changed files with 22 additions and 23 deletions

View File

@ -124,7 +124,6 @@ class PillowTestCase(unittest.TestCase):
self.assert_image_equal(a, img, msg) self.assert_image_equal(a, img, msg)
def assert_image_similar(self, a, b, epsilon, msg=None): def assert_image_similar(self, a, b, epsilon, msg=None):
epsilon = float(epsilon)
self.assertEqual( self.assertEqual(
a.mode, b.mode, msg or "got mode {!r}, expected {!r}".format(a.mode, b.mode) a.mode, b.mode, msg or "got mode {!r}, expected {!r}".format(a.mode, b.mode)
) )
@ -139,7 +138,7 @@ class PillowTestCase(unittest.TestCase):
chdiff = ImageMath.eval("abs(a - b)", a=ach, b=bch).convert("L") chdiff = ImageMath.eval("abs(a - b)", a=ach, b=bch).convert("L")
diff += sum(i * num for i, num in enumerate(chdiff.histogram())) diff += sum(i * num for i, num in enumerate(chdiff.histogram()))
ave_diff = float(diff) / (a.size[0] * a.size[1]) ave_diff = diff / (a.size[0] * a.size[1])
try: try:
self.assertGreaterEqual( self.assertGreaterEqual(
epsilon, epsilon,

View File

@ -20,11 +20,11 @@ class TestColorLut3DCoreAPI(PillowTestCase):
table = [ table = [
[ [
r / float(size1D - 1) if size1D != 1 else 0, r / (size1D - 1) if size1D != 1 else 0,
g / float(size2D - 1) if size2D != 1 else 0, g / (size2D - 1) if size2D != 1 else 0,
b / float(size3D - 1) if size3D != 1 else 0, b / (size3D - 1) if size3D != 1 else 0,
r / float(size1D - 1) if size1D != 1 else 0, r / (size1D - 1) if size1D != 1 else 0,
g / float(size2D - 1) if size2D != 1 else 0, g / (size2D - 1) if size2D != 1 else 0,
][:channels] ][:channels]
for b in range(size3D) for b in range(size3D)
for g in range(size2D) for g in range(size2D)

View File

@ -8,10 +8,10 @@ from .helper import PillowTestCase, hopper
class TestFormatHSV(PillowTestCase): class TestFormatHSV(PillowTestCase):
def int_to_float(self, i): def int_to_float(self, i):
return float(i) / 255.0 return i / 255
def str_to_float(self, i): def str_to_float(self, i):
return float(ord(i)) / 255.0 return ord(i) / 255
def tuple_to_ints(self, tp): def tuple_to_ints(self, tp):
x, y, z = tp x, y, z = tp

View File

@ -164,7 +164,7 @@ class TestImageReduce(PillowTestCase):
ch_diff = ImageMath.eval("convert(abs(a - b), 'L')", a=ach, b=bch) ch_diff = ImageMath.eval("convert(abs(a - b), 'L')", a=ach, b=bch)
ch_hist = ch_diff.histogram() ch_hist = ch_diff.histogram()
average_diff = sum(i * num for i, num in enumerate(ch_hist)) / float( average_diff = sum(i * num for i, num in enumerate(ch_hist)) / (
a.size[0] * a.size[1] a.size[0] * a.size[1]
) )
self.assertGreaterEqual( self.assertGreaterEqual(

View File

@ -499,8 +499,8 @@ class TestImageCms(PillowTestCase):
# paste pattern with varying offsets to avoid correlation # paste pattern with varying offsets to avoid correlation
# potentially hiding some bugs (like channels getting mixed). # potentially hiding some bugs (like channels getting mixed).
paste_offset = ( paste_offset = (
int(band_ndx / float(len(bands)) * channel_pattern.size[0]), int(band_ndx / len(bands) * channel_pattern.size[0]),
int(band_ndx / float(len(bands) * 2) * channel_pattern.size[1]), int(band_ndx / (len(bands) * 2) * channel_pattern.size[1]),
) )
channel_data = Image.new(channel_type, channel_pattern.size) channel_data = Image.new(channel_type, channel_pattern.size)
for delta in nine_grid_deltas: for delta in nine_grid_deltas:

View File

@ -75,8 +75,8 @@ def Ghostscript(tile, size, fp, scale=1):
size = (size[0] * scale, size[1] * scale) size = (size[0] * scale, size[1] * scale)
# resolution is dependent on bbox and size # resolution is dependent on bbox and size
res = ( res = (
float((72.0 * size[0]) / (bbox[2] - bbox[0])), 72.0 * size[0] / (bbox[2] - bbox[0]),
float((72.0 * size[1]) / (bbox[3] - bbox[1])), 72.0 * size[1] / (bbox[3] - bbox[1]),
) )
out_fd, outfile = tempfile.mkstemp() out_fd, outfile = tempfile.mkstemp()

View File

@ -73,7 +73,7 @@ class GradientFile:
for i in range(entries): for i in range(entries):
x = i / float(entries - 1) x = i / (entries - 1)
while x1 < x: while x1 < x:
ix += 1 ix += 1

View File

@ -2357,8 +2357,8 @@ class Image:
elif method == EXTENT: elif method == EXTENT:
# convert extent to an affine transform # convert extent to an affine transform
x0, y0, x1, y1 = data x0, y0, x1, y1 = data
xs = float(x1 - x0) / w xs = (x1 - x0) / w
ys = float(y1 - y0) / h ys = (y1 - y0) / h
method = AFFINE method = AFFINE
data = (xs, 0, x0, 0, ys, y0) data = (xs, 0, x0, 0, ys, y0)

View File

@ -243,7 +243,7 @@ def pad(image, size, method=Image.BICUBIC, color=None, centering=(0.5, 0.5)):
""" """
im_ratio = image.width / image.height im_ratio = image.width / image.height
dest_ratio = float(size[0]) / size[1] dest_ratio = size[0] / size[1]
if im_ratio == dest_ratio: if im_ratio == dest_ratio:
out = image.resize(size, resample=method) out = image.resize(size, resample=method)
@ -420,10 +420,10 @@ def fit(image, size, method=Image.BICUBIC, bleed=0.0, centering=(0.5, 0.5)):
) )
# calculate the aspect ratio of the live_size # calculate the aspect ratio of the live_size
live_size_ratio = float(live_size[0]) / live_size[1] live_size_ratio = live_size[0] / live_size[1]
# calculate the aspect ratio of the output image # calculate the aspect ratio of the output image
output_ratio = float(size[0]) / size[1] output_ratio = size[0] / size[1]
# figure out if the sides or top/bottom will be cropped off # figure out if the sides or top/bottom will be cropped off
if live_size_ratio == output_ratio: if live_size_ratio == output_ratio:

View File

@ -437,7 +437,7 @@ class JpegImageFile(ImageFile.ImageFile):
self.tile = [(d, e, o, a)] self.tile = [(d, e, o, a)]
self.decoderconfig = (scale, 0) self.decoderconfig = (scale, 0)
box = (0, 0, original_size[0] / float(scale), original_size[1] / float(scale)) box = (0, 0, original_size[0] / scale, original_size[1] / scale)
return (self.mode, box) return (self.mode, box)
def load_djpeg(self): def load_djpeg(self):

View File

@ -119,8 +119,8 @@ class PSDraw:
else: else:
dpi = 100 # greyscale dpi = 100 # greyscale
# image size (on paper) # image size (on paper)
x = float(im.size[0] * 72) / dpi x = im.size[0] * 72 / dpi
y = float(im.size[1] * 72) / dpi y = im.size[1] * 72 / dpi
# max allowed size # max allowed size
xmax = float(box[2] - box[0]) xmax = float(box[2] - box[0])
ymax = float(box[3] - box[1]) ymax = float(box[3] - box[1])