def get_thumbnail(img): with NamedTemporaryFile() as thumb: img = imgopen(StringIO(img)) img.thumbnail((300, 300)) img.save(thumb.name, "JPEG") thumb.file.flush() return thumb.read()
def loadTexture(filename): im = imgopen(filename) try: im = im.convert('RGB') ix, iy, image = im.size[0], im.size[1], im.tostring("raw", "RGBA", 0, -1) except SystemError: ix, iy, image = im.size[0], im.size[1], im.tostring("raw", "RGBX", 0, -1) assert ix*iy*4 == len(image), """Unpacked image size for texture is incorrect""" texID = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texID) glPixelStorei(GL_UNPACK_ALIGNMENT, 1) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexImage2D(GL_TEXTURE_2D, 0, 3, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, image) return texID
def __init__(self, tl, ipath=None, resize=False): """ initialization :param tl: the Toplevel widget for this panel (managed directly by the window manger) :param ipath: path of icon (if one) to display the title bar :param resize: resize of Panel or not """ ttk.Frame.__init__(self, tl) # NOTE: we have run into issues if the opened image is not stored as # a member of the class self._appicon = PhotoImage(imgopen(ipath)) if ipath else None if self._appicon: self.tk.call('wm', 'iconphoto', self.master._w, self._appicon) self.master.protocol("WM_DELETE_WINDOW", self.delete) self._panels = {} self.grid(row=0, column=0, sticky='nwse') if not resize: self.master.resizable(0, 0)
def loadTexture(filename): im = imgopen(filename) try: im = im.convert('RGB') ix, iy, image = im.size[0], im.size[1], im.tostring( "raw", "RGBA", 0, -1) except SystemError: ix, iy, image = im.size[0], im.size[1], im.tostring( "raw", "RGBX", 0, -1) assert ix * iy * 4 == len( image), """Unpacked image size for texture is incorrect""" texID = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texID) glPixelStorei(GL_UNPACK_ALIGNMENT, 1) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexImage2D(GL_TEXTURE_2D, 0, 3, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, image) return texID
def recognize(filedata, accepted_languages, force_detection): with NamedTemporaryFile() as infile: infile.write(filedata) infile.file.flush() with NamedTemporaryFile() as textfile: retval = ocr(infile.name, textfile.name) img = imgopen(infile.name) if retval: detected_languages = [] lang = "UNKNOWN" else: lang = guessLanguage(textfile.read().decode('utf-8')) detected_languages = [lang] final_filename = infile.name + '-rotated' try: for rotation in (180, 90, 180, 0): if lang in accepted_languages: textfile.seek(0) return lang, img, textfile.read().decode('utf-8') img = img.rotate(rotation) img.save(final_filename, "JPEG") retval = ocr(final_filename, textfile.name) if retval: continue textfile.seek(0) lang = guessLanguage(textfile.read().decode('utf-8')) detected_languages.append(lang) finally: try: remove(final_filename) except OSError: pass if force_detection: raise TypeError( "Languages %s not in range of accepted " "languages %s" % (str(detected_languages), str(accepted_languages))) return lang, img, textfile.read().decode('utf-8')
def recognize(filedata, accepted_languages, force_detection): with NamedTemporaryFile() as infile: infile.write(filedata) infile.file.flush() with NamedTemporaryFile() as textfile: retval = ocr(infile.name, textfile.name) img = imgopen(infile.name) if retval: detected_languages = [] lang = "UNKNOWN" else: lang = guessLanguage(textfile.read().decode('utf-8')) detected_languages = [lang] final_filename = infile.name + '-rotated' try: for rotation in (180, 90, 180, 0): if lang in accepted_languages: textfile.seek(0) return lang, img, textfile.read().decode('utf-8') img = img.rotate(rotation) img.save(final_filename, "JPEG") retval = ocr(final_filename, textfile.name) if retval: continue textfile.seek(0) lang = guessLanguage(textfile.read().decode('utf-8')) detected_languages.append(lang) finally: try: remove(final_filename) except OSError: pass if force_detection: raise TypeError("Languages %s not in range of accepted " "languages %s" % (str(detected_languages), str(accepted_languages))) return lang, img, textfile.read().decode('utf-8')
def redpi(path, append=0, pictype="png", outimg=None): ##将大于、小于96dpi的都转换成96dpi files = map(lambda x: os.path.join(path, x), os.listdir(path)) imgs, width, height = [], 0, 0 for file in files: img = imgopen(file) # img=Image().im # img=imgt img2 = img.copy() if pictype not in ["gif", "GIF"]: scale = img.info['dpi'] scale2 = max(scale[0], 96) img.save(file, dpi=(scale2, scale2)) else: img.save(file) img = None if append: img2 = img2.convert('RGB') if img2.mode != "RGB" else img2 imgs.append(img2) width = img2.size[0] if img2.size[0] > width else width height += img2.size[1] img2 = None if append: pasteimg(imgs, width, height, outimg)
def get_crowns(img_relative_path): img = imgopen(img_relative_path) img = img.point(lambda x: x * 0.95) img = rgb2gray(array(img)) recursive_mask_and_crop(img)
headers = {'user-agent': 'Mozilla/5.0'} r = get(url, headers=headers) if r.status_code == 200: imgurl = url + '/' + search(r'background-image:url\(.*.jpg', r.text).group()[22:] # 正则匹配图片链接 imgname = BeautifulSoup(r.text, 'lxml').find('a', id='sh_cp').get('title') # 获得图片标题 for i in range(30): img = get(imgurl) # 下载图片 if img.status_code == 200: with open(imgpath, 'wb') as fp: # 保存图片 fp.write(img.content) # 将图片标题加入图片右下角并重新保存 with imgopen(imgpath) as img: x, y = img.size draw = Draw(img) font = truetype('C:/Windows/Fonts/msyhbd.ttc', 20) draw.text((x - len(imgname) * 13, y - 30), imgname, (255, 255, 255), font=font) img.save(imgpath) subprocess.call( "powershell.exe . ./Set-Wallpaper; Set-Wallpaper -Path %s" % imgpath, False) break else: #顺利循环完 30 次没有 break,说明下载失败 pass else: pass