def __init__(self, filename=None, color=None, *args, **kargs): self.img = None if sys.version_info >= (3, ) and isinstance(filename, (str)): self.img = pgmagick.Image(str(filename)) elif sys.version_info < (3, ) and isinstance(filename, (unicode, str)): self.img = pgmagick.Image(str(filename)) elif isinstance(filename, (list, tuple)): size = filename geometry = pgmagick.Geometry(int(size[0]), int(size[1])) if isinstance(color, (list, tuple)): r, g, b = int(color[0]), int(color[1]), int(color[2]) color = pgmagick.Color(r, g, b) self.img = pgmagick.Image(geometry, color) elif isinstance(color, str): if color.find('gradient') == 0 or color.find('plasma') == 0: self.img = pgmagick.Image(geometry, pgmagick.Color()) self.img.read(color) else: color = pgmagick.Color(color) self.img = pgmagick.Image(geometry, color) else: self.img = pgmagick.Image(geometry, pgmagick.Color()) self.img.write(pgmagick.Blob(), 'MIFF') else: self.img = pgmagick.Image()
def pgmagick_scale_plus_sharpen(filename, width, height): im = pgmagick.Image(pgmagick.Blob(open(filename).read()), pgmagick.Geometry(width, height)) im.scale('%dx%d' % (width, height)) im.sharpen(1) im.quality(95) im.write('outpgsharpen.jpg')
def _resize_using_pg(self, image, width, height, mode): """ Resize using image mode. """ blob = pg.Blob(image) blob_out = pg.Blob() img = pg.Image(blob) img.filterType(pg.FilterTypes.LanczosFilter) img = process_image_with_mode(img, width, height, mode) # Image should be repaged after a crop/resize img.page(pg.Geometry(0, 0, 0, 0)) img.quality(90) # minimise artifacts but keep size down img.write(blob_out, 'JPEG') return blob_out.data, img.size().width(), img.size().height()
def _resize_using_pg(self, image, width, height, mode): """ Resize using image mode. """ blob = pg.Blob(image) blob_out = pg.Blob() img = pg.Image(blob) img.filterType(pg.FilterTypes.LanczosFilter) img = process_image_with_mode(img, width, height, mode) # Image should be repaged after a crop/resize img.page(pg.Geometry(0, 0, 0, 0)) if settings.IMAGE_QUALITY is not None: # May be handled by custom mode img.quality(settings.IMAGE_QUALITY) img.write(blob_out, 'JPEG') return blob_out.data, img.size().width(), img.size().height()
def _pgmagick(self): '''When an error is encountered while opening an image, run it through pgmagick since it is a lot more forgiving of errors (truncation, bad headers, etc). This seems to be rare, but this way we can process more things successfully. We want to still use PIL for all other operations we perform since they are faster than pgmagick.''' if self._pgmagick_ran: raise BadImage(_('Already converted with pgmagick')) self._pgmagick_ran = True blob = pgmagick.Blob(self.raw) image = pgmagick.Image() image.ping(blob) self._check_info( dict(format=image.magick(), width=image.columns(), height=image.rows())) image = pgmagick.Image(blob) image.quality(self.config['quality']) blob = pgmagick.Blob() image.write(blob) self.raw = blob.data self.profile.mark_time('pgmagick') self.profile.mark('pgmagick_size', len(self.raw))
def fingerprint_image(filename): img = pgmagick.Image(fix_gm_filename(filename)) img.sample('160x160!') img.modulate(100.0, -100.0, 100.0) # saturation=-100. img.blur(3, 99) # radius=3, sigma=99. img.normalize() img.equalize() img.sample('16x16') img.threshold(half_threshold) img.magick('mono') blob = pgmagick.Blob() img.write(blob) # The output of the following command is identical to blob.data, but it's # diferent from `convert' (ImageMagick) instead of `gm # convert' (GraphicsMagick): even the output of (-sample '160x160!') is # different. # # gm convert "${filename}" -sample '160x160!' -modulate 100,-100,100 -blur 3x99 -normalize -equalize -sample '16x16' -threshold 50% mono:t.out return blob.data # 32 bytes.
#!/usr/bin/env python import pgmagick im = pgmagick.Image('existing.tif') pdf = pgmagick.ImageList() pdf.append(im) pdf.append(im) pdf.writeImages('new.pdf') blob = pgmagick.Blob() pdf.writeImages(blob) print blob.length()
def pgmagick_scale_from_blob(filename, width, height): im = pgmagick.Image(pgmagick.Blob(open(filename).read()), pgmagick.Geometry(width, height)) im.scale('%dx%d' % (width, height)) im.quality(95) im.write('outpg_fromblob.jpg')