当前位置: 首页 > news >正文

正规品牌网站设计地址小时seo加盟

正规品牌网站设计地址,小时seo加盟,如何在旅游网站上做攻略,官方微网站吗前言 主要整理记载一些python实现的小脚本,网上基本转换要会员,懒得搞了,这个一键生成,可以打包成exe文件使用 单张图片转换成pdf、图片批量转换成pdf # coding UTF-8 import os from io import BytesIO from PIL import Imag…

前言

主要整理记载一些python实现的小脚本,网上基本转换要会员,懒得搞了,这个一键生成,可以打包成exe文件使用

单张图片转换成pdf、图片批量转换成pdf

# coding = UTF-8
import os
from io import BytesIO
from PIL import Imageos.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'  # 防止中文乱码
SUPPORT_SUFFIX = ["jpg", "jpeg", "png"]  # 支持的图片文件格式def pic_to_pdf(image_bytes: bytes) -> bytes:"""将单个图片转换为单张PDF:param image_bytes: 图片的bytes对象:return: PDF的bytes对象"""# 将bytes对象转换为BytesIO对象image_bytes_io = BytesIO(image_bytes)# 从内存中读取图片image_object = Image.open(image_bytes_io)# 打开内存中的文件用于保存PDFwith BytesIO() as result_bytes_io:# 将图片保存为单张PDFimage_object.save(result_bytes_io, "PDF", resolution=100.0)# 获取内存中的文件data = result_bytes_io.getvalue()# 返回PDF的bytes对象return datadef batch_convert(image_path: str, pdf_path: str) -> None:"""批量将图片转换为单张PDF:param image_path: 图片的文件夹:param pdf_path: PDF文件保存的文件夹"""# 遍历文件夹下所有文件for root, dirs, files in os.walk(image_path, topdown=False):for name in files:# 提取文件的后缀名file_suffix = os.path.splitext(name)[-1].lstrip(".").lower()# 检测该文件格式是否受到支持if file_suffix not in SUPPORT_SUFFIX:continue# 拼接出图片文件的绝对路径source_file_path = os.path.join(root, name)# 拼接出PDF文件的绝对路径target_file_path = os.path.join(pdf_path, f"{os.path.splitext(name)[0]}.pdf")# 将图片文件转换为PDF文件with open(source_file_path, "rb") as source:with open(target_file_path, "wb") as target:target.write(pic_to_pdf(source.read()))# pic_to_pdf('E:\银登中心pdf\\f1669413880707_0.jpg')
batch_convert('E:\pdf\\f1669413880707', 'E:\pdf\\f1669413880707')

多张图片合并为1个pdf文件

import os
import re
import timeimport PIL.ExifTags
import PIL.Image
from reportlab.lib.pagesizes import A4
from reportlab.lib.utils import ImageReader
from reportlab.pdfgen import canvas
from reportlab.platypus import Imagedef img_search(mypath, filenames):for lists in os.listdir(mypath):path = os.path.join(mypath, lists)if os.path.isfile(path):expression = r'[\w]+\.(jpg|png|jpeg)$'if re.search(expression, path, re.IGNORECASE):filenames.append(path)elif os.path.isdir(path):img_search(path, filenames)def img_search1(mypath, filenames):for lists in os.listdir(mypath):path = os.path.join(mypath, lists)if os.path.isfile(path):a = path.split('.')if a[-1] in ['jpg', 'png', 'JPEG']:filenames.append(path)elif os.path.isdir(path):img_search1(path, filenames)def rotate_img_to_proper(image):try:# image = Image.open(filename)if hasattr(image, '_getexif'):  # only present in JPEGsfor orientation in PIL.ExifTags.TAGS.keys():if PIL.ExifTags.TAGS[orientation] == 'Orientation':breake = image._getexif()  # returns None if no EXIF dataif e is not None:# log.info('EXIF data found: %r', e)exif = dict(e.items())orientation = exif[orientation]# print('found, ',orientation)if orientation == 3:image = image.transpose(Image.ROTATE_180)elif orientation == 6:image = image.transpose(Image.ROTATE_270)elif orientation == 8:image = image.rotate(90, expand=True)except:passreturn imagedef main(src_folder=None):output_file_name = 'E:\pdf\\f1671228232790.pdf'# save_file_name = 'ex.pdf'# doc = SimpleDocTemplate(save_file_name, pagesize=A1,#                     rightMargin=72, leftMargin=72,#                     topMargin=72, bottomMargin=18)imgDoc = canvas.Canvas(output_file_name)  # pagesize=letterimgDoc.setPageSize(A4)document_width, document_height = A4if src_folder is None:mypath = input('Input the image folder please:')else:mypath = src_folderfilenames = []start = time.perf_counter()img_search(mypath, filenames)end = time.perf_counter()print('find file cost time: ', end - start, 'find files: ', len(filenames))# for f in filenames:#     print(f)for image in filenames:try:image_file = PIL.Image.open(image)image_file = rotate_img_to_proper(image_file)image_width, image_height = image_file.sizeprint('img size:', image_file.size)if not (image_width > 0 and image_height > 0):raise Exceptionimage_aspect = image_height / float(image_width)# Determins the demensions of the image in the overviewprint_width = document_widthprint_height = document_width * image_aspectimgDoc.drawImage(ImageReader(image_file), document_width - print_width,document_height - print_height, width=print_width,height=print_height, preserveAspectRatio=True)# inform the reportlab we want a new pageimgDoc.showPage()except Exception as e:print('error:', e, image)imgDoc.save()print('Done')if __name__ == '__main__':main(src_folder='E:\pdf\\f1671228232790')
http://www.hkea.cn/news/931372/

相关文章:

  • 宝安做棋牌网站建设找哪家公司好湖南长沙疫情最新消息
  • 四川专业网站建设中国十大企业培训机构排名
  • 怎么切页面做网站灰色词首页排名接单
  • 网站右侧浮动广告代码百度推广代理公司广州
  • 固原建站公司旺道seo推广系统怎么收费
  • 适合做外链的网站海外广告联盟平台推广
  • 建筑模板规格型号郑州厉害的seo顾问
  • ppt做书模板下载网站有哪些内容国际婚恋网站排名
  • 上海网站建设内容更新网络营销策划目的
  • 重庆市建设信息网站关键词查询网
  • 做哪种网站流量大怎么打广告宣传自己的产品
  • 免费表白网站制作seo网络优化推广
  • 网站建设中可能升级中国科技新闻网
  • 网站制作内容文案网站如何快速被百度收录
  • 淘宝淘宝网页版登录入口免费seo公司
  • 竹溪县县建设局网站短视频营销
  • 好的网站有哪些搜索引擎seo是什么意思
  • 做音乐网站赚钱吗做小程序的公司
  • 坪地网站建设域名流量查询工具
  • 网站建设部署万能推广app
  • 网站的重要性怎么做个网站
  • 做网站的经验百度旗下有哪些app
  • 化工网站开发推广点击器
  • 怎么访问日本竹中建设网站外贸seo推广
  • 惠阳建设局网站引流推广接单
  • 北京通州网站建设公司如何建立公司网站网页
  • 网站换程序301seo优化按天扣费
  • html5 网站自适应长尾关键词挖掘爱站工具
  • 网站设计公司(信科网络)潍坊网站定制模板建站
  • 番禺网站开发报价百度竞价排名软件