Пример #1
0
    def __init__(self, source_file, name_hint=""):
        # we will retain the original file object if AND ONLY IF it is directly readable by PIL,
        # so that it can be returned as-is for use as the original rendition in the case of web-usable formats
        self.file = None

        opened_with_pil = False
        try:
            self.image = Image.open(
                source_file)  # raises IOError if image can't be identified
            opened_with_pil = True
            self.file = source_file
        except IOError:
            basename, ext = os.path.splitext(os.path.basename(name_hint))
            if ext and ext[1:].lower() in ANSILOVE_C_READABLE_FORMATS:
                # write source file contents to a temporary file
                fd, input_path = tempfile.mkstemp(prefix=basename, suffix=ext)
                f = os.fdopen(fd, 'wb')
                source_file.seek(0)
                f.write(source_file.read())
                f.close()

                # run file through ansilove
                output_path = input_path + '.png'
                subprocess.check_call(
                    [ANSILOVE_C_PATH, input_path, '-o', output_path])

                # open result with PIL
                try:
                    self.image = Image.open(output_path)
                except IOError:
                    # On Linux (or just on the live server or something), ansilove adds a
                    # redundant .png to the output filename. LULZ.
                    output_path += '.png'
                    self.image = Image.open(output_path)
                self.image.load(
                )  # load pixel data and close file so we can delete it

                # delete input and output files
                os.remove(input_path)
                os.remove(output_path)

            else:
                # try pyrecoil
                try:
                    source_file.seek(0)
                    img = RecoilImage(name_hint, source_file)
                    self.image = img.to_pil()
                except ValueError:
                    raise IOError("Image format is not supported")

        if opened_with_pil and self.image.format not in PIL_READABLE_FORMATS:
            raise IOError("Image format is not supported")
Пример #2
0
	def __init__(self, source_file, name_hint=""):
		# we will retain the original file object if AND ONLY IF it is directly readable by PIL,
		# so that it can be returned as-is for use as the original rendition in the case of web-usable formats
		self.file = None

		opened_with_pil = False
		try:
			self.image = Image.open(source_file)  # raises IOError if image can't be identified
			opened_with_pil = True
			self.file = source_file
		except IOError:
			basename, ext = os.path.splitext(os.path.basename(name_hint))
			if ext and ext[1:].lower() in ANSILOVE_C_READABLE_FORMATS:
				# write source file contents to a temporary file
				fd, input_path = tempfile.mkstemp(prefix=basename, suffix=ext)
				f = os.fdopen(fd, 'wb')
				source_file.seek(0)
				f.write(source_file.read())
				f.close()

				# run file through ansilove
				output_path = input_path + '.png'
				subprocess.check_call([ANSILOVE_C_PATH, input_path, '-o', output_path])

				# open result with PIL
				try:
					self.image = Image.open(output_path)
				except IOError:
					# On Linux (or just on the live server or something), ansilove adds a
					# redundant .png to the output filename. LULZ.
					output_path += '.png'
					self.image = Image.open(output_path)
				self.image.load()  # load pixel data and close file so we can delete it

				# delete input and output files
				os.remove(input_path)
				os.remove(output_path)

			else:
				# try pyrecoil
				try:
					source_file.seek(0)
					img = RecoilImage(name_hint, source_file)
					self.image = img.to_pil()
				except ValueError:
					raise IOError("Image format is not supported")

		if opened_with_pil and self.image.format not in PIL_READABLE_FORMATS:
			raise IOError("Image format is not supported")
Пример #3
0
	def __init__(self, source_file, name_hint=""):
		self.file = source_file

		opened_with_pil = False
		try:
			self.image = Image.open(source_file)  # raises IOError if image can't be identified
			opened_with_pil = True
		except IOError:
			# try pyrecoil
			try:
				source_file.seek(0)
				img = RecoilImage(name_hint, source_file)
				self.image = img.to_pil()
			except ValueError:
				raise IOError("Image format is not supported")

		if opened_with_pil and self.image.format not in PIL_READABLE_FORMATS:
			raise IOError("Image format is not supported")
Пример #4
0
    def __init__(self, source_file, name_hint=""):
        # we will retain the original file object if AND ONLY IF it is directly readable by PIL,
        # so that it can be returned as-is for use as the original rendition in the case of web-usable formats
        self.file = None

        opened_with_pil = False
        try:
            self.image = Image.open(source_file)  # raises IOError if image can't be identified
            opened_with_pil = True
            self.file = source_file
        except IOError:
            # try pyrecoil
            try:
                source_file.seek(0)
                img = RecoilImage(name_hint, source_file)
                self.image = img.to_pil()
            except ValueError:
                raise IOError("Image format is not supported")

        if opened_with_pil and self.image.format not in PIL_READABLE_FORMATS:
            raise IOError("Image format is not supported")