コード例 #1
0
ファイル: make_ico.py プロジェクト: Paradiseeee/Heaven
 def generate(self):
     os.chdir(self.path)
     img = Image(self.filename)
     img.sample("128x128")
     name = self.filename.split('.')[0]
     img.write(name + '.ico')
     print(f'>>> 成功写出文件:{self.path + name}.ico')
コード例 #2
0
ファイル: gettags.py プロジェクト: offbyone/cjb
def extractCover(uuid, data):
	"""Extract the cover, resize, and save."""
	f = NamedTemporaryFile(delete=False)
	f.write(data)
	f.close()
	image = Image(f.name)
	image.magick('JPEG') 
	image.sample('256x256')
	image.write('htdocs/images/'+uuid+'.jpg')
	unlink(f.name)
コード例 #3
0
def pad_image_to_x480(file):
    from PythonMagick import Image, CompositeOperator
    fname = file.split(".")[0]
    ext = file.split(".")[-1]
    outfile = os.path.join(destdir, fname + "_" + "l" + ".jpg")

    ## Make BG layer
    bgimg = Image('400x480', 'white')

    ## Open Primary image 
    img = Image(file)
    img.backgroundColor("white")
    img.sample('350x432')

    # Composite + Save Primary over bg, padding primary with white of bg
    type = img.type
    img.composite(bgimg, 0, 0, CompositeOperator.DstOverCompositeOp)
    img.magick('JPG')
    img.type = type
    img.quality(100)
    img.write(outfile)
コード例 #4
0
def convert(file_name, target_width=1500):
    try:
        with Image(filename=file_name) as img:
            image_page_num = len(img.sequence)
            # PDF里面只有一张图片
            if image_page_num == 1:
                # 获取最终图片宽高
                target_width, target_height = _get_one_info(
                    target_width, img.width, img.height)
                # 缩放,文档上说比resize速度快
                img.sample(target_width, target_height)

                # 如果最终高度大于百度最大高度,则crop
                if target_height > bai_du_ocr_max:
                    img.crop(0, 0, target_width, bai_du_ocr_max)

                # img.save(filename='%s.jpg' % (str(int(time.time())) + '_' +
                # str(img.width)))
                result = img.make_blob('jpg')
                # 下面是准备二值化,发现总体速度还不如直接传给百度
                # paste_image =
                # PImage.open(StringIO.StringIO(img.make_blob('jpg')))
                # paste_image = paste_image.convert("L")
                # paste_image.show()
                # d = StringIO.StringIO()
                # paste_image.save(d, 'JPEG')
                # result = d.getvalue()
            # PDF里面有一张以上图片
            else:
                # 多张时,获取最终宽高、拼接页数
                target_width, target_height, page_num = _get_more_info(
                    target_width, img.width, img.height, image_page_num)
                # 生成粘贴的背景图 (测试多次,发现L比RGB快)
                paste_image = PImage.new('L', (target_width, target_height))
                # 拼接图片
                for i in range(0, page_num):
                    image = Image(image=img.sequence[i])
                    # 计算一张图的高度
                    one_img_height = int(target_height / page_num)
                    # 缩放
                    image.sample(target_width, one_img_height)
                    # 将wand库文件转成PIL库文件
                    pasted_image = PImage.open(
                        StringIO.StringIO(image.make_blob('jpg')))
                    # 将图片粘贴到背景图
                    paste_image.paste(pasted_image, (0, one_img_height * i))
                    # 如果最终高度大于百度最大高度,则crop
                    if target_height > bai_du_ocr_max:
                        paste_image = paste_image.crop(
                            (0, 0, target_width, bai_du_ocr_max))
                    # 从内存中读取文件
                    d = StringIO.StringIO()
                    # 这里是JPEG不是JPG
                    paste_image.save(d, 'JPEG')
                    result = d.getvalue()
                    # paste_image.save('%s.jpg' % (str(int(time.time())) + '_' +
                    # str(img.width)))
                    # 测试的时候可以打开
                    # paste_image.show()
    except Exception as e:
        print(e)
        result = False
    return result
コード例 #5
0
ファイル: easyicon.py プロジェクト: CMCboy/easyicon
def pngtoico(filename):  #png转ico
    from PythonMagick import Image  # https://www.lfd.uci.edu/~gohlke/pythonlibs/#pythonmagick
    img = Image(filename)
    # 这里要设置一下尺寸,不然会报ico尺寸异常错误
    img.sample('128x128')
    img.write(filename.split('.')[0] + '.ico')
コード例 #6
0
import os

from PyPDF4 import PdfFileReader, PdfFileWriter
from tempfile import NamedTemporaryFile
from PythonMagick import Image

reader = PdfFileReader(open("D:/data/pdf-scan/普通生物学(清晰PDF版).pdf", "rb"))
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)
    print(temp.name)
    tempname = temp.name
    temp.close()
    im = Image(tempname)
    im.quality(100)  # 0-100 full compression
    # 不保持比例
    #im.sample('298x412!')
    # 保持比例
    im.sample('1788x2526')
    #im.density("3000") # DPI, for better quality
    # im.read(tempname)
    im.write("D:/data/pdf-scan/temp-pdf/{}.jpeg".format(page_num))

    os.remove(tempname)