Exemplo n.º 1
0
 def __drawPictureThumbnail(self):
     # load original image
     origImage = Bitmap.FromFile(self.filename, False)
     # calculate
     size = ThumbnailSize
     if self.__dims is None:
         self.__dims = (origImage.Width, origImage.Height)
     width, height = self.CalcResizedDims(size)
     drawWidth, drawHeight = width, height
     width, height = max(width, size), max(height, size)
     drawXOffset, drawYOffset = (width - drawWidth) / 2, (height -
                                                          drawHeight) / 2
     # draw new image
     newImage = Bitmap(width, height)
     g = Graphics.FromImage(newImage)
     g.InterpolationMode = InterpolationMode.HighQualityBicubic
     g.FillRectangle(Brushes.GhostWhite, 0, 0, width, height)
     g.DrawRectangle(Pens.LightGray, 0, 0, width - 1, height - 1)
     g.DrawImage(origImage, drawXOffset, drawYOffset, drawWidth, drawHeight)
     imageBytes = BitmapToBytes(newImage)
     # cleanup
     g.Dispose()
     newImage.Dispose()
     origImage.Dispose()
     return imageBytes
Exemplo n.º 2
0
class Image(object):
    ImageEncoders = dict([(i.MimeType, i)
                          for i in ImageCodecInfo.GetImageEncoders()])

    @staticmethod
    def ImageFromZip(zipFile, file):
        if ZipFile is None:
            raise Exception(
                "'Ionic.Zip.dll' not found! ZipFile not supported!")
        z = ZipFile(zipFile)
        m = MemoryStream()
        e = z[file]
        e.Extract(m)
        return Image(m)

    def __init__(self, file):
        self._file = file
        self._bitmap = Bitmap(file)

    @property
    def width(self):
        return self._bitmap.Width

    @property
    def height(self):
        return self._bitmap.Height

    def resize(self, width, height):
        rImg = Bitmap(width, height)
        g = Graphics.FromImage(rImg)
        g.InterpolationMode = HighQualityBicubic
        g.DrawImage(self._bitmap, 0, 0, width, height)
        g.Dispose()
        self._bitmap.Dispose()
        self._bitmap = rImg

    def crop(self, left=0, top=0, right=0, bottom=0):
        width = self._bitmap.Width - right
        height = self._bitmap.Height - bottom
        r = Rectangle(left, top, width, height)
        try:
            cropImage = self._bitmap.Clone(r, self._bitmap.PixelFormat)
        except:
            print "retrying  rectangle=%s" % str(r)
            self.resize(self._bitmap.Width + 10, self._bitmap.Height + 10)
            try:
                cropImage = self._bitmap.Clone(r, self._bitmap.PixelFormat)
            except Exception, e:
                print "Error in crop: %s" % str(e)
                return
        self._bitmap.Dispose()
        self._bitmap = cropImage
Exemplo n.º 3
0
 def DrawResizedImage(self, size):
     # load original image
     origImage = Bitmap.FromFile(self.filename, False)
     # calculate
     if self.__dims is None: self.__dims = (origImage.Width, origImage.Height)
     width, height = self.CalcResizedDims(size)
     # draw new image
     newImage = Bitmap(width, height)
     g = Graphics.FromImage(newImage)
     g.InterpolationMode = InterpolationMode.HighQualityBicubic
     g.DrawImage(origImage, 0, 0, width, height)
     imageBytes = BitmapToBytes(newImage)
     # cleanup
     g.Dispose()
     newImage.Dispose()
     origImage.Dispose()
     return imageBytes
Exemplo n.º 4
0
 def __drawFolderThumbnail(self, parent):
     size = ThumbnailSize
     # create new image
     newImage = Bitmap(size, size)
     g = Graphics.FromImage(newImage)
     g.InterpolationMode = InterpolationMode.HighQualityBicubic
     # draw background
     if parent: bc = ParentFolderColor
     else: bc = ChildFolderColor
     b = LinearGradientBrush(Point(0, 0), Point(size, size), bc,
                             Color.GhostWhite)
     g.FillRectangle(b, 0, 0, size, size)
     b.Dispose()
     g.DrawRectangle(Pens.LightGray, 0, 0, size - 1, size - 1)
     # draw up to 4 subitems
     folderItems = self.GetFirstFolderItems(4)
     delta = 10
     side = (size - 3 * delta) / 2 - 1
     rects = (Rectangle(delta + 3, delta + 12, side, side),
              Rectangle(size / 2 + delta / 2 - 3, delta + 12, side, side),
              Rectangle(delta + 3, size / 2 + delta / 2 + 6, side, side),
              Rectangle(size / 2 + delta / 2 - 3, size / 2 + delta / 2 + 6,
                        side, side))
     for rect, item in zip(rects, folderItems):
         subImage = Bitmap.FromStream(MemoryStream(item.thumbnail()), False)
         g.DrawImage(subImage, rect)
         subImage.Dispose()
     for rect in rects:
         g.DrawRectangle(Pens.LightGray, rect)
     # draw folder name
     if parent: name = '[..]'
     else: name = Path.GetFileName(self.path)
     f = Font('Arial', 10)
     g.DrawString(name, f, Brushes.Black,
                  RectangleF(2, 2, size - 2, size - 2))
     f.Dispose()
     # get the bytes of the image
     imageBytes = BitmapToBytes(newImage)
     # cleanup
     g.Dispose()
     newImage.Dispose()
     return imageBytes