Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def rotate():
"""Rotates an image."""
with Image(filename=asset('rotatetest.gif')) as img:
assert 150 == img.width
assert 100 == img.height
with img.clone() as cloned:
cloned.rotate(360)
assert img.size == cloned.size
with Color('black') as black:
assert black == cloned[0, 50] == cloned[74, 50]
assert black == cloned[0, 99] == cloned[74, 99]
with Color('white') as white:
assert white == cloned[75, 50] == cloned[75, 99]
with img.clone() as cloned:
cloned.rotate(90)
assert 100 == cloned.width
assert 150 == cloned.height
with Color('black') as black:
with Color('white') as white:
@tests.test
def setitem():
with Image(filename=asset('apple.ico')) as imga:
with Image(filename=asset('google.ico')) as imgg:
imga.sequence[2] = imgg
assert len(imga.sequence) == 4
assert imga.sequence[2].size == (16, 16)
expire(imga)
assert imga.sequence[2].size == (16, 16)
@tests.test
def extend_offset():
with Image(filename=asset('apple.ico')) as a:
instances = list(a.sequence)
with Image(filename=asset('github.ico')) as b:
a.sequence.extend(list(b.sequence)[::-1], 2)
instances[2:2] = list(b.sequence)[::-1]
assert list(a.sequence) == instances
expire(a)
assert list(a.sequence) == instances
assert len(a.sequence) == 6
def fetch(self, key=''):
if key == 'filename_zxing':
pill = self.fetch('pillow')
if hasattr(pill, 'n_frames') and pill.n_frames != 1:
return None
# ZXing cannot handle most TIFF images, convert to PNG.
with wand.image.Image(filename=self.fetch('filename')) \
as tiff_image:
tiff_image.format = 'png'
fd, name = tempfile.mkstemp(
suffix=os.path.split(self.fetch('filename'))[-1] + '.png',
prefix='tmp_file_metadata')
os.close(fd)
tiff_image.save(filename=name)
self.temp_filenames.add(name)
return pathlib2.Path(name).as_uri()
return super(TIFFFile, self).fetch(key)
def load_image(filename):
if wand is not None:
return wand.image.Image(filename=filename)
elif PIL is not None:
return PIL.Image.open(filename)
else:
sys.stderr.write('You must have wand or Pillow/PIL installed to use the dhash command\n')
sys.exit(1)
def extract_pdf_datamatrix(self):
uuid_set = str(uuid.uuid4().fields[-1])[:5]
image_folder = os.path.dirname(self.path)
image_file = os.path.join(image_folder, 'image%s.png' % uuid_set)
pages = Image(filename=self.path)
page = pages.sequence[0] # Check first page
with Image(page) as img:
img.format = 'png'
img.background_color = Color('white')
img.alpha_channel = 'remove'
img.save(filename=image_file)
datamatrix = pylibdmtx.decode(Img.open(image_file))
os.remove(image_file)
return datamatrix and datamatrix[0].data.decode() or False
def resize(format, filep, info, logger):
image_buffer = None
try:
with Image(file=filep, resolution=96) as tiff:
image_format = format.get('format')
if image_format is not None:
image = tiff.convert(image_format)
else:
image = tiff
target_height = format.get('height')
target_width = format.get('width')
target_resolution = format.get('resolution')
if target_resolution is not None:
image.resolution = (target_resolution, target_resolution)
if target_height is None and target_width is None:
target_height = image.height
target_width = image.width
async def deepfry(filename, params):
outfile = tempfile.mkstemp(prefix="deepfry", suffix=".jpg")[1]
logging.info("About to run deepfry(%s) -> %s", filename, outfile)
with Image(filename=filename) as img:
img.modulate(brightness=params['brightness'], saturation=params['saturation'])
slope = math.tan((math.pi * (55/100.0+1.0)/4.0))
if slope < 0.0:
slope=0.0
intercept = 15/100.0+((100-15)/200.0)*(1.0-slope)
img.function("polynomial", [slope, intercept])
img.evaluate(operator='gaussiannoise', value=0.05)
if params['sine'] != None:
img.function('sinusoid', params['sine'])
logging.info('Fried img %s with params %s' % (filename, str(params)))
img.save(filename=outfile)
response = format_params(params)
return outfile, response
tmpdir = tempfile.mkdtemp(prefix="mymagick")
try:
os.environ["MAGICK_TMPDIR"] = tmpdir
pdf_imgs = Image()
pdf_imgs.read(blob=pdf_data)
# XXX: There's a known bug that some pdf cannot be read using
# constructor, but to use `read` method after construction of
# Image. The following code will result in
# wand.exceptions.CorruptImageError:
# with Image(blob=pdf_data) as pdf_imgs:
for idx in pages:
blob = pdf_imgs.sequence[idx]
j = Image(blob)
j.format = "jpg"
j.transform(resize="{}x{}".format(width_max, height_max))
bio = io.BytesIO()
j.save(bio)
img_data = bio.getvalue()
rst[idx] = img_data
pdf_imgs.close()
finally:
shutil.rmtree(tmpdir)
return rst
import cgi
import wand.image
for char_i in range(33, 126):
svg = """
<svg viewBox="0 0 256 256" height="256" width="256" xmlns="http://www.w3.org/2000/svg">
<text fill="black" font-size="280" font-family="Cooper Black" text-anchor="middle" y="200" x="128">{}</text>
</svg>
""".format(cgi.escape(chr(char_i)))
with wand.image.Image(blob=svg.encode('utf-8'), format='svg') as img:
filename = 'Assets/GameAssets/Characters/c' + str(char_i).zfill(3) + '.png'
img.save(filename=filename)