本文整理汇总了Python中pgmagick.Image类的典型用法代码示例。如果您正苦于以下问题:Python Image类的具体用法?Python Image怎么用?Python Image使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Image类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: render_image_background
def render_image_background(self, image_path, opacity=True):
image = Image(image_path)
if type(opacity) is int:
image.opacity(opacity)
elif opacity is True:
image.opacity(50)
return image
示例2: get_image
def get_image( self ):
# Open the image
try:
img_file = urllib.urlopen( self.path )
img_data = img_file.read()
bytes_read = len(img_data)
except urllib.HTTPError as e:
raise ImageRetrievalError(self.path, "Error code: %s" % e.code)
except urllib.URLError as e:
raise ImageRetrievalError(self.path, e.reason)
blob = Blob( img_data )
image = Image( blob )
# Check if the whole image should be used and cropped if necessary.
src_width = image.size().width()
src_height = image.size().height()
if self.width != src_width or self.height != src_height:
box = Geometry( self.width, self.height, self.x_min_src, self.y_min_src )
image.crop( box )
# Estimates the size in Bytes of this image part by scaling the number
# of Bytes read with the ratio between the needed part of the image and
# its actual size.
self.estimated_size = bytes_read * abs(float(self.width * self.height) /
float(src_width * src_height))
return image
示例3: ConvertJp2
def ConvertJp2(input):
"""
If the input is a jp2 picture, the image is transformed into bmp,
else the image is returned without any modifications.
@param input: A binary string representing the picture to convert
@type input: A string
@return: A binary string representing the picture in bmp, or the original input if the input is not a jp2 stream.
"""
jp2 = open("tmp.jp2", "wb")
jp2.write(input)
jp2.close()
if isMagick == False:
geojasper = "geojasper"
if (sys.platform != "win32") and os.path.isfile(
os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "geojasper", geojasper)
):
geojasper = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "geojasper", geojasper)
a = os.popen(geojasper + " -f tmp.jp2 -F tmp.jpg")
a.close()
else:
jpg = MImage("tmp.jp2")
jpg.write("tmp.jpg")
try:
f = open("tmp.jpg", "rb")
input = f.read()
f.close()
except IOError, msg:
pass
示例4: test_fromblob
def test_fromblob(self):
with open('../example/X.jpg', 'rb') as f:
data = f.read()
b = Blob(data)
img = Image(b)
img.write('X2.jpg')
self.assertEqual(type(img), Image)
示例5: _watermark
def _watermark(self, image, watermark_path, opacity, size, position_str):
with open(watermark_path, "rb") as watermark_file:
watermark = self.get_image(watermark_file)
image_size = self.get_image_size(image)
layer = Image(Geometry(image_size[0], image_size[1]), "transparent")
if opacity < 1:
self._reduce_opacity(watermark, opacity)
if not size:
mark_size = self.get_image_size(watermark)
else:
mark_size = tuple(
self._get_new_watermark_size(size, self.get_image_size(watermark))
)
options = {
"crop": "center",
"upscale": mark_size > self.get_image_size(watermark),
}
watermark = self.scale(watermark, mark_size, options)
watermark = self.crop(watermark, mark_size, options)
if position_str == "tile":
for x_pos in range(0, image_size[0], mark_size[0]):
for y_pos in range(0, image_size[1], mark_size[1]):
layer.composite(watermark, x_pos, y_pos, CoOp.OverCompositeOp)
else:
position = self._define_watermark_position(
position_str, image_size, mark_size
)
layer.composite(watermark, position[0], position[1], CoOp.OverCompositeOp)
image.composite(layer, 0, 0, CoOp.OverCompositeOp)
return image
示例6: pgmagick_image
def pgmagick_image(source, **options):
"""
Try to open the source file using pgmagick, ignoring any errors.
"""
# Use a StringIO wrapper because if the source is an incomplete file like
# object, PIL may have problems with it. For example, some image types
# require tell and seek methods that are not present on all storage
# File objects.
# import ipdb; ipdb.set_trace()
if not source:
return
source.open() # If tried by a previous source_generator, will be closed
source = StringIO(source.read())
try:
blob = Blob(source.read())
image = Image(blob)
except Exception:
logger.exception("unable to read image to create thumbnail")
return
if not image.isValid():
return
return convertGMtoPIL(image)
示例7: test_scale_jpeg
def test_scale_jpeg(self):
img = api.Image((400, 400), 'blue')
img.write(self.tmp_filename_jpg)
img2 = Image(Blob(open(self.tmp_filename_jpg).read()),
Geometry(200, 200))
img2.scale('200x200')
img2.write(self.tmp_filename_jpg)
示例8: test_image_setpixels
def test_image_setpixels(self):
img = Image(Geometry(300, 200), Color('transparent'))
pixels = img.setPixels(40, 50, 5, 5)
for pixel in pixels:
pixel.red = 50
img.syncPixels()
for pixel in img.getPixels(40, 50, 5, 5):
self.assertEqual(50, pixel.red)
示例9: get_sized_image
def get_sized_image(filename, size):
if not os.path.exists(DIR):
os.mkdir(DIR)
cache_hash = sha1('{0}\0{1}'.format(filename, size)).hexdigest() + '.jpg'
cache_path = os.path.join(DIR, cache_hash)
if not os.path.exists(cache_path):
im = Image(filename.encode('utf-8'))
im.sample(size)
im.write(cache_path)
return '/'.join(PARTS + (cache_hash,))
示例10: resize2
def resize2( srcFile="", destFile="", w=200,h=200 ):
blobData = Blob(open(srcFile).read())
if ( h != -1 ):
img = Image( blobData, Geometry(w, h))
img.scale("%dx%d!" % (w,h))
else:
img = Image( blobData )
img.scale("%dx!" % w )
img.profile("*",Blob())
img.write(destFile)
return "True"
示例11: get_image
def get_image( self ):
# Open the image
img_file = urllib.urlopen( self.path )
blob = Blob( img_file.read() )
image = Image( blob )
# Check if the whole image should be used and croped
# if necessary.
src_width = image.size().width()
src_height = image.size().height()
if self.width != src_width or self.height != src_height:
box = Geometry( self.width, self.height, self.x_min_src, self.y_min_src )
image.crop( box )
return image
示例12: test_color_histogram
def test_color_histogram(self):
redColor = Color('red')
im = Image(Geometry(30, 20), redColor)
histogram = im.colorHistogram()
self.assertEqual(1, len(histogram))
# test in, __getitem__
self.assertIn(redColor, histogram)
self.assertEqual(30 * 20, histogram[redColor])
# iteration example
for packet in histogram:
color, count = packet.key(), packet.data()
self.assertEqual(redColor, color)
self.assertEqual(30 * 20, count)
示例13: process_images
def process_images(subdir):
curr_dir = data_dir + subdir + '/'
counter = 1
files = os.listdir(curr_dir)
interval = int(len(files) / target)
for file in files:
if counter % interval == 0 and counter / interval <= target:
img = Image(curr_dir + '/' + file)
img.type(ImageType.GrayscaleType)
img.write(out_dir + subdir + str(counter / interval) + '.tif')
counter = counter + 1
示例14: _xor_composite_clip_layer
def _xor_composite_clip_layer(self, clips, layer_level=0):
"""
round two clipping.
"""
#skip the first clip as it doesn't need anything taken away from it.
this_clip = GMImage(os.path.join(self.clips_dir, 'clip-%s.png' %
clips[0]))
this_clip.write(os.path.join(self.clips_dir, "clip-co-%s.png" %
clips[0]))
#self.masks.append(os.path.join(self.clips_dir, 'clip-co-%s.png' %
#clips[0]))
clip_i = 0
for clip_id in clips[1:]:
previous_clip = GMImage(os.path.join(self.clips_dir, 'clip-%s.png' %
clips[clip_i]))
this_clip = GMImage(os.path.join(self.clips_dir, 'clip-%s.png' %
clip_id))
this_clip.composite(previous_clip, 0, 0, co.XorCompositeOp)
img_file = os.path.join(self.clips_dir, "clip-co-%s.png" % clip_id)
this_clip.write(img_file)
clip_i = clip_i + 1
im = Image.open(img_file)
if not im.getbbox(): # nothing there so delete it
os.unlink(img_file)
示例15: resize0
def resize0( srcFile="", destFile="", w=200 ):
img = Image(srcFile)
sw = img.columns()
sh = img.rows()
if ( sw > w ):
tw = w
th = sh*(float(w)/float(sw))
img.scale("%dx%d"%(tw,th))
img.profile("*", Blob())
img.write(destFile)
return "True"