本文整理汇总了Python中PythonMagick.Image.density方法的典型用法代码示例。如果您正苦于以下问题:Python Image.density方法的具体用法?Python Image.density怎么用?Python Image.density使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PythonMagick.Image
的用法示例。
在下文中一共展示了Image.density方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: bmp_or_jpg_to_pdf
# 需要导入模块: from PythonMagick import Image [as 别名]
# 或者: from PythonMagick.Image import density [as 别名]
def bmp_or_jpg_to_pdf(list_of_file_paths, destination_directory, delete_source=False):
operation_result = {}
invalid_file_paths = []
for files in list_of_file_paths:
if os.path.exists(files):
continue
else:
invalid_file_paths.append(files)
list_of_file_paths.remove(files)
for files in list_of_file_paths:
image = PyImage()
image.density("600")
image.read(files)
file_name = os.path.basename(files)
name, ext = os.path.splitext(file_name)
file_name_at_destination = os.path.join(destination_directory, name + ".pdf")
image.write(file_name_at_destination)
if delete_source is True:
for files in list_of_file_paths:
os.remove(files)
if not invalid_file_paths:
operation_result.update({"code": 0, "invalid_file_paths": "None"})
else:
invalid_files = ",".join(list_of_file_paths)
operation_result.update({"code": 1, "invalid_file_paths": invalid_files})
return operation_result
示例2: main
# 需要导入模块: from PythonMagick import Image [as 别名]
# 或者: from PythonMagick.Image import density [as 别名]
def main():
# os.environ["MAGICK_HOME"] = r"path_to_ImageMagick"
if len(sys.argv) == 1:
dirpath = '.'
else:
dirpath = sys.argv[1]
temp_dir = os.path.join(dirpath, '.temp')
if not os.path.exists(temp_dir):
os.mkdir(temp_dir)
for file_name in os.listdir(dirpath):
if not os.path.isfile(file_name) or not file_name.endswith('.pdf'):
continue
print('Converting file {} ...'.format(file_name))
in_path = os.path.join(dirpath, file_name)
with open(in_path, 'rb') as handle:
inputpdf = PdfFileReader(handle)
for i in xrange(inputpdf.numPages):
outputpdf = PdfFileWriter()
outputpdf.addPage(inputpdf.getPage(i))
new_file_name = file_name.replace('.pdf', '') + '_{}.pdf'.format(i)
new_in_path = os.path.join(temp_dir, new_file_name)
with open(new_in_path, 'wb') as handle:
outputpdf.write(handle)
output_file_name = new_file_name.replace('.pdf', '.jpeg')
output_path = os.path.join(dirpath, output_file_name)
p = Image()
p.density('1080')
p.read(os.path.abspath(new_in_path))
p.write(os.path.abspath(output_path))
os.remove(new_in_path)
os.rmdir(temp_dir)
示例3: pdf2images
# 需要导入模块: from PythonMagick import Image [as 别名]
# 或者: from PythonMagick.Image import density [as 别名]
def pdf2images(name):
np = getPdfNumPages(name)
for p in range(np):
i = Image()
i.density('200')
i.quality(100)
i.depth(24)
#i.backgroundColor(
#i.channel(
i.read(name + '[' + str(p) + ']')
i.write(name + str(p) + defaultImageExtension)
示例4: __init__
# 需要导入模块: from PythonMagick import Image [as 别名]
# 或者: from PythonMagick.Image import density [as 别名]
class Answers:
def __init__(self, filename, i):
self.image = Image()
self.image.density('%d' % DENSITY)
self.image.read('%s[%d]' % (filename, i))
temp = tempfile.NamedTemporaryFile(suffix='.png')
self.image.write(temp.name)
self.pimage = pygame.image.load(temp.name)
temp.close()
def draw(self, screen, C):
pygame.draw.rect(screen, (255, 255, 255), (0, 0, W, H), 0)
scaled = self.pimage
screen.blit(scaled, (0, 0), (C[0], C[1], C[2] - C[0], C[3] - C[1]))
pygame.display.flip()
示例5: pdf2jpg
# 需要导入模块: from PythonMagick import Image [as 别名]
# 或者: from PythonMagick.Image import density [as 别名]
def pdf2jpg(pdf,temp):
#Generate the path for the jpg file. Need to use a temp directory in case
#pdf location is read only.
pdf = str(pdf)
base = os.path.basename(pdf)
basefile = os.path.splitext(base)
jpg = temp + basefile[0] + ".jpg"
#jpg = str(jpg.replace("\\","\\\\"))
jpg = str(jpg)
pdf = str(pdf)
img = PMImage()
img.density('300')
img.depth(24)
img.read(pdf)
img.write(jpg)
img = Image.open(jpg)
rgbimg = Image.new("RGBA", img.size)
rgbimg.paste(img)
rgbimg.save(jpg)
return jpg
示例6: pdftojpg
# 需要导入模块: from PythonMagick import Image [as 别名]
# 或者: from PythonMagick.Image import density [as 别名]
def pdftojpg(fname):
reader = PdfFileReader(open(fname, "rb"))
directory = os.path.basename(fname)
if not os.path.exists(directory):
os.makedirs(directory)
else:
shutil.rmtree(directory)
time.sleep(2)
os.makedirs(directory)
for page_num in range(reader.getNumPages()):
writer = PdfFileWriter()
writer.addPage(reader.getPage(page_num))
temp = NamedTemporaryFile(prefix=str(page_num), suffix=".pdf", delete=False)
writer.write(temp)
temp.close()
im = Magick_Image()
im.density("300") # DPI, for better quality
im.read(temp.name)
im.write(directory+"/some%d.jpg" % (int(page_num)+1))
os.remove(temp.name)
return(directory)
示例7: tiff_to_pdf
# 需要导入模块: from PythonMagick import Image [as 别名]
# 或者: from PythonMagick.Image import density [as 别名]
def tiff_to_pdf(list_of_file_paths, destination_directory):
"""
This method converts individual TIFF image files to PDF.
:param list_of_file_paths: This argument is the list of absolute file paths for example
['C:/User/Documents/Images/Image1.tiff', 'C:/User/Documents/Images/Image2.tiff' ]
:param destination_directory: Pass in the absolute path to the directory you want the
converted files to be saved to.
:return: This method returns a dictionary giving information about the success or failure
of the operation and also gives the list of files that failed the conversion..
"""
operation_result = {}
invalid_file_paths = []
for files in list_of_file_paths:
if os.path.exists(files):
continue
else:
invalid_file_paths.append(files)
list_of_file_paths.remove(files)
if not os.path.exists(os.path.join(os.getcwd(), "temp_dir")):
os.mkdir(os.path.join(os.getcwd(), "temp_dir"))
path_to_temp = os.path.join(os.getcwd(), "temp_dir")
for image in list_of_file_paths:
temp_file_list = []
img = Image.open(image)
tif_file_full_name = os.path.basename(image)
tif_file_name, tif_file_ext = os.path.splitext(tif_file_full_name)
for i in range(100):
try:
img.seek(i)
temp_file_list.append(os.path.join(path_to_temp, 'page' + str(i + 1) + ".tif"))
img.save(os.path.join(path_to_temp, 'page' + str(i + 1) + ".tif"))
except EOFError:
break
png_temp_array = []
for tif_img in temp_file_list:
img_png = Image.open(tif_img)
file_name = os.path.basename(tif_img)
name, ext = os.path.splitext(file_name)
file_name_at_destination = os.path.join(path_to_temp, name + ".png")
png_temp_array.append(file_name_at_destination)
img_png.save(file_name_at_destination)
os.remove(tif_img)
pdf_temp_array = []
for png_img in png_temp_array:
image = PyImage()
image.density("600")
image.read(png_img)
png_file_name = os.path.basename(png_img)
png_name, png_ext = os.path.splitext(png_file_name)
file_name_at_destination = os.path.join(path_to_temp, png_name + ".pdf")
pdf_temp_array.append(file_name_at_destination)
image.write(file_name_at_destination)
os.remove(png_img)
if pdf_temp_array.__len__() > 1:
ImageOpr.merge_pdf(pdf_temp_array, destination_directory, tif_file_name, delete_source=False)
for pdfs in pdf_temp_array:
os.remove(pdfs)
else:
os.rename(pdf_temp_array[0], os.path.join(path_to_temp, tif_file_name + ".pdf"))
destination_directory_file_name = os.path.join(destination_directory, tif_file_name + ".pdf")
shutil.move(os.path.join(path_to_temp, tif_file_name + ".pdf"), destination_directory_file_name)
shutil.rmtree(path_to_temp, ignore_errors=True)
if not invalid_file_paths:
operation_result.update({"code": 0, "invalid_file_paths": "None"})
else:
invalid_files = ",".join(list_of_file_paths)
operation_result.update({"code": 1, "invalid_file_paths": invalid_files})
return operation_result
示例8: PdfFileReader
# 需要导入模块: from PythonMagick import Image [as 别名]
# 或者: from PythonMagick.Image import density [as 别名]
import os
from pyPdf import PdfFileReader, PdfFileWriter
from tempfile import NamedTemporaryFile
from PythonMagick import Image
import sys
pdfname=sys.argv[1]
#os.system("rm /tmp/test2/*")
#os.system("mkdir /tmp/test2")
cmd="cp /opt/%s /tmp/test2/ocr.pdf"%(pdfname)
#os.system("cp /opt/corrected.html /tmp/test2/some_0.hocr")
os.system(cmd)
reader = PdfFileReader(open("/tmp/test2/ocr.pdf", "rb"))
for page_num in xrange(reader.getNumPages()):
writer = PdfFileWriter()
writer.addPage(reader.getPage(page_num))
temp = NamedTemporaryFile(prefix=str(page_num), suffix=".pdf", delete=False)
writer.write(temp)
temp.close()
im = Image()
im.density("300") # DPI, for better quality
im.read(temp.name)
im.write("/tmp/test2/some_%d.jpg" % (page_num))
os.remove(temp.name)
示例9: __init__
# 需要导入模块: from PythonMagick import Image [as 别名]
# 或者: from PythonMagick.Image import density [as 别名]
#.........这里部分代码省略.........
return self._x
def GetY(self):
if self._y:
return self._y
map_geometry = self._GetMapGeometry()
offset = _MAGIC_COORDINATE_OFFSET_ * self._scale_factor
width = _MAGIC_COORDINATE_BBOX_HEIGHT_ * self._scale_factor
height = _MAGIC_COORDINATE_BBOX_WIDTH_ * self._scale_factor
coordinate_geometry = Geometry(width, height, map_geometry.xOff() - offset - width, map_geometry.yOff())
image = self._CropGeometry(coordinate_geometry)
image.rotate(90)
self._y = self._ocr.GetDecimalDegrees(image)
return self._y
def GetWidth(self):
if self._width:
return self._width
map_geometry = self._GetMapGeometry()
offset = _MAGIC_COORDINATE_OFFSET_ * self._scale_factor
width = _MAGIC_COORDINATE_BBOX_WIDTH_ * self._scale_factor
height = _MAGIC_COORDINATE_BBOX_HEIGHT_ * self._scale_factor
x_offset = map_geometry.xOff() + map_geometry.width()
y_offset = map_geometry.yOff() + map_geometry.height()
coordinate_geometry = Geometry(width, height, x_offset - width, y_offset + offset)
image = self._CropGeometry(coordinate_geometry)
self._width = self._ocr.GetDecimalDegrees(image) - self.GetX()
return self._width
def GetHeight(self):
if self._height:
return self._height
map_geometry = self._GetMapGeometry()
offset = _MAGIC_COORDINATE_OFFSET_ * self._scale_factor
width = _MAGIC_COORDINATE_BBOX_HEIGHT_ * self._scale_factor
height = _MAGIC_COORDINATE_BBOX_WIDTH_ * self._scale_factor
x_offset = map_geometry.xOff() + map_geometry.width()
y_offset = map_geometry.yOff() + map_geometry.height()
coordinate_geometry = Geometry(width, height, x_offset + offset, y_offset - height)
image = self._CropGeometry(coordinate_geometry)
image.rotate(90)
self._height = self._ocr.GetDecimalDegrees(image) - self.GetY()
return self._height
def _RefreshCoordinates(self):
if self.IsValid():
return
self._x = 0.0
self._y = 0.0
self._width = 0.0
self._height = 0.0
self.GetWidth()
self.GetHeight()
def _CropGeometry(self, geometry):
if not self._map_image:
self._GenerateImage()
image = Image(self._map_image)
image.crop(geometry)
return image
def _GetMapGeometry(self):
width = self.WIDTH - self.MARGIN_LEFT - self.MARGIN_RIGHT
height = self.HEIGHT - self.MARGIN_TOP - self.MARGIN_BOTTOM
width *= self._scale_factor
height *= self._scale_factor
margin_left = self.MARGIN_LEFT * self._scale_factor
margin_top = self.MARGIN_TOP * self._scale_factor
return Geometry(width, height, margin_left, margin_top)
def _GenerateImage(self):
scaled_density = 72 * self._scale_factor
self._map_image = Image()
self._map_image.density("%dx%d" % (scaled_density, scaled_density))
self._map_image.read(self._map_path)