def imdirect_open(fp): """Opens, identifies the given image file, and rotates it if it is a JPEG. Note that this method does NOT employ the lazy loading methodology that the PIL Images otherwise use. This is done to avoid having to save new Args: fp: A filename (string), pathlib.Path object or a file-like object. Returns: The image as an :py:class:`~PIL.Image.Image` object. Raises: IOError: If the file cannot be found, or the image cannot be opened and identified. """ img = pil_open(fp, 'r') if img.format == 'JPEG': # Read Exif tag on image. if isinstance(fp, string_types): exif = piexif.load(text_type_to_use(fp)) else: fp.seek(0) exif = piexif.load(fp.read()) # If orientation field is missing or equal to 1, nothing needs to be done. orientation_value = exif.get('0th', {}).get(piexif.ImageIFD.Orientation) if orientation_value is None or orientation_value == 1: return img # Otherwise, rotate the image and update the exif accordingly. img_rot = autorotate(img) exif = update_exif_for_rotated_image(exif) # Now, lets restore the output image to # PIL.JpegImagePlugin.JpegImageFile class with the correct, # updated Exif information. # Save image as JPEG to get a correct byte representation of # the image and then read it back. with io.BytesIO() as bio: img_rot.save(bio, format='jpeg', exif=piexif.dump(exif)) bio.seek(0) img_rot_new = pil_open(bio, 'r') # Since we use a BytesIO we need to avoid the lazy # loading of the PIL image. Therefore, we explicitly # load the data here. img_rot_new.load() img = img_rot_new return img
def clean_image(self): data = self.cleaned_data["image"] if data: if data.size > 41943040: raise forms.ValidationError("Image must be smaller than 5MB") self.image_format = pil_open(data.file).format if not self.image_format in self.IMAGE_FORMATS: raise forms.ValidationError("Images cannot be of type %s" % data.content_type) return data
def clean_image(self): data = self.cleaned_data["image"] if data: if data.size > 4194304: raise forms.ValidationError(_("Community images cannot be larger than 512K")) self.image_format = pil_open(data.file).format if not self.image_format in GroupForm.IMAGE_FORMATS: raise forms.ValidationError(_("Images cannot be of type %(type)s") % {"type": data.content_type}) return data
def loadImage(imageName): im = pil_open(imageName) try: 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) ID = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, ID) glPixelStorei(GL_UNPACK_ALIGNMENT, 4) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, image) return ID
def clean_image(self): data = self.cleaned_data["image"] if data: if data.size > 4194304: raise forms.ValidationError( "Community images cannot be larger than 512K") self.image_format = pil_open(data.file).format if not self.image_format in GroupForm.IMAGE_FORMATS: raise forms.ValidationError("Images cannot be of type %s" % data.content_type) return data
def difference(img, dest): fiarr = numpy.asarray(Image(img)) fiarr = fiarr[..., ::-1] pilarr = numpy.asarray(pil_open(img)) fiarr = fiarr.astype(numpy.int16) pilarr = pilarr.astype(numpy.int16) diff = numpy.absolute(fiarr - pilarr) toimage(diff).convert("L").save(dest)
def loadImage(imageName): im = pil_open(imageName) try: 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) ID = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, ID) glPixelStorei(GL_UNPACK_ALIGNMENT,4) glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, image ) return ID
def test_toBuffer_PIL(self): try: from PIL.Image import open as pil_open except ImportError: return imgbuf = self.img.toBuffer() pilimg = pil_open(imgbuf) self.assertEqual(pilimg.format, 'JPEG') self.assertEqual(pilimg.size, self.img.size) del pilimg pilimg = self.img.toPIL(format=fi.FIF_PNG) self.assertEqual(pilimg.format, 'PNG') del pilimg
def _save_with_exif_and_return_PIL_image(mat, orientation_value): i = Image.new('L', (len(mat[0]), len(mat))) i.putdata(list(chain(*mat))) exif = {'0th': {piexif.ImageIFD.Orientation: orientation_value}} with io.BytesIO() as b: i.save(b, format='jpeg', exif=piexif.dump(exif), quality=100, subsampling=0) b.seek(0) i = pil_open(b) i.load() i.putdata(list(chain(*mat))) return i
def _validate_image(image_path): """ Check file existence, image is correct PNG, dimension is 512x? or ?x512 and file size < 350KB :param image_path: The image for a sticker :return: Boolean if all limits on stickers met """ try: with pil_open(image_path) as image: criteria = [ max(image.size) == 512, image.format == 'PNG', image_path.stat().st_size < 350 * 1000 ] return True if all(criteria) else False except IOError: print(ERROR_INVAILD_STICKER_IMAGE % image_path.name) return False # invalid image or just 404
def load_image(file_name,texture_id): im = pil_open(file_name) try: width,height,image = im.size[0], im.size[1], im.tobytes("raw", "RGBX", 0, -1) except SystemError: width,height,image = im.size[0], im.size[1], im.tobytes("raw", "RGBX", 0, -1) # To use OpenGL 4.2 ARB_texture_storage to automatically generate a single mipmap layer # uncomment the 3 lines below. Note that this should replaced glTexImage2D below. #bind_texture(texture_id,'DEFAULT') #glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, width, height); #glTexSubImage2D(GL_TEXTURE_2D,0,0,0,width,height,GL_RGBA,GL_UNSIGNED_BYTE,image) # "Bind" the newly created texture : all future texture functions will modify this texture bind_texture(texture_id,'MIN_FILTER') glTexImage2D( GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image ) return texture_id
def load_image(file_name): im = pil_open(file_name) try: width,height,image = im.size[0], im.size[1], im.tostring("raw", "RGBA", 0, -1) except SystemError: width,height,image = im.size[0], im.size[1], im.tostring("raw", "RGBX", 0, -1) texture_id = glGenTextures(1) # To use OpenGL 4.2 ARB_texture_storage to automatically generate a single mipmap layer # uncomment the 3 lines below. Note that this should replaced glTexImage2D below. #bind_texture(texture_id,'DEFAULT') #glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, width, height); #glTexSubImage2D(GL_TEXTURE_2D,0,0,0,width,height,GL_RGBA,GL_UNSIGNED_BYTE,image) # "Bind" the newly created texture : all future texture functions will modify this texture bind_texture(texture_id,'MIN_FILTER') glTexImage2D( GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image ) return texture_id
def update_exif_for_rotated_image(exif): """Modifies the Exif tag if rotation has been performed. 0th, 1st -------- ImageWidth = 256 ImageLength = 257 XResolution = 282 YResolution = 283 TileWidth = 322 TileLength = 323 Exif ---- PixelXDimension = 40962 PixelYDimension = 40963 Args: exif (dict): The parsed Exif tag Returns: The modified Exif dict. """ orientation_value = exif.get('0th', ).get( piexif.ImageIFD.Orientation, exif.get('1st', ).get( piexif.ImageIFD.Orientation, None)) if orientation_value is not None: # Update orientation. exif['0th'][piexif.ImageIFD.Orientation] = 1 if exif.get('1st', {}).get(piexif.ImageIFD.Orientation) is not None: exif['1st'][piexif.ImageIFD.Orientation] = 1 # If 90 or 270 degree rotation, x dimensions are now y dimensions, # so flip all such properties. if orientation_value > 4: for exif_tag in ['0th', '1st']: if exif.get(exif_tag) is not None: x, y = (exif.get(exif_tag).get(piexif.ImageIFD.ImageWidth), exif.get(exif_tag).get(piexif.ImageIFD.ImageLength)) if x is not None and y is not None: exif[exif_tag][piexif.ImageIFD.ImageWidth] = y exif[exif_tag][piexif.ImageIFD.ImageLength] = x x, y = (exif.get(exif_tag).get(piexif.ImageIFD.XResolution), exif.get(exif_tag).get(piexif.ImageIFD.YResolution)) if x is not None and y is not None: exif[exif_tag][piexif.ImageIFD.XResolution] = y exif[exif_tag][piexif.ImageIFD.YResolution] = x x, y = (exif.get(exif_tag).get(piexif.ImageIFD.TileWidth), exif.get(exif_tag).get(piexif.ImageIFD.TileLength)) if x is not None and y is not None: exif[exif_tag][piexif.ImageIFD.TileWidth] = y exif[exif_tag][piexif.ImageIFD.TileLength] = x if exif.get('Exif') is not None: x, y = (exif.get('Exif').get(piexif.ExifIFD.PixelXDimension), exif.get('Exif').get(piexif.ExifIFD.PixelYDimension)) if x is not None and y is not None: exif['Exif'][piexif.ExifIFD.PixelXDimension] = y exif['Exif'][piexif.ExifIFD.PixelYDimension] = x if exif.get('thumbnail') is not None: try: thumbnail = pil_open(io.BytesIO(exif.get('thumbnail'))) thumbnail = autorotate(thumbnail, orientation=orientation_value) with io.BytesIO() as bio: thumbnail.save(bio, format='jpeg') bio.seek(0) exif['thumbnail'] = bio.read() except Exception as e: warnings.warn("deprecated", DeprecationWarning) return exif
from io import BytesIO from os import environ # Package in current directory from tensorflow.lite.python.interpreter import Interpreter INTERPRETER = Interpreter(model_path='../model.tflite') INTERPRETER.allocate_tensors() INPUT_DETAILS = INTERPRETER.get_input_details() OUTPUT_DETAILS = INTERPRETER.get_output_details() with open("../example.png", "rb") as image_file: encoded_string = b64encode(image_file.read()) image_binary = pil_open(BytesIO(b64decode(encoded_string))) image_binary = image_binary.resize([96, 96]) full_image_np = asarray(image_binary, dtype='float32')/255. arr = [] BATCH_SIZE = 1 arr = [] for i in range(BATCH_SIZE): arr.append(full_image_np) full_image_np = asarray(arr, dtype='float32') start_time = int(round(time.time() * 1000)) for i in range(16384//BATCH_SIZE):
def basic_output(): set_env(title="PyWebIO Test") set_scope('top') put_markdown('### Basic') for i in range(3): put_text('text_%s' % i) put_text('测试空格:20空格:[%s]结束' % (' ' * 20)) for i in range(3): put_text('inline_text_%s' % i, inline=True) put_markdown("""### put_markdown 测试 `行内代码` 无序列表: - 北京 - 上海 - 天津 有序列表: 1. 北京 2. 上海 3. 天津 [链接](./#) ~~删除线~~ """, lstrip=True) put_link('链接', '#') put_text('<hr/>:') put_html("<hr/>") put_markdown('### Style') style(put_text('Red'), 'color:red') style([put_text('Red'), put_markdown('~~del~~')], 'color:red') put_table([ ['A', 'B'], ['C', style(put_text('Red'), 'color:red')], ]) put_collapse('title', style([ put_text('text'), put_markdown('~~del~~'), ], 'margin-left:20px'), open=True) put_markdown('### Table') put_table([ ['Name', 'Gender', 'Address'], ['Wang', 'M', 'China'], ['Liu', 'W', 'America'], ]) put_table([ ['Wang', 'M', 'China'], ['Liu', 'W', 'America'], ], header=['Name', 'Gender', 'Address']) put_table([ { "Course": "OS", "Score": "80" }, { "Course": "DB", "Score": "93" }, ], header=["Course", "Score"]) put_table([ { "Course": "OS", "Score": "80" }, { "Course": "DB", "Score": "93" }, ], header=[("课程", "Course"), ("得分", "Score")]) img_data = open(path.join(here_dir, 'assets', 'img.png'), 'rb').read() put_table( [['Type', 'Content'], ['text', '<hr/>'], ['html', put_html('X<sup>2</sup>')], ['buttons', put_buttons(['A', 'B'], onclick=None, small=True)], ['markdown', put_markdown('`awesome PyWebIO!`\n - 1\n - 2\n - 3')], ['file', put_file('hello.text', b'')], ['image', put_image(img_data)], [ 'table', put_table([['A', 'B'], [put_markdown('`C`'), put_markdown('`D`')]]) ]]) put_markdown('### Code') with use_scope('scroll_basis'): put_code( json.dumps(dict(name='pywebio', author='wangweimin'), indent=4), 'json') put_text('move ⬆ code block to screen ... :') with use_scope('scroll_basis_btns'): put_buttons(buttons=[ ('BOTTOM', Position.BOTTOM), ('TOP', Position.TOP), ('MIDDLE', Position.MIDDLE), ], onclick=lambda pos: scroll_to('scroll_basis', pos)) def show_popup(): popup('Popup title', [ put_html('<h3>Popup Content</h3>'), 'html: <br/>', put_table( [['Type', 'Content'], ['html', put_html('X<sup>2</sup>')], ['text', '<hr/>'], ['buttons', put_buttons(['A', 'B'], onclick=...)], ['markdown', put_markdown('`Awesome PyWebIO!`')], ['file', put_file('hello.text', b'')], ['table', put_table([['A', 'B'], ['C', 'D']])]]), put_buttons(['close_popup()'], onclick=lambda _: close_popup()) ], size=PopupSize.NORMAL) with use_scope('popup_btn'): put_buttons([('popup()', '')], onclick=[show_popup]) def edit_row(choice, row): put_text("You click %s button at row %s" % (choice, row), scope='table_cell_buttons') with use_scope('table_cell_buttons'): put_table([ ['Idx', 'Actions'], [ '1', put_buttons(['edit', 'delete'], onclick=partial(edit_row, row=1)) ], [ '2', put_buttons(['edit', 'delete'], onclick=partial(edit_row, row=2)) ], [ '3', put_buttons(['edit', 'delete'], onclick=partial(edit_row, row=3)) ], ]) with use_scope('put_buttons'): put_buttons(['A', 'B', 'C'], onclick=partial(put_text, scope='put_buttons')) put_markdown('### Image') put_image(img_data) from PIL.Image import open as pil_open pil_img = pil_open(path.join(here_dir, 'assets', 'img.png')) put_image(pil_img, width="30px") put_image( 'https://cdn.jsdelivr.net/gh/wang0618/pywebio/test/assets/img.png', height="50px") put_file('hello_word.txt', b'hello word!') put_collapse('Collapse', [ 'text', put_markdown('~~删除线~~'), put_table([ ['商品', '价格'], ['苹果', '5.5'], ['香蕉', '7'], ]) ], open=True) put_collapse('title', 'something', open=True) put_scrollable('scrollable\n' * 20, height=50, keep_bottom=True) put_markdown('### Scope') with use_scope('scope1'): put_text("to be cleared") clear() put_text('A') # 输出内容: A put_text('B', position=0) # 输出内容: B A put_text('C', position=-2) # 输出内容: B C A with use_scope('scope2'): put_text('scope2') put_text('scope2') put_text('D', position=1) # 输出内容: B D C A put_text('before=top again', scope='top') with use_scope('to_remove'): put_text('to remove') remove('to_remove') put_markdown('### Info') session_info = get_info() from django.http import HttpRequest from flask import Request from tornado.httputil import HTTPServerRequest from aiohttp.web import BaseRequest request_type = { 'tornado': HTTPServerRequest, 'flask': Request, 'django': HttpRequest, 'aiohttp': BaseRequest, } request_ok = isinstance(session_info.request, request_type.get(session_info.backend)) if not request_ok: print( 'Error: request check error: backend %s, request type %s, class %s' % (session_info.backend, type( session_info.request).__name__, session_info.request)) put_markdown(rf"""### 会话信息 ``` * `user_agent`: * `is_mobile` (bool): {session_info.user_agent.is_mobile} * `is_tablet` (bool): {session_info.user_agent.is_tablet} * `is_pc` (bool): {session_info.user_agent.is_pc} * `is_touch_capable` (bool): {session_info.user_agent.is_touch_capable} * `browser.family` (str): {session_info.user_agent.browser.family} * `os.family` (str): {session_info.user_agent.os.family} * `os.version` (tuple): {session_info.user_agent.os.version} * `os.version_string` (str): {session_info.user_agent.os.version_string} * `device.family` (str): {session_info.user_agent.device.family} * `device.brand` (str): {session_info.user_agent.device.brand} * `device.model` (str): {session_info.user_agent.device.model} * `user_language` (str): {session_info.user_language} * `server_host` (str): {session_info.server_host} * `origin` (str): {session_info.origin or 'http://' + session_info.server_host} * `user_ip` (str): {session_info.user_ip} * `request type check` (str): {request_ok} ``` """, strip_indent=4) put_markdown('### Layout') put_row([ put_column([ put_code('A'), put_row([ put_code('B1'), None, put_code('B2'), None, put_code('B3'), ]), put_code('C'), ]), None, put_code('python'), None, style(put_code('python\n' * 20), 'max-height:200px;'), ]) put_grid([[ style(put_code('[%s,%s]' % (x, y)), 'margin-right:10px;') for y in range(4) ] for x in range(5)], direction='column') put_row([style(put_code(i), 'margin-right:10px;') for i in range(4)], 'repeat(auto-fill, 25%)') put_markdown('### Span') cell = lambda text: style(put_code(text), 'margin-right:10px;') put_grid([ [span(cell('A'), col=2), None], [span(cell('C'), row=2, col=2), span(cell('D'), row=2)], [], ], cell_width='1fr', cell_height='1fr') put_table([ ['C'], [span('E', col=2)], ], header=[span('A', row=2), 'B']) put_processbar('processbar', 0.3) set_processbar('processbar', 0.6) put_loading() # output hobby = output(put_text('Coding')) put_table([['Name', 'Hobbies'], ['Wang', hobby]]) hobby.reset(put_text('Movie')) hobby.append(put_text('Music'), put_text('Drama')) hobby.insert(0, put_markdown('**Coding**'))
for i in xrange(RW_COUNT): tiff = Image(TIFF) arr = asarray(tiff) arr = arr[..., ::-1] bytescale(arr, 64, 192) end = time() - start print " - tiff load + numpy.asarray() with bytescale() %0.3f sec" % end # ------------------------------------------------------------------- # PIL load hdr = "PIL %s (with standard libjpeg)" % PIL_VERSION print "\n%s\n%s" % (hdr, "-" * len(hdr)) start = time() for i in xrange(RW_COUNT): pil_open(IMG).load() end = time() - start print " - read JPEG %0.3f sec" % end # load resaved pil_open(IMG).save("pon_rewrite_pil.jpg") start = time() for i in xrange(RW_COUNT): pil_open("pon_rewrite_pil.jpg").load() end = time() - start print " - read JPEG %0.3f sec (resaved)" % end # write img = pil_open(IMG) img.load() start = time()
def pil(src=IMG, dst=OUT, pil_open=pil_open): img = pil_open(src) for w, h in ((128, 182), (300, 427), (500, 712), (700, 997), (900, 1281)): new = img.resize((w, h), ANTIALIAS) new.save(dst)
def test_open(): img = Image.open(img_path).as_pil() pil_img = pil_open(img_path) assert pil_equals(img, pil_img)